[英]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
EDIT 编辑
我相信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: 我有一张泳池地图:
Map < connectionUrlString,List< c3poPool > > map映射<connectionUrlString,List <c3poPool >> map
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.
让我解释一下你是如何解决这个问题的。
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)使用您要加载的属性参数化的池数据库配置。
Create a locator class: In this class you'll need a HashMap 创建一个定位器类:在这个类中,你需要一个HashMap
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.