Spring JTA事务配置JOTM

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: JTA可以在多个数据库上使用一个事务,weblogic等应用服务器提供了jta数据源的支持,可以直接被使用。但是tomcat本身并不支持这种特性。如果想在tomcat上使用jta就必须使用其它的工具。jotm就是一个独立的可以提供JTA功能的组件。<?xml version="1.0" encoding="UTF-8"?>    <beans xmlns="http

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

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
12天前
|
SQL Java 数据库连接
(自用)Spring常用配置
(自用)Spring常用配置
15 0
|
1月前
|
监控 Java 数据处理
【Spring云原生】Spring Batch:海量数据高并发任务处理!数据处理纵享新丝滑!事务管理机制+并行处理+实例应用讲解
【Spring云原生】Spring Batch:海量数据高并发任务处理!数据处理纵享新丝滑!事务管理机制+并行处理+实例应用讲解
|
1月前
|
Java 数据库 Spring
Spring事务失效的场景详解
Spring事务失效的场景详解
31 0
|
1月前
|
Java 数据库 Spring
Spring事务的传播机制(行为、特性)
Spring事务的传播机制(行为、特性)
36 0
|
1月前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
41 1
|
2月前
|
Java 关系型数据库 数据库连接
Spring源码解析--深入Spring事务原理
本文将带领大家领略Spring事务的风采,Spring事务是我们在日常开发中经常会遇到的,也是各种大小面试中的高频题,希望通过本文,能让大家对Spring事务有个深入的了解,无论开发还是面试,都不会让Spring事务成为拦路虎。
35 1
|
5天前
|
存储 安全 Java
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
15 0
|
5天前
|
安全 Java 数据库
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(上)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)
28 0
|
6天前
|
安全 Java Spring
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
18 0
|
6天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
24 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置