Spring JTA事务配置JOTM

来源:互联网 发布:广电网络倒闭 编辑:程序博客网 时间:2024/05/29 13:20

JTA可以在多个数据库上使用一个事务,weblogic等应用服务器提供了jta数据源的支持,可以直接被使用。但是tomcat本身并不支持这种特性。如果想在tomcat上使用jta就必须使用其它的工具。jotm就是一个独立的可以提供JTA功能的组件。


<?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:context="http://www.springframework.org/schema/context"        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/context http://www.springframework.org/schema/context/spring-context-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">        <description>springJTA</description>        <!--指定Spring配置中用到的属性文件        <bean id="propertyConfig"                class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">            <property name="locations">                <list>                    <value>classpath:jdbc.properties</value>                </list>            </property>        </bean>           -->    <!-- JOTM实例 -->      <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean">            <property name="defaultTimeout" value="500000"/>      </bean>        <!-- JTA事务管理器 -->      <bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">              <property name="userTransaction" ref="jotm" />          </bean>        <!-- 数据源A -->        <bean id="dataSourceA" 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/emp_mvc"/>               </bean>           </property>           <property name="user" value="root"/>           <property name="password" value="123456"/>        </bean>          <!-- 数据源B -->        <bean id="dataSourceB" 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/emp_mvc2"/>               </bean>           </property>           <property name="user" value="root"/>           <property name="password" value="123456"/>        </bean>         <bean id = "sessionFactoryA"            class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">         <property name = "dataSource" ref="dataSourceA"/>         <property name="mappingResources">             <list>                          <value>com/ouku/JOTM/entity/Emp.hbm.xml</value>             </list>         </property>        </bean>             <bean id = "sessionFactoryB"             class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">            <property name = "dataSource" ref="dataSourceB"/>         <property name="mappingResources">             <list>                          <value>com/ouku/JOTM/entity/Emp2.hbm.xml</value>             </list>         </property>        </bean>             <!-- 事务切面配置 -->        <aop:config>            <aop:pointcut id="pointCut"                  expression="execution(* com.ouku.JOTM..*.*(..))"/><!-- 包及其子包下的所有方法 -->          <aop:advisor pointcut-ref="pointCut" advice-ref="txAdvice"/>                      <aop:advisor pointcut="execution(* *..ouku.JOTM.*.*(..))" advice-ref="txAdvice"/>      </aop:config>          <!-- 通知配置 -->        <tx:advice id="txAdvice" transaction-manager="jtaTransactionManager">           <tx:attributes>              <tx:method name="delete*" rollback-for="Exception"/>              <tx:method name="save*" rollback-for="Exception"/>              <tx:method name="update*" rollback-for="Exception"/>              <tx:method name="find*" read-only="true" rollback-for="Exception"/>            <tx:method name="*" read-only="true" rollback-for="Exception"/>           </tx:attributes>        </tx:advice>          <bean id="genericDao"                class="com.ouku.JOTM.DAO.GenericDaoImpl" autowire="byName">             <property name="sessionFactoryA" ref="sessionFactoryA"></property>             <property name="sessionFactoryB" ref="sessionFactoryB"></property>     </bean>        <bean id="userService"                class="com.ouku.JOTM.biz.UserServiceImpl" autowire="byName">      </bean>    </beans>





本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1744796

0 0