Spring学习总结9(基于Hibernate的事务管理)
2011-04-17 17:45
786 查看
基于注解形式的事务管理
XML配置
代码示例
类名上也可以写事务注解,但是优先级低于方法上的事务注解
@Transactional 注解的属性
事务传播特性详解:
1.required
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/18/43ad2b59fa7e9204438199171cf2ec81.gif)
如果在执行该方法之前,已经打开了一个事务,会将当前方法加入到之前的事务之中。如果没有,则自己新开一个事务。
2.requiredsNew
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/18/682566394f85d628c61e931acf03e38a.gif)
如果在执行该方法之前,已经打开了一个事务,会挂起之前的事务,自己新开一个事务。当自己的事务执行完之后,再进行之前的事务。
3.mandatory
在调用方法之前,必须已经打开了一个事务,否则将会抛出异常
4.nested
如果在执行方法前,已经打开了一个事务,则再开一个新事务内嵌在之前的事务之中。
nested与required的区别:nested注解的方法,如果事务回滚不会影响到之前的事务
nested与requiredsNew的区别:nested注解的方法,如果之前的事务,在nested已经执行完之后,事务进行了回滚,也会让nested里的事务进行回滚
5.never
如果在执行该方法之前,已经打开了一个事务,则抛出异常
6.not_supported
注解not_supported的方法不支持事务。如果在执行该方法之前,已经打开了一个事务,将之前的事务挂起,执行完该方法之后,在继续进行之前的事务
7.supported
如果在执行该方法之前,已经打开了一个事务,支持之前的事务,如果没有打开事务,则也不打开新的事务。即与调用supported注解的方法的事务保持一致,它有我就有,它无我就无
基于XML的事务管理(常用)
将上面XML中的<tx:annotation-driven transaction-manager="txManager"/>给去掉,增加如下的配置
XML配置
<?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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context-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"> <context:annotation-config /> <context:component-scan base-package="com.bjsxt" /> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="annotatedClasses"> <list> <value>com.bjsxt.model.User</value> <value>com.bjsxt.model.Log</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <!-- 声明事务的管理bean -> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 指明事务由annotation来驱动 --> <tx:annotation-driven transaction-manager="txManager"/> </beans>
代码示例
类名上也可以写事务注解,但是优先级低于方法上的事务注解
@Transactional(readOnly = true) public class DefaultFooService implements FooService { @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW) public void updateFoo(Foo foo) { // do something } }
@Transactional 注解的属性
属性 | 类型 | 描述 |
---|---|---|
propagation | 枚举型:Propagation | 可选的传播性设置 默认为required |
isolation | 枚举型:Isolation | 可选的隔离性级别(默认值:ISOLATION_DEFAULT) |
readOnly | 布尔型 | 读写型事务 只读型事务(当查询时设为只读会比不设要快) |
timeout | int型(以秒为单位) | 事务超时 |
rollbackFor | 一组 Class 类的实例,必须是Throwable 的子类 | 一组异常类,遇到时 必须 进行回滚。默认情况下checked exceptions不进行回滚,仅unchecked exceptions(即RuntimeException的子类)才进行事务回滚。 |
rollbackForClassname | 一组 Class 类的名字,必须是Throwable的子类 | 一组异常类名,遇到时 必须 进行回滚 |
noRollbackFor | 一组 Class 类的实例,必须是Throwable 的子类 | 一组异常类,遇到时 必须不 回滚。 |
noRollbackForClassname | 一组 Class 类的名字,必须是Throwable 的子类 | 一组异常类,遇到时 必须不 回滚 |
1.required
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/18/43ad2b59fa7e9204438199171cf2ec81.gif)
如果在执行该方法之前,已经打开了一个事务,会将当前方法加入到之前的事务之中。如果没有,则自己新开一个事务。
2.requiredsNew
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/18/682566394f85d628c61e931acf03e38a.gif)
如果在执行该方法之前,已经打开了一个事务,会挂起之前的事务,自己新开一个事务。当自己的事务执行完之后,再进行之前的事务。
3.mandatory
在调用方法之前,必须已经打开了一个事务,否则将会抛出异常
4.nested
如果在执行方法前,已经打开了一个事务,则再开一个新事务内嵌在之前的事务之中。
nested与required的区别:nested注解的方法,如果事务回滚不会影响到之前的事务
nested与requiredsNew的区别:nested注解的方法,如果之前的事务,在nested已经执行完之后,事务进行了回滚,也会让nested里的事务进行回滚
5.never
如果在执行该方法之前,已经打开了一个事务,则抛出异常
6.not_supported
注解not_supported的方法不支持事务。如果在执行该方法之前,已经打开了一个事务,将之前的事务挂起,执行完该方法之后,在继续进行之前的事务
7.supported
如果在执行该方法之前,已经打开了一个事务,支持之前的事务,如果没有打开事务,则也不打开新的事务。即与调用supported注解的方法的事务保持一致,它有我就有,它无我就无
基于XML的事务管理(常用)
将上面XML中的<tx:annotation-driven transaction-manager="txManager"/>给去掉,增加如下的配置
<aop:config> <aop:pointcut id="bussinessService" expression="execution(public * com.bjsxt.service..*.*(..))" /> <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="getUser" read-only="true" /> <tx:method name="add*" propagation="REQUIRED"/> </tx:attributes> </tx:advice>
相关文章推荐
- Spring学习-34:Spring中的事务管理之声明式事务(基于注解)
- Spring学习历程---Hibernate访问数据库可脱离事务管理器
- spring 事务管理的学习总结
- SpringMVC学习系列(12) 完结篇 之 基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现
- SpringMVC学习系列(12) 完结篇 之 基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现
- SpringMVC学习系列(12) 完结篇 之 基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现
- SpringMVC学习系列之 基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现
- SpringMVC学习系列(11) 完结篇 之 基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之五:Hibernate的事务管理、手动回滚
- spring学习总结(十二):spring 中的事务管理
- Spring学习-32:Spring中的事务管理之声明式事务(原始方式,基于TransactionProxyBean)
- SpringMVC学习系列(12) 完结篇 之 基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现
- 关于spring管理事务的学习总结。
- SpringMVC学习系列(12) 完结篇 之 基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现
- Java学习之道:纯Hibernate使用-不用spring事务管理
- SpringMVC学习系列(12) 完结篇 之 基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现
- Spring学习-33:Spring中的事务管理之声明式事务(基于切面自动代理)
- 学习笔记:spring与hibernate整合(采用aop来管理事务来实现声明式事务)
- SpringMVC学习系列(12) 完结篇 之 基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现
- SpringMVC学习(12):基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现