简体   繁体   English

用于动态数据库连接的连接池

[英]Connection pool for dynamic database connections

The problem setup is based on a webservice (Spring/Java, Tomcat7 and MySql) where every user gets their own database, hence each request needs their own connection. 问题设置基于Web服务(Spring / Java,Tomcat7和MySql),其中每个用户都有自己的数据库,因此每个请求都需要自己的连接。 As all databases are created dynamically during runtime, configuring them statically before startup is not an option. 由于所有数据库都是在运行时动态创建的,因此在启动之前静态配置它们不是一种选择。

To optimise database connection usage, an implementation of a database connection pool would be great, right? 为了优化数据库连接使用,数据库连接池的实现会很棒,对吧?

With Java/Spring: How would I create a connection pool for dynamic databases? 使用Java / Spring:如何为动态数据库创建连接池? I am a bit struck by the lack of clean options here! 我对这里缺乏干净的选择感到震惊!

Problem : Tomcat's Connection Pool (and as far as i understand C3P0 as well) treats each new DataSource instance as a whole new connection pool -> stack-reference 问题Tomcat的连接池 (据我所知也理解为C3P0 )将每个新的DataSource实例视为一个完整的新连接池 - > stack-reference

  1. Is it a good idea to create a static datasource with a generic MySql connection (without specifing the database on connection) and use a connection pool with this datasource together with adapted SQL statements? 创建具有通用MySql连接的静态数据源(不指定连接时的数据库)并使用带有此数据源的连接池以及适配的SQL语句是一个好主意吗?
    stack-reference 堆叠参考
  2. What about developing a custom persistent database based datasource pool? 如何开发基于自定义持久数据库的数据源池? Any experience with performance here? 有表演经验吗? Any advice? 有什么建议? Any libraries that do that? 任何图书馆都这样做?
  3. Or would it be feasable to workaround Tomcat's DataSource problem by creating Tomcat JNDI Datasources dynamically by manipulating it's context.xml dynamically from Java? 或者通过动态地从Java动态创建Tomcat JNDI数据源来解决Tomcat的DataSource问题是否可行?
  4. I can't believe that there aren't more plain/simple solutions for this. 我无法相信没有更简单/简单的解决方案。 Grails/Hibernate struggles with this, Java/JDBC struggles with this, ... is it such a rare use-case to separate userdata on a user basis by creating user specific databases dynamically? Grails / Hibernate正在努力解决这个问题,Java / JDBC正在努力解决这个问题......是否通过动态创建用户特定数据库来分离用户数据这种罕见的用例? If so, what would be a better setup? 如果是这样,那么更好的设置是什么?

EDIT 编辑

  1. Another option is the suggestion from @M.Deinum to use a single configured datasource and dynamically hotswap it for the right connection -> M.Deinum Blog and stack-reference . 另一种选择是来自@ M.Deinum的建议, 使用单个已配置的数据源并动态热交换以获得正确的连接 - > M.Deinum Blogstack-reference
    How does that perform with a connection pool like the ones above? 如何使用上面的连接池执行?

我相信HikariCP无需指定单个数据库即可运行。

Once the databases are created in runtime, you have to create the pools also in runtime. 在运行时创建数据库后,您还必须在运行时创建池。 I am afraid the spring infrastructure is not giving you any help here, as it is tuned for the usual static use case. 我担心弹簧基础设施在这里没有给你任何帮助,因为它是针对通常的静态用例进行调整的。

I'd have a map of pools: 我有一张泳池地图:

  • have a 有一个
     Map < connectionUrlString,List< c3poPool > > map 映射<connectionUrlString,List <c3poPool >> map 
  • when requesting a connection, get the corresponding c3po pool from the map 请求连接时,从地图中获取相应的c3po池
  • and you can get the best of both worlds, since the real connection pool for each dynamically created database is handled by a c3po instance, but you can create new instances in runtime 并且您可以充分利用这两个世界,因为每个动态创建的数据库的真实连接池都由c3po实例处理,但您可以在运行时创建新实例

This works as a low-level solution. 这是一个低级解决方案。 If you want to go further, you can wrap this logic into a db connection provider, and register that as a "driver". 如果您想更进一步,可以将此逻辑包装到数据库连接提供程序中,并将其注册为“驱动程序”。 This way any part of your application requests a new connection, you can just return one connection from the existing pools (and if a totally new connection is requested, create a new pool for that). 这样,应用程序的任何部分都会请求新连接,您只需从现有池返回一个连接(如果请求了一个全新的连接,则为此创建一个新池)。

First than all, sorry for my english, i'm improving every day. 首先,对不起我的英语,我每天都在进步。

In my experience, I had a similar situation and it was resolve with spring framework. 根据我的经验,我有一个类似的情况,它是春天框架的决心。 Let me explain you how you'd solve that question. 让我解释一下你是如何解决这个问题的。

  1. Make a spring config file with these characteristics: a) A resource loader: This one is the responsible of load properties from configurations files or from database, those properties will be the appropriates to establish the database connection. 制作具有以下特征的spring配置文件:a)资源加载器:这一个负责从配置文件或数据库加载属性,这些属性将适合建立数据库连接。 b) A pool database configuration parameterized with the properties that you'll load. b)使用您要加载的属性参数化的池数据库配置。

  2. Create a locator class: In this class you'll need a HashMap 创建一个定位器类:在这个类中,你需要一个HashMap

  3. Use the multi context feature of spring: The idea is assign a code to every one connection that you establish and later load that connection like an application context with spring, then in the locator class, put in the map that context and use it as frequent as you need. 使用spring的多上下文功能:想法是为您建立的每个连接分配一个代码,然后像使用spring的应用程序上下文一样加载该连接,然后在locator类中,将该上下文放入映射并将其作为频繁使用如你所愿。

I think is you follow these steps, you can create dynamic pool or database connection as you want. 我认为您按照这些步骤操作,可以根据需要创建动态池或数据库连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM