Spring事务传播机制和数据库隔离级别
Spring事务传播机制和数据库隔离级别
先看下Spring的 事务传播行为类型
|
事务传播行为类型 |
说明 |
|
PROPAGATION_REQUIRED |
如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是 最常见的选择。 |
|
PROPAGATION_SUPPORTS |
支持当前事务,如果当前没有事务,就以非事务方式执行。 |
|
PROPAGATION_MANDATORY |
使用当前的事务,如果当前没有事务,就抛出异常。 |
|
PROPAGATION_REQUIRES_NEW |
新建事务,如果当前存在事务,把当前事务挂起。 |
|
PROPAGATION_NOT_SUPPORTED |
以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。 |
|
PROPAGATION_NEVER |
以非事务方式执行,如果当前存在事务,则抛出异常。 |
|
PROPAGATION_NESTED |
如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与 PROPAGATION_REQUIRED 类似的操作。 |
当使用 PROPAGATION_NESTED 时, 底层的数据源必须基于 JDBC 3.0 ,并且实现者需要支持保存点事务机制。
readOnly
事务属性中的readOnly标志表示对应的事务应该被最优化为只读事务。这 是一个最优化提示 。在一些情况下,一些事务策略能够起到显著的最优化效果,例如在使用Object/Relational映射工具 (如:Hibernate或TopLink)时避免dirty checking(试图“刷新”)。
- /**
- * PropertyEditor for TransactionAttribute objects. Takes Strings of form
- * <p><code>PROPAGATION_NAME,ISOLATION_NAME,readOnly,timeout_NNNN,+Exception1,-Exception2</code>
- * <p>where only propagation code is required. For example:
- * <p><code>PROPAGATION_MANDATORY,ISOLATION_DEFAULT</code>
- *
- * <p>The tokens can be in <strong>any</strong> order. Propagation and isolation codes
- * must use the names of the constants in the TransactionDefinition class. Timeout values
- * are in seconds. If no timeout is specified, the transaction manager will apply a default
- * timeout specific to the particular transaction manager.
- *
- * <p>A "+" before an exception name substring indicates that
- * transactions should commit even if this exception is thrown;
- * a "-" that they should roll back.
- *
- * @author Rod Johnson
- * @author Juergen Hoeller
- * @since 24.04.2003
- * @see org.springframework.transaction.TransactionDefinition
- * @see org.springframework.core.Constants
- */
- public class TransactionAttributeEditor extends PropertyEditorSupport {
- /**
- * Format is PROPAGATION_NAME,ISOLATION_NAME,readOnly,timeout_NNNN,+Exception1,-Exception2.
- * Null or the empty string means that the method is non transactional.
- * @see java.beans.PropertyEditor#setAsText(java.lang.String)
- */
- public void setAsText(String s) throws IllegalArgumentException {
- if (s == null || "".equals(s)) {
- setValue(null);
- }
- else {
- // tokenize it with ","
- String[] tokens = StringUtils.commaDelimitedListToStringArray(s);
- RuleBasedTransactionAttribute attr = new RuleBasedTransactionAttribute();
- for (int i = 0; i < tokens.length; i++) {
- String token = tokens[i].trim();
- if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_PROPAGATION)) {
- attr.setPropagationBehaviorName(token);
- }
- else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_ISOLATION)) {
- attr.setIsolationLevelName(token);
- }
- else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_TIMEOUT)) {
- String value = token.substring(DefaultTransactionAttribute.PREFIX_TIMEOUT.length());
- attr.setTimeout(Integer.parseInt(value));
- }
- else if (token.equals(RuleBasedTransactionAttribute.READ_ONLY_MARKER)) {
- attr.setReadOnly(true);
- }
- else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_COMMIT_RULE)) {
- attr.getRollbackRules().add(new NoRollbackRuleAttribute(token.substring(1)));
- }
- else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_ROLLBACK_RULE)) {
- attr.getRollbackRules().add(new RollbackRuleAttribute(token.substring(1)));
- }
- else {
- throw new IllegalArgumentException("Illegal transaction attribute token: [" + token + "]");
- }
- }
- setValue(attr);
- }
- }
- }
* <p><code>PROPAGATION_NAME,ISOLATION_NAME,readOnly,timeout_NNNN,+Exception1,-Exception2</code>
* <p>where only propagation code is required. For example:
* <p><code>PROPAGATION_MANDATORY,ISOLATION_DEFAULT</code>
- <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
- <!-- 事务拦截器bean需要依赖注入一个事务管理器 -->
- <property name="transactionManager" ref="transactionManager"/>
- <property name="transactionAttributes">
- <!-- 下面定义事务传播属性-->
- <props>
- <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="*">PROPAGATION_REQUIRED,timeout_11</prop>
- </props>
- </property>
- </bean>
1. 更新丢失(Lost update): 两个事务都同时更新一行数据但是第二个事务却中途失败退出导致对数据两个修改都失效了这是系统没有执 行任何锁操作因此并发事务并没有被隔离开来。
隔离级别 更新丢失 脏读取 重复读取 幻读
未授权读取 N Y Y Y
授权读取 N N Y Y
可重复 读取 N N N Y
串行 N N N N
Spring事务传播机制和数据库隔离级别的更多相关文章
- 理解 spring 事务传播行为与数据隔离级别
事务,是为了保障逻辑处理的原子性.一致性.隔离性.永久性. 通过事务控制,可以避免因为逻辑处理失败而导致产生脏数据等等一系列的问题. 事务有两个重要特性: 事务的传播行为 数据隔离级别 1.事务传播行 ...
- Spring 事务传播机制和数据库的事务隔离级别
Propagation(事务传播属性) 类别 传播类型 说明 支持当前事务 REQUIRED 如果当前没有事务,就新建一个事务.@Transaction的默认选择 支持当前事务 SUPPORTS 就以 ...
- Spring事务传播机制与隔离级别(转)
Spring事务传播机制与隔离级别 博客分类: Spring 转自:http://blog.csdn.net/edward0830ly/article/details/7569954 (写的不错) ...
- spring事务传播机制实例讲解
http://kingj.iteye.com/blog/1680350 spring事务传播机制实例讲解 博客分类: spring java历险 天温习spring的事务处理机制,总结 ...
- 面试突击87:说一下 Spring 事务传播机制?
Spring 事务传播机制是指,包含多个事务的方法在相互调用时,事务是如何在这些方法间传播的. 既然是"事务传播",所以事务的数量应该在两个或两个以上,Spring 事务传播机制的 ...
- spring 事务传播机制
spring 事务 传播机制 描述的 事务方法直接相互调用,父子事物开启,挂起,回滚 等的处理方式. 绿色的 那几个 我认为比较重要. 1 , @Transactional(propagation=P ...
- 数据库事务的四大特性以及事务的隔离级别-与-Spring事务传播机制&隔离级别
数据库事务的四大特性以及事务的隔离级别 本篇讲诉数据库中事务的四大特性(ACID),并且将会详细地说明事务的隔离级别. 如果一个数据库声称支持事务的操作,那么该数据库必须要具备以下四个特性: ⑴ ...
- spring事务传播机制的测试结果
/** * @Component是个一般性的注解,使用此注解修饰的POJO类,有value属性,指定bean的id.也可不写.默认值是类名首字母小写 * @Resource是控制依赖注 ...
- Spring事务传播机制
Spring在TransactionDefinition接口中规定了7种类型的事务传播行为,它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播,即协调已经有事务标识的方法之间的发生调用时的事务 ...
随机推荐
- Mac下VirtualBox共享文件夹设置
环境:CentOS7.2最小化安装 步骤: 先安装必要软件包 yum install -y gcc gcc-devel gcc-c++ gcc-c++-devel make kernel kernel ...
- windows转mac-开发环境搭建(一):需要搭建的环境及安装的工具
作为一个java后端开发者来说,随着项目的增加,前段时间用windows真是受尽折磨,电脑卡到不行,在我们开发部技术大佬的一再安利之下,狠下心选了个17年13寸带touch bar的MacBook P ...
- SpringBoot中关于Mybatis使用的三个问题
SpringBoot中关于Mybatis使用的三个问题 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8495453.html 原本是要讲讲PostgreSQL ...
- the c programing language 学习过程2
manipulated 操纵 notations符号 hexadecimal十六进制 precision精度 be concatenated at 把····联系起来 enumerations枚举 ...
- Java文件及文件夹的创建与删除
功能 这个实例实现了在D盘创建一个文件和文件夹,并删除它们. 函数介绍 createNewFile():当文件不存在时,根据绝对路径创建该文件. delete():删除文件或者文件夹. ...
- Centos搭建mysql/Hadoop/Hive/Hbase/Sqoop/Pig
目录: 准备工作 Centos安装 mysql Centos安装Hadoop Centos安装hive JDBC远程连接Hive Hbase和hive整合 Centos安装Hbase 准备工作: 配置 ...
- sdl的缩放问题
SDL是一种既是开源的,也是跨平台的多媒体开发包,在各种平台上应用很广,经常和FFMPEG等解码器同时使用.对于在windows mobile等缺乏通用播放器的平台来说,是一种很好的选择.网上很多代码 ...
- HI3531uboot开机画面
startvo 0 36 13; startgx 0 0x88000000 1600 0 0 800 600; //startgx 0 0x88000000 2048 0 0 1024 768; se ...
- 【linux】mysql安装问题 g++: not found
问题现象: ../depcomp: line 512: exec: g++: not foundmake[2]: *** [my_new.o] Error 127make[2]: Leaving di ...
- HighCharts之2D带Label的折线图
HighCharts之2D带Label的折线图 1.HighCharts之2D带Label的折线图源码 LineLabel.html: <!DOCTYPE html> <html&g ...