论坛首页 Java企业应用论坛

Spring中的事务属性介绍以及声明式事务管理

浏览 3747 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (3)
作者 正文
   发表时间:2009-06-18   最后修改:2009-10-19
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>

       
在<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> 
....



这是基于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);
	}

}



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

<?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>


     
好了,以上就是Spring中的事务属性的介绍,以及Spring中如何以声明方式管理事务的。
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics