At present, the development of the Java EE system uses two data sources, requires the support of distributed Things (JTA), but Tomcat does not support JTA, development debugging is not very convenient, this article through the use of Atomikos to achieve the support of distributed transactions, the theory can be run in any Java container.
First, put the following jar package into the Lib directory
Second, put the configuration file jta.properties in the Web-inf directory, the content is as follows:
Third, modify the spring configuration file, set up 2 data sources:
<bean id= "DataSource" class= "Com.atomikos.jdbc.AtomikosDataSourceBean" init-method= "Init" destroy-method= "close "> <description>oracle xa datasource</description> <property name= "Uniqueresourcename" > <value>oracle_ds</value> </property> <property name= "Xadatasourceclassname" > <value>${jdbc. Xadriverclassname}</value> </property> <property name= "Xaproperties" > <props> <prop key= "User" >${jdbc.username}</prop> <prop key= "Password" >${jdbc.password}</prop> <prop key= "URL" >${jdbc.url}</prop> </props> </property> <property name= "Minpoolsize" value= "5"/> <property name= "maxpoolsize" value= "/>" <property name= "borrowconnectiontimeout" value= "/>" <property name= "Testquery" value= "SELECT 1 from Dual"/> <property name= "Maintenanceinterval" value= "/>" </bean>
<bean id= "Datasourceforadmin" class= "Com.atomikos.jdbc.AtomikosDataSourceBean" init-method= "Init" destroy-method= "Close" > <description>oracle xa datasource</description> <property name= "Uniqueresourcename" > <value>oracle_ds_admin</value> </property> <property name= "Xadatasourceclassname" > <value>${admin.jdbc.XADriverClassName}</value> </property> <property name= "Xaproperties" > <props> <prop key= "User" >${admin.jdbc.username}</prop> <prop key= "Password" >${admin.jdbc.password}</prop> <prop key= "URL" >${admin.jdbc.url}</prop> </props> </property>
<property name= "Minpoolsize" value= "5"/> <property name= "maxpoolsize" value= "/>" <property name= "borrowconnectiontimeout" value= "/>" <property name= "Testquery" value= "SELECT 1 from Dual"/> <property name= "Maintenanceinterval" value= "/>" </bean>
<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean" > <property name= "DataSource" ref= "DataSource"/> <property name= "Hibernateproperties" > <props>
<prop key= "Hibernate.dialect" >org.hibernate.dialect.Oracle10gDialect</prop> <prop key= "Hibernate.show_sql" >true</prop> <prop key= "Hibernate.format_sql" >false</prop> <prop key= "Hibernate.current_session_context_class" >jta</prop> <prop key= "Hibernate.transaction.factory_class" >org.hibernate.transaction.jtatransactionfactory</prop > </props> </property> <property name= "Packagestoscan" value= "Com.domain"/>
</bean> <bean id= "Sessionfactoryforadmin" class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean" > <property name= "DataSource" ref= "Datasourceforadmin"/> <property name= "Hibernateproperties" > <props>
<prop key= "Hibernate.dialect" >org.hibernate.dialect.Oracle10gDialect</prop> <prop key= "Hibernate.show_sql" >true</prop> <prop key= "Hibernate.format_sql" >false</prop> <prop key= "Hibernate.current_session_context_class" >jta</prop> <prop key= "Hibernate.transaction.factory_class" >org.hibernate.transaction.jtatransactionfactory</prop > </props> </property> <property name= "Packagestoscan" value= "Com.domain"/>
</bean>
<bean id= "Atomikostransactionmanager" class= "Com.atomikos.icatch.jta.UserTransactionManager" Init-method= "Init" destroy-method= "Close" > <description>UserTransactionManager</description> <property name= "Forceshutdown" > <value>true</value> </property> </bean>
<bean id= "atomikosusertransaction" class= "COM.ATOMIKOS.ICATCH.JTA.USERTRANSACTIONIMP" > <property name= "transactiontimeout" value= "/>" </bean>
<bean id= "Txmanager" class= "Org.springframework.transaction.jta.JtaTransactionManager" > <property name= "TransactionManager" > <ref bean= "Atomikostransactionmanager"/> </property> <property name= "UserTransaction" > <ref bean= "Atomikosusertransaction"/> </property> </bean>
<!--configuring transaction facets--- <tx:advice id= "Txadvice" transaction-manager= "Txmanager" > <tx:attributes> <tx:method name= "get*" propagation= "REQUIRED" read-only= "true"/> <tx:method name= "find*" propagation= "REQUIRED" read-only= "true"/> <tx:method name= "query*" propagation= "REQUIRED" read-only= "true"/> <tx:method name= "select*" propagation= "REQUIRED" read-only= "true"/> <tx:method name= "save*" propagation= "REQUIRED"/> <tx:method name= "add*" propagation= "REQUIRED"/> <tx:method name= "delete*" propagation= "REQUIRED"/> <tx:method name= "del*" propagation= "REQUIRED"/> <tx:method name= "update*" propagation= "REQUIRED"/> <tx:method name= "execute*" propagation= "REQUIRED"/> <tx:method name= "exec*" propagation= "REQUIRED"/> <tx:method name= "*" propagation= "REQUIRED" read-only= "true"/> </tx:attributes> </tx:advice>
|
IV. Support for Oracle data sources:
The oracle10g is used here and requires a driver that supports XA data sources, either Ojdbc14.jar or Ojdbc6.jar.
The configuration is as follows, note and the single data source driver class name Difference
V. DAO and different sessionfactory associations:
<bean id= "Answerquerydao" class= "Com.dao.impl.AnswerQueryDaoImpl" scope= "singleton" >
<property name= "Sessionfactory" > <ref bean= "Sessionfactory"/> </property>
</bean>
<bean id= "Adminlogintokendao" class= "Com.dao.impl.AdminLoginTokenDaoImpl" scope= "singleton" >
<property name= "Sessionfactory" > <ref bean= "Sessionfactoryforadmin"/> </property> </bean> |
Six, can only use Opensession to get the session, cannot be obtained through getcurrentsession, and to call the Flush method to make the addition of the delete modify operation takes effect as follows:
Seven, the test is successful, for the needs of peer reference, inappropriate, but also invite netizens to shoot bricks.
struts2+spring3.2.9+hibernate4.2.0+atomikos3.8 Implementing Distributed Transaction JTA