Spring整合Hibernate 二 - 声明式的事务管理
Spring大战Hibernate之声明式的事务管理
Spring配置文件:
添加事务管理类的bean:
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
该类由Spring提供,如果是hibernate4那就用"org.springframework.orm.hibernate4.HibernateTransactionManager"(Spring2不支持hibernate4)。
把sessionFactory注入给该bean的sessionFactory属性
配置事务的通知:
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="getUser" read-only="true" />
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
transaction-manager="txManager":这个事务通知所用的事务管理bean是刚才所定义的管理bean。
为名为 getUse 的方法添加事务。该事务是只读的(read-only="true")。
名字以 add、save 开头的方法添加事务。该事务传播特性为REQUIRED(propagation="REQUIRED")。
事务通知的属性解释:
一 propagation:事务的传播特性(支持程度),默认为REQUIRED
1.REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
2.SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。
3.MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。
4.REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。
5.NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
6.NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。
7.NESTED:支持当前事务,如果当前事务存在,则执行一个嵌套事务,如果当前没有事务,就新建一个事务。
二 isolation:事务的隔离级别
1. ISOLATION_DEFAULT:使用数据库默认的事务隔离级别。
2. ISOLATION_READ_UNCOMMITTED:未提交读。这是事务最低的隔离级别,它充许别外一个事务可以看到这个事务未提交的数据。这种隔离级别会产生脏读,不可重复读和幻像读。
3. ISOLATION_READ_COMMITTED :提交读。保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据。这种事务隔离级别可以避免脏读出现,但是可能会出现不可重复读和幻像读。
4. ISOLATION_REPEATABLE_READ :重复读。这种事务隔离级别可以防止脏读,不可重复读。但是可能出现幻像读。
5. ISOLATION_SERIALIZABLE:存列化。这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。除了防止脏读,不可重复读外,还避免了幻读。
三 timeout:超时的时间(秒) -1:不限制
四 rollback-for:指定需要回滚的的异常类型,默认是针对unchecked exception回滚
添加事务的切入点
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(public * com.startspring.service..*.*(..))" />
<aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice" />
</aop:config>
指定切面为事务通知 txAdvice。指定切入点为txPointcut。
---------------------------------------------------------------------------------------------------------------
要使用<tx:advice> <aop:config> 等标签,需要先添加tx、aop命名空间,如下:
<?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">
-----------------------------------------------------------------------------------------------------------------
这时候可以把相应方法里事务开起和提交的语句去掉,让Spring来管理事务。如:
public void save(Student student) {
Session session = sessionFactory.getCurrentSession();
//session.beginTransaction();
session.save(student);
//session.getTransaction().commit();
}
把session.beginTransaction();和session.getTransaction().commit();去掉。如果运作不报错则事务配置成功。
注意!!必须把hibernate的 hibernate.current_session_context_class 这项属性去掉!否则事务是不起效的!!也就是hibernate配置文件的<property name="current_session_context_class">thread</property> 或者Spring配置文件的 <prop key="hibernate.current_session_context_class">thread</prop>
Spring整合Hibernate 二 - 声明式的事务管理的更多相关文章
- Spring第13篇—–Spring整合Hibernate之声明式事务管理
不容置疑的我们可以知道Spring的事务管理是通过AOP(AOP把我们的事务管理织入到我们的业务逻辑里面了)的方式来实现的,因为事务方面的代码与spring的绑定并以一种样板式结构使用.(面向切面编程 ...
- 8.Spring整合Hibernate_2_声明式的事务管理(Annotation的方式)
声明式的事务管理(AOP的主要用途之一) (Annotation的方式) 1.加入annotation.xsd 2.加入txManager bean 3.<tx:annotation-drive ...
- 9.Spring整合Hibernate_2_声明式的事务管理(Xml的方式)
使用xml的方式进行声明式的事务管理 推荐使用xml的方式,因为可以同时为多个方法进行声明 <!-- 开启Spring中的事务管理(声明式的事务管理) xml--> <!-- 不管是 ...
- Spring整合Hibernate:2、使用Annotation方式进行声明式的事务管理
1.加入DataSourceTransactionManager的命名空间 修改applicationContext.xml文件,增加如下内容: 1 2 3 4 5 6 7 8 9 10 11 12 ...
- spring声明式的事务管理
spring支持声明式事务管理和编程式事务管理两种方式. 编程式事务使用TransactionTemplate来定义,可在代码级别对事务进行定义. 声明式事务基于aop来实现,缺点是其最细粒度的事务声 ...
- Spring整合hibernate:3、使用XML进行声明式的事务管理
配置applicationContext.xml文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2 ...
- Spring整合Hibernate:1、annotation方式管理SessionFactory
1.在applicationContext.xml文件中初始化SessionFactory(annotation方式) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 ...
- Spring整合Hibernate的步骤
为什么要整合Hibernate?1.使用Spring的IOC功能管理SessionFactory对象 LocalSessionFactoryBean2.使用Spring管理Session对象 Hib ...
- Spring学习笔记:声明式事务管理增删改查业务
一.关于是事务 以方法为单位,进行事务控制:抛出异常,事务回滚. 最小的执行单位为方法.决定执行成败是通过是否抛出异常来判断的,抛出异常即执行失败 二.声明式事务: 声明式事务(declarative ...
随机推荐
- sql复制表数据的方法
第一种: INSERT INTO dbo.Student1 ( uid, username ) select uid,username FROM dbo.Student2 备注:Student1表是必 ...
- Git入门——基础知识问答
问题一:为什么要选择Git作为Android开发的版本控制工具? 答:1)git是android项目和社区的统一语言. 2)高通版本发布频繁,需要与平台及时同步,快速re ...
- U盘制作Linux系统盘
一.首先需要有一个U盘,最好能是8G及以上的 二.下载Linux的镜像文件(我这边安装的是red hat6.3) 下载路径:http://pan.baidu.com/s/1jIGYCKI 三.下载制作 ...
- PureLayout(轻量级自动布局)
直接整理用法 1.设置高度宽度 [view1 autoSetDimension:ALDimensionHeight toSize:70.0]; [view1 autoSetDimension:ALDi ...
- 两个string数组对应比较
最近做的array string类型对比.这个可能比较复杂,用的是linq 是请教别人的,我在这里记录一下 jquery 方法里面的数组 function arrtxt() { var arrt= [ ...
- connectionStrings基本配置
常用connectionStrings配置: <connectionStrings> <add name="LocalSqlServer" ...
- 在head标签里加一个meta标签让指定ie使用特定内核 解决css在ie中的兼容性问题
<meta http-equiv="x-ua-compatible" content="IE=edge, chrome=1"/> IE=edge: ...
- 数据类型MSVC和gcc/g++的不同
前言: 在16位环境下,int/unsigned int 占16位,long/unsigned long占32位 在32位环境下,int占32位,unsigned int占16位,long/unsig ...
- Why Functional Programming Matters
http://hi.baidu.com/lhurricane/item/35b57e12a1e3c5ddbf9042a7 http://blog.csdn.net/ddwn/article/detai ...
- jquery中使用offset()获得的div的left=0,top=0
写东西的时候要获取div的left和top,但怎么也取不到值都为0,但在chrome的console下是可以取到值的, 瞬间就纳闷了,于是乎就在网上找各种方法,大家一般的问题可能都是要获取的div被隐 ...