分佈式事務是指操做多個數據庫之間的事務,在tomcat下,是沒有分佈式事務的,不過能夠藉助於第三方軟件jotm(Java Open Transaction Manager )和AtomikosTransactionsEssentials實現,mysql
(PS:Spring3 已經再也不支持jotm了,我如今使用 AtomikosTransactionsEssentials)web
在spring中分佈式事務是經過jta(jotm,atomikos)來進行實現,下面是採用jotm進行實現spring跨庫之間的事務spring
jotm下載地址:http://jotm.ow2.org/xwiki/bin/view/Main/Download_Releasessql
我採用的是2.1.4版本的,解壓后里面有不少jar包,只須要提取其中的幾個便可:數據庫
carol.jar,howl.jar,jotm-core.jar,jotm-datasource.jar,ow2-connector-1.5-spec.jar,ow2-jta-1.1-spec.jar,xapool.jar,jotm-client.jar,commons-cli-1.0.jarexpress
applicationContext.xml配置以下:tomcat
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">session
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>app
<!--定義jta事務-->分佈式
<bean id="tsManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="jotm"/>
</bean>
<!--使用xapool鏈接池-->
<bean id="familyDataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
<property name="dataSource">
<bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
<property name="transactionManager" ref="jotm"/>
<property name="driverName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/systemdb"/>
</bean>
</property>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean id="localDataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
<property name="dataSource">
<bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
<property name="transactionManager" ref="jotm"/>
<property name="driverName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/userdb"/>
</bean>
</property>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean id="sessionFactorySystemdb"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/cn/luotoo/system/model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.batch_size">15</prop>
<prop key="hibernate.connection.autocommit ">false</prop>
</props>
</property>
<property name="dataSource">
<ref bean="familyDataSource"/>
</property>
</bean>
<!-- userdb sessionFactory -->
<bean id="sessionFactoryUserdb"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/cn/luotoo/user/model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.batch_size">15</prop>
<prop key="hibernate.connection.autocommit ">false</prop>
</props>
</property>
<property name="dataSource">
<ref bean="localDataSource"/>
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="tsManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--面向切面事務-->
<aop:config>
<aop:advisor pointcut="execution(* cn.luotoo.user.service.*.*(..))" advice-ref="txAdvice"/>
<aop:advisor pointcut="execution(* cn.luotoo.system.service.*.*(..))" advice-ref="txAdvice"/>
<!--
<aop:pointcut id="interceptorPointCuts"
expression="execution(* cn.luotoo.user.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice"
pointcut-ref="interceptorPointCuts"/>-->
</aop:config>
<bean id="userDao" class="cn.luotoo.user.dao.impl.UserDaoHibernate">
<property name="sessionFactory">
<ref local="sessionFactoryUserdb"/>
</property>
</bean>
<bean id="userService" class="cn.luotoo.user.service.impl.UserServiceImpl">
<property name="userDao"><ref local="userDao"/></property>
<property name="testDao"><ref local="testDao"/></property>
</bean>
<bean id="testDao" class="cn.luotoo.system.dao.impl.TestDaoHibernate">
<property name="sessionFactory">
<ref local="sessionFactorySystemdb"/>
</property>
</bean>
<bean id="testService" class="cn.luotoo.system.service.impl.TestServiceImpl">
<property name="testDao">
<ref local="testDao"/>
</property>
</bean>
</beans>