我可以为数据库实例使用多个C3P0数据源吗? [英] Can I use multiple C3P0 datasources for DB instance?

查看:91
本文介绍了我可以为数据库实例使用多个C3P0数据源吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以为一个数据库运行多个c3p0数据源,例如:

I was wondering if I can run multiple c3p0 datasources for one DB, something like:

<bean id="dataSource1" class = "com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${db.driverClassName}"/>
    <property name="jdbcUrl" value="${db.url}/schema1"/>
    <property name="user" value="${db.username}"/>
    <property name="password" value="${db.password}"/>

    <property name="acquireIncrement" value="1" />
    <property name="idleConnectionTestPeriod" value="100"/>
    <property name="minPoolSize" value="5" />
    <property name="maxPoolSize" value="50" />
    <property name="maxIdleTime" value="1800" />
</bean>

<bean id="dataSource2" class = "com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${db.driverClassName}"/>
    <property name="jdbcUrl" value="${db.url}/schema2"/>
    <property name="user" value="${db.username}"/>
    <property name="password" value="${db.password}"/>

    <property name="acquireIncrement" value="1" />
    <property name="idleConnectionTestPeriod" value="100"/>
    <property name="minPoolSize" value="5" />
    <property name="maxPoolSize" value="50" />
    <property name="maxIdleTime" value="1800" />
</bean>

差异持久化服务将使用它们.

They will be used by difference persistence services.

谢谢.

推荐答案

那绝对没问题.您将遇到一些配置问题,例如:

That's absolutely fine. You'll run into some configuration issues like:

  • 按类型自动装配DataSource无效

@Transactional/声明式事务将仅对选择的DataSource起作用.另外,您必须手动告诉您要使用哪个事务管理器(因此,您还将需要两个事务管理器:transactionManager1transactionManager2):

@Transactional/declarative transactions will work only against one, selected DataSource. Alternatively you'll have to manually tell which transaction manager you want to use (thus you'll need two transaction managers as well: transactionManager1 and transactionManager2):

@Transactional("transactionalManager2")

此外,此配置没有任何问题.实际上,这是个不错的主意(+1):如果应用程序的某些层/组件使池饱和,则其他层/组件仍可以运行.

Besides, there's nothing wrong with this configuration. Actually it's a pretty good idea (+1): if some layers/components of your application saturate the pool, others can still operate.

我唯一推荐的方法是使用鲜为人知的abstract/parent bean声明,以避免重复:

The only thing I recommend is to use lesser known abstract/parent bean declarations to avoid repetition:

<bean id="dataSource" abstract="true" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${db.driverClassName}"/>
    <property name="user" value="${db.username}"/>
    <property name="password" value="${db.password}"/>
    <property name="acquireIncrement" value="1" />
    <property name="idleConnectionTestPeriod" value="100"/>
    <property name="minPoolSize" value="5" />
    <property name="maxPoolSize" value="50" />
    <property name="maxIdleTime" value="1800" />
</bean>

<bean id="dataSource1" parent="dataSource">
    <property name="jdbcUrl" value="${db.url}/schema1"/>
</bean>
<bean id="dataSource2" parent="dataSource">
    <property name="jdbcUrl" value="${db.url}/schema2"/>
</bean>

另请参见:

  • > abstract =" true是什么意思?在春天?
  • See also:

    • What is meant by abstract="true" in spring?
    • 这篇关于我可以为数据库实例使用多个C3P0数据源吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆