`
fengfeng925
  • 浏览: 105020 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

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

<?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中如何以声明方式管理事务的。
分享到:
评论

相关推荐

    Spring中的四种声明式事务的配置

    本文系统的介绍了Spring中的四种声明式事务的配置。可应用于实践项目中。

    Spring Boot多数据源(支持Spring声明式事务切换和回滚).pdf

    1. 基于Aspectj实现动态数据源...6. 实现事务内切换数据源(支持原生Spring声明式事务哟,仅此一家),并支持多数据源事务回滚(有了它除了跨服务的事务你需要考虑分布式事务,其他都不需要,极大的减少了系统的复杂程度)

    Spring的编程式事务和声明式事务详解

    主要介绍了Spring的编程式事务和声明式事务的相关内容,涉及Spring事务属性、Spring事务的基本类等知识,需要的朋友可以了解下。

    主题:详解spring事务属性.doc

    我们在使用Spring声明式事务时,有一个非常重要的概念就是事务属性。事务属性通常由事务的传播行为,事务的隔离级别,事务的超时值和事务只读标志组成。我们在进行事务划分时,需要进行事务定义,也就是配置事务的...

    Spring中文帮助文档

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 事务传播 9.5.8. 通知...

    Spring-Reference_zh_CN(Spring中文参考手册)

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.6.1. @Transactional 有关的设置 ...

    spring.net中文手册在线版

    14.5.1.理解Spring.NET声明式事务管理的实现 14.5.2.第一个例子 14.5.3.Transaction特性的设置 14.5.4.通过AutoProxyCreator使用声明式事务 14.5.5.通过TransactionProxyFactoryObject使用声明式事务 14.5.6. 通过...

    spring4示例代码

    spring-1 演示了使用setter方式及构造器方式创建bean,util:list标签创建集合,p标签简化配置 和依赖注入, 以及bean的autowire和继承与依赖,以及bean的作用域...spring-5 演示了声明式事务及使用xml配置文件处理事务

    Spring 2.0 开发参考手册

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 插入事务操作 9.5.8. ...

    尚硅谷佟刚Spring4代码及PPT.rar

    JdbcDaoSupport、使用 NamedParameterJdbcTemplate、Spring 的声明式事务、事务的属性(传播行为、隔离级别、回滚属性、只读属性、过期时间)、使用 XML 文件的方式配置事务、整合 Hibernate、整合 Struts2 等。

    spring chm文档

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 插入事务操作 9.5.8. ...

    spring.doc

    5.1.8声明式事务管理 116 5.1.8.1Spring的事务管理器 117 5.1.8.2Spring事务的传播属性 117 5.1.8.3Spring事务的隔离级别 117 拓展: 118 5.1.8.4以XML配置的 形式 119 拓展: 120 5.1.8.5以注解方式配置 125 拓展:...

    Spring API

    9.5.1. 理解Spring的声明式事务管理实现 9.5.2. 第一个例子 9.5.3. 回滚 9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.7. 事务传播 9.5.8. 通知...

    spring in action英文版

     5.3 声明式事务  5.3.1 理解事务属性  5.3.2 声明一个简单的事务策略  5.4 通过方法名声明事务  5.4.1 使用NameMatchTransactionAttributeSource  5.4.2 名称匹配事务的捷径  5.5 用元数据声明...

    Spring攻略(第二版 中文高清版).part1

    13.6 管理集成测试中的事务 530 13.6.1 问题 530 13.6.2 解决方案 530 13.6.3 工作原理 531 13.7 在集成测试中访问数据库 536 13.7.1 问题 536 13.7.2 解决方案 536 13.7.3 工作原理 537 13.8 使用...

    Spring攻略(第二版 中文高清版).part2

    13.6 管理集成测试中的事务 530 13.6.1 问题 530 13.6.2 解决方案 530 13.6.3 工作原理 531 13.7 在集成测试中访问数据库 536 13.7.1 问题 536 13.7.2 解决方案 536 13.7.3 工作原理 537 13.8 使用...

    Spring.3.x企业应用开发实战(完整版).part2

    9.6 使用注解配置声明式事务 9.6.1 使用@Transactional注解 9.6.2 通过AspectJ LTW引入事务切面 9.7 集成特定的应用服务器 9.7.1 BEA WebLogic 9.7.2 BEA WebLogic 9.8 小结 第10章 Spring的事务管理难点剖析 10.1 ...

    Spring in Action(第二版 中文高清版).part2

    6.4 声明式事务 6.4.1 定义事务参数 6.4.2 代理事务 6.4.3 在Spring 2.0里声明事务 6.4.4 定义注释驱动事务 6.5 小结 第7章 保护Spring 7.1 Spring Security介绍 7.2 验证用户身份 7.2.1 配置Provider ...

    Spring in Action(第二版 中文高清版).part1

    6.4 声明式事务 6.4.1 定义事务参数 6.4.2 代理事务 6.4.3 在Spring 2.0里声明事务 6.4.4 定义注释驱动事务 6.5 小结 第7章 保护Spring 7.1 Spring Security介绍 7.2 验证用户身份 7.2.1 配置Provider ...

    spring最小版所需包

    Spring提供了一个用标准Java语言编写的AOP框架,它给POJOs提供了声明式的事务管理和其他企业事务--如果你需要--还能实现你自己的aspects。这个框架足够强大,使得应用程序能够抛开EJB的复杂性,同时享受着和传统EJB...

Global site tag (gtag.js) - Google Analytics