Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading from multiple Db's with same Persistence Unit?

I need some help to configure several connection's to multiple db's using the same Persistence unit.

They all have the same schema. Therefore I want to use the same Persistence unit/ DAO's etc and dont want to have to setup 10 EntityManagers, 10 Persistence xml's etc. Is there a way to do this? Here is my current config:

  <persistence-unit name="PersistenceUnit-c1" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="hibernate.show_sql" value="${hibernate-show-sql}"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.SybaseDialect" />
            <property name="hibernate.c3p0.min_size" value="${hibernate-c3p0-min-size}" />
            <property name="hibernate.c3p0.max_size" value="${hibernate-c3p0-max-size}" />
            <property name="hibernate.c3p0.timeout" value="${hibernate-c3p0-timeout}" />
            <property name="hibernate.c3p0.max_statements" value="${hibernate-c3p0-max-statements}" />
            <property name="hibernate.c3p0.idle_test_period" value="${hibernate-c3p0-idle-test-periods}" />     
        </properties>
        <class>com.domain.TktOrder</class>
        <exclude-unlisted-classes/>
    </persistence-unit>

I am also using Spring/hibernate to set up my context:

    <bean id="EntityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:persistenceUnitName="PersistenceUnit-c1" 
    p:dataSource-ref="DataSource">
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
            p:showSql="${hibernate-show-sql}" 
            p:generateDdl="false" 
            p:databasePlatform="org.hibernate.dialect.SybaseDialect" />
    </property>
    <property name="loadTimeWeaver">
        <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    </property>
</bean>

<bean id="DataSource" 
    class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" 
    p:driverClass="net.sourceforge.jtds.jdbc.Driver"
    p:jdbcUrl="jdbc:jtds:sybase://url.net:port;DatabaseName=db_1"
    p:user="user" 
    p:password="password"
    />

and finally I use:

@PersistenceContext(unitName="PersistenceUnit-c1")
public void setEntityManager(EntityManager entityManager)
{
    this.entityManager = entityManager;     

}

to inject my EntityManager into my DAO

How can I extend this model to be able to use db1 then change the data source and execute again for db2 etc?

Many thanks for any help in advance!

like image 560
Michael W Avatar asked Dec 06 '11 15:12

Michael W


2 Answers

After a few attempts I have found a solution that seems to fit the bill.

Please first have a look at this: dynamic-datasource-routing

This uses a few custom classes which you will need and the key class is AbstractRoutingDataSource.

This reconfigures my datasource bean like so:

    <bean id="dataSource" class="com.domain.etc.etc.recon.utils.RoutingDataSource">
    <property name="targetDataSources">
        <map key-type="com.domain.etc.etc.recon.utils.DbType">
            <entry key="C1" value-ref="C1" />
            <entry key="C2" value-ref="C2" />
        </map>
    </property>
    <property name="defaultTargetDataSource" ref="C3" />
</bean>

Where Connection one C1, C2 look like:

    <bean id="parentDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    abstract="true">
    <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
    <property name="username" value="*******" />
    <property name="password" value="*******" />
</bean>

<bean id="C1" parent="parentDataSource">
    <property name="url" 
        value="jdbc:jtds:sybase://URL:PORT;DatabaseName=dbname" />
</bean>

<bean id="C2" parent="parentDataSource">
    <property name="url"
        value="jdbc:jtds:sybase://URL:PORT;DatabaseName=dbname2" />
</bean>

<bean id="C3" parent="parentDataSource">
    <property name="url"
        value="jdbc:jtds:sybase://URL:PORT;DatabaseName=dbname3" />
</bean>

you can inject this into the EntityManager as I have in the original Question;

    <bean id="EntityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:persistenceUnitName="PersistenceUnit" 
    p:dataSource-ref="dataSource">
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence-.xml" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
            p:showSql="${hibernate-show-sql}" 
            p:generateDdl="false" 
            p:databasePlatform="org.hibernate.dialect.SybaseDialect" />
    </property>
    <property name="loadTimeWeaver">
        <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    </property>
</bean>

After this you need to use your own implementation of the java classes in the link above to be able to switch between data sources. This is nothing more than renaming the classes to ones that are more meaning full to you. Linking the Enum up to C1,C2,C3 etc and finally pointing to your own dao to carry out the work.

Good Luck!

like image 105
Michael W Avatar answered Nov 05 '22 19:11

Michael W


This might be a use case for Hibernate Shards. We started evaluating it a year ago but then dropped it because all of a sudden sharding wasn't needed after all. So I cannot provide any real experience with it, but I think it's worth a look.

like image 2
Robert Petermeier Avatar answered Nov 05 '22 19:11

Robert Petermeier