Spring学习(十五)-事务的其他属性
2017-08-23 16:52
169 查看
事务的属性:
1.使用propagation指定事务的的传播行为,即当前的事务方法被另一个事务方法
调用时如何使用事务,默认取值为REQUIRED,即使用调用方法的事务
REQUIRES_NEW使用自己的事务 调用的事务方法的事务被挂起
2.使用isolation指定事务的隔离级别,最常用的取值为READ_COMMITTED
3.默认情况下Spring的声明式事务对所有的运行时异常进行回滚,也可通过对应的
属性进行设置为readOnly=true
5.使用timeout指定强制回滚之前事务可以占用的时间
1.BookShopServiceImpl实现类
“`
package com.spring.tx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service(“bookShopService”)
public class BookShopServiceImpl implements BookShopService {
}
““
1.使用propagation指定事务的的传播行为,即当前的事务方法被另一个事务方法
调用时如何使用事务,默认取值为REQUIRED,即使用调用方法的事务
REQUIRES_NEW使用自己的事务 调用的事务方法的事务被挂起
2.使用isolation指定事务的隔离级别,最常用的取值为READ_COMMITTED
3.默认情况下Spring的声明式事务对所有的运行时异常进行回滚,也可通过对应的
属性进行设置为readOnly=true
5.使用timeout指定强制回滚之前事务可以占用的时间
1.BookShopServiceImpl实现类
“`
package com.spring.tx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service(“bookShopService”)
public class BookShopServiceImpl implements BookShopService {
@Autowired public BookShopDao bookShopDao; /*添加事务注解 * 1.使用propagation指定事务的的传播行为,即当前的事务方法被另一个事务方法调用时 * 如何使用事务,默认取值为REQUIRED,即使用调用方法的事务 * REQUIRES_NEW使用自己的事务 调用的事务方法的事务被挂起 * 2.使用isolation指定事务的隔离级别,最常用的取值为READ_COMMITTED * 3.默认情况下Spring的声明式事务对所有的运行时异常进行回滚,也可通过对应的属性进行设置 * 4.使用readOnly指定事务是否为可读,表示这个事务只读数据但不更新数据 * 应设置为readOnly=true * 5.使用timeout指定强制回滚之前事务可以占用的时间 * @Transactional(propagation=Propagation.REQUIRES_NEW, isolation=Isolation.READ_COMMITTED, noRollbackFor= {UserAccountException.class}, readOnly=false, timeout=3) */ @Transactional(propagation=Propagation.REQUIRES_NEW, isolation=Isolation.READ_COMMITTED) @Override public void purchase(String username, String isbn) { try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //1.获取书的单价 int price=bookShopDao.findBookPriceByIsbn(isbn); //2.更新书的库存 bookShopDao.updateBookStock(isbn); //3.更新用户余额 bookShopDao.updateUserAccount(username, price); }
}
““
相关文章推荐
- Spring中事务的其他属性(隔离级别、回滚、只读、过期)
- 【Spring学习33】Spring事务(3):事务属性之隔离级别(isolation level)
- 【Spring学习34】Spring事务(4):事务属性之7种传播行为
- spring 事务属性中的传播行为?
- spring事务一 事务属性分析
- spring框架学习(八)spring管理事务方式之注解配置
- 【Spring学习32】Spring事务(2):事务管理器
- Spring.NET教程(十五)——事务传播行为(基础篇)
- Spring 事务属性
- Spring入门学习——bean属性配置(一)
- Spring系列之 (十五):Spring事务管理方式-声明式事务(注解)
- Spring事务属性
- Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别
- Spring事务属性详解
- Spring学习-30:Spring中的事务管理之环境搭建
- Spring学习历程---Hibernate访问数据库可脱离事务管理器
- Spring学习-31:Spring中的事务管理之手动编码的方式
- Spring学习笔记(7)----装配各种集合类型的属性
- applicationContext-common.xml 之spring tx:advice 和 aop:config 配置事务属性详解
- SpringBoot 学习记录(八): properties 属性自定义