Friday, December 24, 2010

Access Multiple Database Using Spring 3, Hibernate 3 and Atomikos

This blog is in continuation with previous blog http://tiwarij2eeblog.blogspot.com/2010/12/handling-transaction-with-multiple.html


In the previous example we had used different transaction managers for different databases. 


Now we will use only one transaction manger to coordinate the transactions using Atomikos. You can download dependent jar files from http://www.atomikos.com/.


All the java code is same from previous blog except applicationContext.xml contents.


Below xml file contents show how to configure data sources and Spring JTA transaction manger to access multiple data sources.


This strategy can be used when you are not having J2EE application server to run your application.


Just replace the applicationContext.xml and run the test again.


Here we are using only one JTA transaction manager.



<?xml version="1.0" encoding="UTF-8"?>
<!--
Application context definition for Online Exam
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/jdbc
            http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<context:annotation-config />
<aop:aspectj-autoproxy />


<bean id="mySQLDatasource" 
class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean">
<property name="uniqueResourceName"><value>NONXADBMS</value></property>
<property name="url"><value>jdbc:mysql://localhost/pradeep</value></property>
<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
<property name="user"><value>root</value></property>
<property name="password"><value>root</value></property>
<property name="poolSize"><value>1</value></property>
<property name="borrowConnectionTimeout"><value>60</value></property>
</bean>


<bean id="oracleDatasource" 
class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean">
<property name="uniqueResourceName"><value>NONXADBMSORACLE</value></property>
<property name="url"><value>jdbc:oracle:thin:@localhost:1521:ORACLE</value></property>
<property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
<property name="user"><value>pradeep</value></property>
<property name="password"><value>pradeep</value></property>
<property name="poolSize"><value>1</value></property>
<property name="borrowConnectionTimeout"><value>60</value></property>
</bean>


<!-- Hibernate plugin code -->

<bean id="mysqlSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="mySQLDatasource"></property>
<property name="configLocation" value="classpath:mysql_hibernate.cfg.xml" />
</bean>


<bean id="oracleSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="oracleDatasource"></property>
<property name="configLocation" value="classpath:oracle_hibernate.cfg.xml" />
</bean>


<!-- Transaction managers -->
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<!--  when close is called, should we force transactions to terminate or not?-->
<property name="forceShutdown"><value>true</value></property>
</bean>


<!-- Also use Atomikos UserTransactionImp, needed to configure Spring  --> 
<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
     <property name="transactionTimeout"><value>300</value></property>
</bean>


<!-- Configure the Spring framework to use JTA transactions from Atomikos
-->
<bean id="springTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager"><ref bean="atomikosTransactionManager"  /></property>
<property name="userTransaction"><ref bean="atomikosUserTransaction"  /></property>
</bean>


<aop:config>
<aop:pointcut id="purchaseBeanMethods"
expression="execution(* org.paandav.PurchaseServiceIfc.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="purchaseBeanMethods" />
</aop:config>


<tx:advice id="txAdvice" transaction-manager="springTransactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>


<!-- Service -->
<bean id="purchaseBean" class="org.paandav.PurchaseServiceImpl" />


<!-- DAO -->
<bean id="itemDAO" class="org.paandav.ItemDAOImpl"></bean>


<bean id="purDAO" class="org.paandav.PurchaseDAOImpl"></bean>
</beans>

2 comments:

  1. can you plz post a running project ??

    ReplyDelete
  2. This is an excellent post I seen thanks to share it. It is really what I wanted to see hope in future you will continue for sharing such a excellent post.
    omni channel call center services in Nigeria

    ReplyDelete