spring分布式事务

强烈鄙视 只问问题就会啥笑的 技术总监  今天晚上遇到好人了大笑

 

 

 

分布式事务是指操作多个数据库之间的事务,在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>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值