Can I use multiple C3P0 datasources for DB instance?

那年仲夏 提交于 2020-02-01 05:41:10

问题


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.

Thanks.


回答1:


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

  • autowiring DataSource by type won't work

  • @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")
    

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.

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>

See also:

  • What is meant by abstract="true" in spring?


来源:https://stackoverflow.com/questions/12922351/can-i-use-multiple-c3p0-datasources-for-db-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!