Spring分布式事务

Java代码
分布式事务是指操作多个数据库之间的事务,在tomcat下,是没有分布式事务的,不过可以借助于第三方软件jotm(Java Open Transaction Manager )和AtomikosTransactionsEssentials实现,在spring中分布式事务是通过jta(jotm,atomikos)来进行实现,下面是采用jotm进行实现spring跨库之间的事务  
 
jotm下载地址:http://jotm.ow2.org/xwiki/bin/view/Main/Download_Releases  
 
我采用的是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.jar  
 
applicationContext.xml配置如下:  
 
<?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 ">  
 
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>  
 
<!--定义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> 

你可能感兴趣的:(spring,tomcat,xml,Hibernate)