3

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.

scabbage
  • 1,412
  • 2
  • 15
  • 27

1 Answers1

5

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:

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • I'm not using autowiring. I manually define the dependencies in the applicationContext.xml file. I have a dependency graph like this: `MessageService=>MessageDAO=>sessionFactory=>dataSource1` and `PersonService=>PersonDAO=>sessionFactory=>dataSource2`. Both services have `@Transactional` declaration for the functions. I hope this works. Thanks. – scabbage Oct 16 '12 at 20:20
  • @scabbage: then it will "just work". However see my update on `@Transactional`. – Tomasz Nurkiewicz Oct 16 '12 at 20:26