如果没有,如何动态获取多个数据库的多个池?解决方案每个JDBC网址都需要一个不同的c3p0数据源.连接池必须包含同构连接:从客户端的角度来看,所有签出的连接必须等效.如果来自多个数据库的连接包含在同一个池中,则客户端将无法指定或知道与之通信的数据库.(例如,如果您正在复制一个只读数据库,并且您确实希望来自多个源的连接驻留在单个池中,因为从客户端的角度保证它们是等效的,则可以通过以下方法实现:定义一个自定义的,未池化的数据源,该数据源循环或随机选择一个副本,然后通过c3p0的DataSources工厂合并该数据源.)动态创建和配置c3p0数据源非常容易.请参见示例代码此处.如果将动态配置捕获为c3p0属性名称到值的映射,则还会有另一种更简洁的方法来获取具有该配置的数据源.I am developing a tool that receives different connection parameters to test values in different databases (a plugin for Nagios in jNRPE that keeps an open connection to different databases).Because the configuration is dynamic (there could be more databases or they can be removed) I cannot have a configuration file.I want to know if I should have an instance of C3P0 per database or can I use the same instance and just change the URL each time I ask for a connection?The code is at github:https://github.com/angoca/db2-jnrpe/blob/master/src/main/java/com/github/angoca/db2_jnrpe/database/pools/c3p0/DBCP_c3p0.javaIf not, how can I get multiple pool for multiple databases dynamically? 解决方案 You'll need a different c3p0 DataSource for each JDBC url. A Connection pool must contain homogeneous Connections: all checked out Connections must be equivalent from a client's perspective. If Connections from multiple databases were included in the same pool, clients would have no way of specifying or knowing which db they were communicating with.(If you are replicating, say, a read-only DB and you really want Connections from multiple sources to live in a single pool, because they are guaranteed to be equivalent from a client's point of view, you could do that by defining a custom, unpooled DataSource that round-robined or randomly chose a replicant, and then pooling the DataSource via c3p0's DataSources factory.)It is very easy to dynamically create and configure c3p0 DataSources. See example code here.If you capture your dynamic config as a map of c3p0 property names to values, there's also an alternative, more concise way to get a DataSource with that configuration. 这篇关于C3P0的多个数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-12 09:28