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>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章