Spring中的事务管理主要有编程式事务管理和声明式事务管理,这里主要介绍声明式事务管理。在Spring中,主要是通过AOP来完成声明式的事务管理。要了解Spring对事务的支持,首先要了解事务的属性。在Spring中事务属性主要分以下几方面:
1、传播行为(Propagation behavior)
可以找到相对应的常数与说明,列出下列几个:
PROPAGATION_MANDATORY:方法必须在一个现存的事务中进行,否则丢出异常
PROPAGATION_NESTED:在一个嵌入的事务中进行
PROPAGATION_NEVER:不应在事务中进行,如果有则丢异常
PROPAGATION_NOT_SUPPORTED:不应再事务中进行,如果有就暂停现存的事务
PROPAGATION_REQUIRED:支持现在的事务,如果没有就建立一个新的事务
PROPAGATION_REQUIRES_NEW:建立一个新的事务,如果现存一个事务就暂停它
PROPAGATION_SUPPORTS:支持现在的事务,如果没有就以非事务的方式执行
2、隔离层级(Isolation level)
在一个应用程序中,可能有多个事务在同时进行,这些事务应当彼此之间互不知道另一个事务的存在,比如现在整个应用程序就只有一个事务存在,由于事务彼此之间独立,若读取的是同一个数据的话,就容易发生问题,比如:
Dirty read(脏读):某个事务已经更新了一份数据,另一份事务在此时读取了同一份数据,由于某些原因,前一个事务回滚了,则后一个事务读取的数据则是错误的。
Non-repeatable read(非重复读):在一个事务的两次查询中事务不一致,可能是因为两次查询过程中间插入了一个事务更新的原有数据。
Phantom read(幻象读):在一个事务的两次查询中数据笔数不一致。
解决以上问题的方法之一,就是在某个事务进行过程中锁定正在更新或查询的数据,但是这样会造成效率上的问题,别的事务必须等待当前事务解锁后才能进行。然而,根据需求的不同,并不用在事务进行时完全的锁定数据,隔离层级可以让您根据实际的需求,对数据的锁定进行设置。一下是几个隔离层级的参数说明:
ISOLATION_DEFAULT:使用底层数据库预设的隔离层级
ISOLATION_READ_COMMITTED:运行事务读取其他事务已经提交的数据字段,可以防止脏读问题
ISOLATION_READ_UNCOMMITTED:运行事务读取其他并行事务还没有提交的数据,会发生脏读、非重复读、幻象读等问题
ISOLATION_REPEATABLE_READ:要求多次读取的数据必须相同,除非事务本身更新数据,可以防止脏读、非重复读等问题
ISOLATION_SERIALIZABLE:完整的隔离层级,防止所有问题,会锁定数据对应的表,有效率问题
事实上,对于事务的传播特性,可以设置对应的隔离层级。在Spring中,我们用的最多的就是PROPAGATIOIN_REQUIRED这种传播行为。这个意思是,如果应用程序中已经存在一个事务了,当另一个事务进来时,会加入到这个事务中,如果没有事务存在,则开启一个新的事务。
请看一下对事务传播特性设置的配置文件
- <?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/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">
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation">
- <value>classpath:hibernate.cfg.xml</value>
- </property>
- </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="insert*" propagation="REQUIRED" />
- <tx:method name="del*" propagation="REQUIRED"/>
- <tx:method name="update*" propagation="REQUIRED"/>
- <tx:method name="*" read-only="true"/>
- </tx:attributes>
- </tx:advice>
- <aop:config>
- <aop:pointcut id="allDaoMethod" expression="execution (* org.whatisjava.dao..*.*(..))"/>
- <aop:advisor advice-ref="txAdvice" pointcut-ref="allDaoMethod"/>
- </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: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/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"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </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="insert*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="allDaoMethod" expression="execution (* org.whatisjava.dao..*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="allDaoMethod"/> </aop:config> </beans>
在<tx:method>中的属性设置,对于传播行为、隔离层级、只读、超时等,都有对应的"propagation"、"isolation"、"timeout"、"read-only"等等,这里设置的传播属性是"REQUIRED",则它对应的默认的隔离层级就是"DEFAULT","timeout"默是"-1","read-only"默认是"false"。我们也可以根据这些参数来选取不同的参数设置,比如
- ...
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="insert*" propagation="REQUIRED" isolation="SERIALIZABLE"/>
- <tx:method name="del*" propagation="REQUIRED" isolation="SERIALIZABLE"/>
- <tx:method name="update*" propagation="REQUIRED" isolation="SERIALIZABLE"/>
- <tx:method name="*" read-only="true"/>
- </tx:attributes>
- </tx:advice>
- ....
... <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED" isolation="SERIALIZABLE"/> <tx:method name="del*" propagation="REQUIRED" isolation="SERIALIZABLE"/> <tx:method name="update*" propagation="REQUIRED" isolation="SERIALIZABLE"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> ....
这是基于xml配置文件进行的事务属性的传播控制,也可以基于注解方式的,请看如下代码
- package org.whatisjava.dao.impl;
- import java.util.List;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import org.springframework.transaction.annotation.Propagation;
- import org.springframework.transaction.annotation.Transactional;
- import org.whatisjava.dao.UserDao;
- import org.whatisjava.po.User;
- public class UserDaoBean extends HibernateDaoSupport implements UserDao {
- @Transactional(propagation = Propagation.REQUIRED)
- public void delUser(Integer id) {
- getHibernateTemplate().delete((User)getHibernateTemplate().get(User.class, id));
- }
- @Transactional(readOnly = true)
- public User findUser(Integer id) {
- return (User)getHibernateTemplate().get(User.class, id);
- }
- @Transactional(propagation = Propagation.REQUIRED)
- public void insertUser(User user) {
- getHibernateTemplate().save(user);
- }
- @Transactional(readOnly = true)
- public List listUser() {
- return getHibernateTemplate().find("from User user");
- }
- @Transactional(propagation = Propagation.REQUIRED)
- public void updateUser(User user) {
- getHibernateTemplate().update(user);
- }
- }
package org.whatisjava.dao.impl; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.whatisjava.dao.UserDao; import org.whatisjava.po.User; public class UserDaoBean extends HibernateDaoSupport implements UserDao { @Transactional(propagation = Propagation.REQUIRED) public void delUser(Integer id) { getHibernateTemplate().delete((User)getHibernateTemplate().get(User.class, id)); } @Transactional(readOnly = true) public User findUser(Integer id) { return (User)getHibernateTemplate().get(User.class, id); } @Transactional(propagation = Propagation.REQUIRED) public void insertUser(User user) { getHibernateTemplate().save(user); } @Transactional(readOnly = true) public List listUser() { return getHibernateTemplate().find("from User user"); } @Transactional(propagation = Propagation.REQUIRED) public void updateUser(User user) { getHibernateTemplate().update(user); } }
要想让这些注解配置生效,必须在配置文件里加入最后一行
- <?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/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">
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation">
- <value>classpath:hibernate.cfg.xml</value>
- </property>
- </bean>
- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <tx:annotation-driven transaction-manager="transactionManager"/>
- </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: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/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"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
转载:http://www.iteye.com/topic/410461
好了,以上就是Spring中的事务属性的介绍,以及Spring中如何以声明方式管理事务的。
相关推荐
Spring事务原理和配置 Spring事务原理是指Spring框架中的一种机制,用于管理事务,并提供了多种配置方式。事务是指一系列的操作,作为一个整体执行,如果其中某个操作失败,整个事务将回滚。Spring事务原理围绕着两...
spring事务与数据库操作
Spring事务管理Demo
Spring事务流程图时序图Spring事务流程图时序图Spring事务流程图时序图Spring事务流程图时序图
Spring事务操作示例(四种方式),包含完整代码和数据库文件(基于MySQL,在项目sql文件夹中),可运行,学习Spring事务详见博客:http://blog.csdn.net/daijin888888/article/details/51822257
spring事务配置详解 spring事务配置详解
spring 事务spring 事务spring 事务spring 事务spring 事务
spring 事务传播 demo
spring事务源码解析
spring 事务传播与隔离级别DEMO 脏读、重复读、幻影读 spring 事务传播与隔离级别DEMO 脏读、重复读、幻影读
Spring事务管理.pdf 1.资料 2.本地事务与分布式事务 3.编程式模型 4.宣告式模型
此ppt中前半部分通过spring事务的60道题的测试,摸底对事务的掌握情况,后半部分,对spring中的事务属性(传播行为、隔离级别、回滚规则、事务超时、是否只读)进行说明
本文档介绍了spring事务隔离级别的使用及详解
包含spring事务管理案例的项目源码和说明文档
Synchronized锁在Spring事务管理下,导致线程不安全。
spring事务管理几种方式代码实例:涉及编程式事务,声明式事务之拦截器代理方式、AOP切面通知方式、AspectJ注解方式,通过不同方式实例代码展现,总结spring事务管理的一般规律,从宏观上加深理解spring事务管理特性...
spring事务的底层实现流程图 spring事务的底层实现流程图 spring事务的底层实现流程图 spring事务的底层实现流程图 spring事务的底层实现流程图 spring事务的底层实现流程图 spring事务的底层实现流程图 spring事务...
Spring事务管理中所需要的jar包是 spring-tx-3.2.0.RELEASE.jar
最详细的Spring 事务讲解 让你彻底明白
讲述spring事务机制,aopafter,aopbefore,aopmethodintercept,4个例子