If the container supports jta, then spring configuration is very simple, because spring provides JtaTransactionManager, which provides the jndi name of jta for almost all servers, such as
*
"java:comp/UserTransaction"for Resin 2 .x, Oracle OC4J (Orion), JOnAS (JOTM), BEA WebLogic*"java:comp/TransactionManager"for Resin 3 .x*"java:appserver/TransactionManager"for GlassFish*"java:pm/TransactionManager"for Borland Enterprise Server and Sun Application Server (Sun ONE 7 and later)*"
java:/TransactionManager " for JBoss Application Server
java:/TransactionManager " for JBoss Application Server
Since most servers get jta in different ways, spring also provides different jta implementations for different servers, for example:
WebLogicJtaTransactionManager
WebSphereUowTransactionManager
OC4JJtaTransactionManager
WebSphereUowTransactionManager
OC4JJtaTransactionManager
Participate in the specific content:
http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/transaction/jta/JtaTransactionManager.html
Whether it is jdbc, hibernate or ibatis, you can configure the distribution through JtaTransactionManager The
following is a simple post for some configuration code:
hibernate:
<beans>
<jee:jndi-lookup id="myDataSource1" jndi-name="dsjndi1" />
<jee:jndi-lookup id="myDataSource2" jndi-name="dsjndi2" />
<bean id="mySessionFactory1"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="myDataSource1" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop>
<prop key="hibernate.show_sql"> true </prop>
<prop key="hiberante.format_sql"> true </prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value> com.landor.test.entity.User </value>
</list>
</property>
<property name="annotatedPackages">
<list>
<value> com.landor.test.entity </value>
</list>
</property>
</bean>
<bean id="mySessionFactory2"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="myDataSource2" />
</property>
</bean>
<bean id="myTxManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
<!--如果默认id= transactionManager,那么可以有下面的标签,这样spring会自动寻找相应的jta的jndi并且注入
<tx:jta-transaction-manager/> -->
<tx:advice id="txAdvice" transaction-manager="myTxManager">
<tx:attributes>
<tx:method name="select*" read-only="true" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="update*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="add*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
<tx:method name="delete*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
</tx:attributes>
</tx:advice>
</beans>
<jee:jndi-lookup id="myDataSource1" jndi-name="dsjndi1" />
<jee:jndi-lookup id="myDataSource2" jndi-name="dsjndi2" />
<bean id="mySessionFactory1"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="myDataSource1" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop>
<prop key="hibernate.show_sql"> true </prop>
<prop key="hiberante.format_sql"> true </prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value> com.landor.test.entity.User </value>
</list>
</property>
<property name="annotatedPackages">
<list>
<value> com.landor.test.entity </value>
</list>
</property>
</bean>
<bean id="mySessionFactory2"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="myDataSource2" />
</property>
</bean>
<bean id="myTxManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
<!--如果默认id= transactionManager,那么可以有下面的标签,这样spring会自动寻找相应的jta的jndi并且注入
<tx:jta-transaction-manager/> -->
<tx:advice id="txAdvice" transaction-manager="myTxManager">
<tx:attributes>
<tx:method name="select*" read-only="true" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="update*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
<tx:method name="add*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
<tx:method name="delete*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
</tx:attributes>
</tx:advice>
</beans>
ibatis:
ibatis与hibernate是一样的,只需要将hibernate的sessionFactory部分更换一下
......
<bean id="sqlMap1Client"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="myDataSource1" />
<property name="configLocation" value="xxx/SqlMapConfig.xml" />
</bean>
<bean id="sqlMap2Client"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="OssDS" />
<property name="configLocation" value="xxx/SqlMapConfig.xml" />
</bean>
......
<bean id="sqlMap1Client"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="myDataSource1" />
<property name="configLocation" value="xxx/SqlMapConfig.xml" />
</bean>
<bean id="sqlMap2Client"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="OssDS" />
<property name="configLocation" value="xxx/SqlMapConfig.xml" />
</bean>
......