Spring事务SPI及配置介绍


版权声明:本文为博主原创文章,未经博主允许不得转载。
Spring事务管理的抽象,核心的三个接口:PlatformTransactionManager、TransactionDefinition和TransactionStatus。关系如下图所示:
TransactionDefinition:定义了Spring兼容的事务属性,包含:事务隔离级别、事务传播行为、超时时长、只读状态;
TransactionStatus:代表一个事务的具体运行状态。事务管理器通过该接口得到事务运行的状态信息,也可以通过它回滚事务等;
PlatformTransactionManager:事务管理器的顶级接口,它的常用实现类如下图所示:

Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。
DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactory,TransactionManager的实现为HibernateTransactionManager。
根据代理机制的不同,总结了五种Spring事务的配置方式,配置文件如下:
第一种方式:每个Bean都有一个代理
- <?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"
- 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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation" value="classpath:hibernate.cfg.xml" />
- <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
- </bean>
- <!-- 定义事务管理器(声明式的事务) -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <!-- 配置DAO -->
- <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <bean id="userDao"
- class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
- <!-- 配置事务管理器 -->
- <property name="transactionManager" ref="transactionManager" />
- <property name="target" ref="userDaoTarget" />
- <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />
- <!-- 配置事务属性 -->
- <property name="transactionAttributes">
- <props>
- <prop key="*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
- </beans>
第二种方式:所有Bean共享一个代理基类
- <?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"
- 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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation" value="classpath:hibernate.cfg.xml" />
- <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
- </bean>
- <!-- 定义事务管理器(声明式的事务) -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <bean id="transactionBase"
- class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
- lazy-init="true" abstract="true">
- <!-- 配置事务管理器 -->
- <property name="transactionManager" ref="transactionManager" />
- <!-- 配置事务属性 -->
- <property name="transactionAttributes">
- <props>
- <prop key="*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
- <!-- 配置DAO -->
- <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <bean id="userDao" parent="transactionBase" >
- <property name="target" ref="userDaoTarget" />
- </bean>
- </beans>
第三种方式:使用拦截器
- <?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"
- 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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation" value="classpath:hibernate.cfg.xml" />
- <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
- </bean>
- <!-- 定义事务管理器(声明式的事务) -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <bean id="transactionInterceptor"
- class="org.springframework.transaction.interceptor.TransactionInterceptor">
- <property name="transactionManager" ref="transactionManager" />
- <!-- 配置事务属性 -->
- <property name="transactionAttributes">
- <props>
- <prop key="*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
- <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <property name="beanNames">
- <list>
- <value>*Dao</value>
- </list>
- </property>
- <property name="interceptorNames">
- <list>
- <value>transactionInterceptor</value>
- </list>
- </property>
- </bean>
- <!-- 配置DAO -->
- <bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- </beans>
第四种方式:使用tx标签配置的拦截器
- <?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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <context:annotation-config />
- <context:component-scan base-package="com.bluesky" />
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation" value="classpath:hibernate.cfg.xml" />
- <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
- </bean>
- <!-- 定义事务管理器(声明式的事务) -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="*" propagation="REQUIRED" />
- </tx:attributes>
- </tx:advice>
- <aop:config>
- <aop:pointcut id="interceptorPointCuts"
- expression="execution(* com.bluesky.spring.dao.*.*(..))" />
- <aop:advisor advice-ref="txAdvice"
- pointcut-ref="interceptorPointCuts" />
- </aop:config>
- </beans>
第五种方式:全注解
- <?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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <context:annotation-config />
- <context:component-scan base-package="com.bluesky" />
- <tx:annotation-driven transaction-manager="transactionManager"/>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation" value="classpath:hibernate.cfg.xml" />
- <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
- </bean>
- <!-- 定义事务管理器(声明式的事务) -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- </beans>
此时在DAO上需加上@Transactional注解,如下:
- @Transactional
- @Component("userDao")
- public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
- public List<User> listUsers() {
- return this.getSession().createQuery("from User").list();
- }
- }
配置参考:http://www.blogjava.net/robbie/archive/2009/04/05/264003.html
Spring事务SPI及配置介绍的更多相关文章
- Spring声明式事务管理与配置介绍
转至:http://java.9sssd.com/javafw/art/1215 [摘要]本文介绍Spring声明式事务管理与配置,包括Spring声明式事务配置的五种方式.事务的传播属性(Propa ...
- Spring事务解析1-使用介绍
spring的事务控制让我们从复杂的事务处理中得到解脱,是我们再也不需要去处理获得连接,关闭连接,事务提交和回滚等操作,再也不需要在事务相关的方法中处理大量的try..catch...finally代 ...
- spring 事务隔离级别配置
声明式的事务处理中,要配置一个切面,即一组方法,如 其中就用到了propagation,表示打算对这些方法怎么使用事务,是用还是不用,其中propagation有七种配置,REQUIRED.SUPPO ...
- Spring事务管理的配置
spring-datasource-config.xml配置事务 <bean id="txManager" class="org.springframework.j ...
- Spring事务传播及数据库事务操作
从Spring 事务配置说起 先看看Spring 事务的基础配置 <aop:aspectj-autoproxy proxy-target-class="true"/> ...
- Spring 事务管理原理探究
此处先粘贴出Spring事务需要的配置内容: 1.Spring事务管理器的配置文件: 2.一个普通的JPA框架(此处是mybatis)的配置文件: <bean id="sqlSessi ...
- 【面试】Spring事务面试考点吐血整理(建议珍藏)
Spring和事务的关系 关系型数据库.某些消息队列等产品或中间件称为事务性资源,因为它们本身支持事务,也能够处理事务. Spring很显然不是事务性资源,但是它可以管理事务性资源,所以Spring和 ...
- Spring事务回滚和异常类
1.异常的一些基本知识 异常的架构 异常的继承结构:Throwable为基类,Error和Exception继承Throwable.Error和RuntimeException及其子类成为未检查异常( ...
- Spring 事务相关点整理
Spring和事务的关系 关系型数据库.某些消息队列等产品或中间件称为事务性资源,因为它们本身支持事务,也能够处理事务. Spring很显然不是事务性资源,但是它可以管理事务性资源,所以Spring和 ...
随机推荐
- js享元模式
享元模式(Flyweight),运用共享技术有效地支持大量细粒度的对象. 内部状态与外部状态 内部状态存储于对象内部. 内部状态可以被一些对象共享 内部状态独立于具体的场景,通常不会改变 外部状态取决 ...
- Mac下git安装(使用Xcode)
(不使用Xcode的出门右转百度其他方法:比如直接安装git软件包.) 一.AppStore 最安全途径:搜索下载Xcode,(需要AppleID). 其他:直接百度Xcode下载. 二.Xcode ...
- uva 10453 dp/LCS变形
https://vjudge.net/problem/UVA-10453 给出一个字符串,问最少添加几个字符使其变为回文串,并输出任意一种答案.就是一个类似于LCS的题目,而且简化了一下,只会出现三种 ...
- lr中检查点的使用web_find()和web_reg_find()的区别
web_find()和web_reg_find()的区别:1. 这两个函数函数类型不同,web_find()是普通函数,web_reg_find()是注册函数;2. VU run time设置中的 “ ...
- BEC translation exercise 1
U.S. oil drillers have made major efficiency improvements with a speed that has repeatedly surprised ...
- _Meta 部分用法
model.UserInfo._meta.app_label #获取该类所在app的app名称 model.UserInfo._meta.model_name #获取该类对应表名(字符串类型) mod ...
- vector map迭代器失效解决方案
vector : iter = container.erase(iter); //erase的返回值是删除元素下一个元素的迭代器 vector<int>::iterator it = ...
- Express+Mongoose(MongoDB)+Vue2全栈微信商城项目全记录(一)
最近用vue2做了一个微信商城项目,因为做的比较仓促,所以一边写一下整个流程,一边稍做优化. 项目github地址:https://github.com/seven9115/vue-fullstack ...
- storm 学习教程
转自:http://blog.csdn.net/hrn1216/article/details/51538962 翻译太累了,再也不想去翻译了,真的太累了: 在这个教程中, 你将学到如何创建一个Sto ...
- OpenJudge9278:旅行
总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 131072kB 描述 转眼毕业了,曾经朝夕相处的同学们不得不都各奔东西,大家都去了不同的城市开始新的生活.在各自城 ...