`

spring事务

阅读更多

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这种传播行为。这个意思是,如果应用程序中已经存在一个事务了,当另一个事务进来时,会加入到这个事务中,如果没有事务存在,则开启一个新的事务。

请看一下对事务传播特性设置的配置文件

Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.         xmlns:aop="http://www.springframework.org/schema/aop"  
  6.         xmlns:tx="http://www.springframework.org/schema/tx"  
  7.         xsi:schemaLocation="   
  8.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  9.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
  10.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">   
  11.   
  12.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  13.         <property name="configLocation">   
  14.             <value>classpath:hibernate.cfg.xml</value>   
  15.         </property>      
  16.     </bean>   
  17.        
  18.     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">   
  19.         <property name="sessionFactory" ref="sessionFactory" />   
  20.     </bean>   
  21.        
  22.     <tx:advice id="txAdvice" transaction-manager="transactionManager">   
  23.         <tx:attributes>   
  24.             <tx:method name="insert*" propagation="REQUIRED" />   
  25.             <tx:method name="del*" propagation="REQUIRED"/>   
  26.             <tx:method name="update*" propagation="REQUIRED"/>   
  27.             <tx:method name="*" read-only="true"/>   
  28.         </tx:attributes>   
  29.     </tx:advice>    
  30.        
  31.     <aop:config>   
  32.         <aop:pointcut id="allDaoMethod" expression="execution (* org.whatisjava.dao..*.*(..))"/>   
  33.         <aop:advisor advice-ref="txAdvice" pointcut-ref="allDaoMethod"/>   
  34.     </aop:config>   
  35.     </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"。我们也可以根据这些参数来选取不同的参数设置,比如

Java代码 复制代码
  1. ...   
  2. <tx:advice id="txAdvice" transaction-manager="transactionManager">   
  3.         <tx:attributes>   
  4.             <tx:method name="insert*" propagation="REQUIRED" isolation="SERIALIZABLE"/>   
  5.             <tx:method name="del*" propagation="REQUIRED" isolation="SERIALIZABLE"/>   
  6.             <tx:method name="update*" propagation="REQUIRED" isolation="SERIALIZABLE"/>   
  7.             <tx:method name="*" read-only="true"/>   
  8.         </tx:attributes>   
  9.     </tx:advice>    
  10. ....  
...
<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配置文件进行的事务属性的传播控制,也可以基于注解方式的,请看如下代码

Java代码 复制代码
  1. package org.whatisjava.dao.impl;   
  2.   
  3. import java.util.List;   
  4.   
  5. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;   
  6. import org.springframework.transaction.annotation.Propagation;   
  7. import org.springframework.transaction.annotation.Transactional;   
  8. import org.whatisjava.dao.UserDao;   
  9. import org.whatisjava.po.User;   
  10.   
  11. public class UserDaoBean extends HibernateDaoSupport implements UserDao {   
  12.   
  13.     @Transactional(propagation = Propagation.REQUIRED)   
  14.     public void delUser(Integer id) {   
  15.         getHibernateTemplate().delete((User)getHibernateTemplate().get(User.class, id));   
  16.     }   
  17.   
  18.     @Transactional(readOnly = true)   
  19.     public User findUser(Integer id) {   
  20.         return (User)getHibernateTemplate().get(User.class, id);   
  21.     }   
  22.   
  23.     @Transactional(propagation = Propagation.REQUIRED)   
  24.     public void insertUser(User user) {   
  25.         getHibernateTemplate().save(user);   
  26.     }   
  27.   
  28.     @Transactional(readOnly = true)   
  29.     public List listUser() {   
  30.         return getHibernateTemplate().find("from User user");   
  31.     }   
  32.   
  33.     @Transactional(propagation = Propagation.REQUIRED)   
  34.     public void updateUser(User user) {   
  35.         getHibernateTemplate().update(user);   
  36.     }   
  37.   
  38. }  
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);
	}

}



要想让这些注解配置生效,必须在配置文件里加入最后一行

Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.         xmlns:aop="http://www.springframework.org/schema/aop"  
  6.         xmlns:tx="http://www.springframework.org/schema/tx"  
  7.         xsi:schemaLocation="   
  8.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  9.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
  10.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">   
  11.   
  12.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  13.         <property name="configLocation">   
  14.             <value>classpath:hibernate.cfg.xml</value>   
  15.         </property>      
  16.     </bean>   
  17.        
  18.     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">   
  19.         <property name="sessionFactory" ref="sessionFactory" />   
  20.     </bean>   
  21.        
  22. <tx:annotation-driven transaction-manager="transactionManager"/>   
  23. </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中如何以声明方式管理事务的。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics