9ine
9ine

Reputation: 859

Can configure two DataSoure in a Spring configuration?

I am developing a web application and I need two DataSource to connect two difference database according to my requirement.One DataSource will use Spring + JPA framework and another DataSource is use Spring + MyBatis framework.

Upvotes: 0

Views: 417

Answers (3)

Pune
Pune

Reputation: 91

Here is the sample code for you

class Main {
  public static void main(String args[]) throws Exception {
    ApplicationContext ac = new ClassPathXmlApplicationContext("context.xml", Main.class);
    DataSource dataSource = (DataSource) ac.getBean("dataSource");
    DataSource mysqlDataSource = (DataSource) ac.getBean("mysqlDataSource");

Context.xml

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@oracle.devcake.co.uk:1521:INTL"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
</bean>

<bean id="mysqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://dbhost-prospring-psql/prospring"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
</bean>

<bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler">
    <property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/>
</bean>

<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"/>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"/>
</bean>

Upvotes: 1

Shailendra
Shailendra

Reputation: 9102

You can configure as many databases as you like in Spring context by declaring datasource beans with different id's and injecting appropriate properties from properties file. if the two databases are different then you are in territory of distributed transactions and you must configure a Spring transaction manager which can operate with JTA. Also worth noting is that Spring transaction manager is merely an abstraction and it needs to have external JTA transaction manager configured (like Bitrionix / Atomikos) or if deploying under EE application server then transaction manager can be looked up in JNDI registry. Then when you mark the transaction boundary (perhaps using Spring Transactional annotation) then Spring would automatically co-ordinate the transactions.

Upvotes: 0

Koitoer
Koitoer

Reputation: 19533

Yes you can I suggest manage both from spring and obtained from the applicationContext.

<bean class="org.apache.tomcat.jdbc.pool.DataSource" id="dataSource" >
        <property name="driverClassName" value="${database.driverClassName}"/>
        <property name="url" value="${database.url}"/>
        <property name="username" value="${database.username}"/>
        <property name="password" value="${database.password}"/>

   <bean class="org.apache.tomcat.jdbc.pool.DataSource" id="dataSourceOrderDetail" >
        <property name="driverClassName" value="${database.driverClassName}"/>
        <property name="url" value="${database.url.orderdetail}"/>
        <property name="username" value="${database.username}"/>
        <property name="password" value="${database.password}"/>
  </bean>

Just need to have different name of id, to be properly injected

Check this to review how you can integrate spring with ibatis then configure beans using the datasource beans

Or if you want to use datasource-ds.xml just put the two datasource xml files in the lib folder within your application context, if you are using something like jboss or tomcat.

UPDATE

<jpa:repositories base-package="com.staples.sa.pricemart.repository.pag"
    entity-manager-factory-ref="entityManagerFactory" />

<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <qualifier value="pagTransactionManager" />
</bean>

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnit" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
</bean>
<jpa:repositories base-package="com.staples.sa.pricemart.repository.orderdetail"
    entity-manager-factory-ref="entityManagerFactoryOrderDetail" />


<bean id="transactionManagerOrderDetail" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactoryOrderDetail" />
    <qualifier value="orderDetailTX" />
</bean>


<bean
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    id="entityManagerFactoryOrderDetail">
    <property name="persistenceUnitName" value="persistenceUnitOrderDetail" />
    <property name="dataSource" ref="dataSourceOrderDetail" />
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
</bean>

<!-- -->

Persistence.xml need to look like this. (need to complete the xml config)

<persistence-unit name="persistenceUnit"
        transaction-type="RESOURCE_LOCAL">

And

<!-- Add the persistence context for OrderDetail -->
<persistence-unit name="persistenceUnitOrderDetail"
    transaction-type="RESOURCE_LOCAL">

Upvotes: 1

Related Questions