Spring事务的传播特性和隔离级别


Spring4 TransactionDefinition接口中定义了事务的隔离级别和事务的传播特性

事务的传播特性

特性说明
1.PROPAGATION_REQUIRED如果存在一个事务,则支持当前事务。如果没有事务则开启
2. PROPAGATION_SUPPORTS如果存在一个事务,支持当前事务。如果没有事务,则非事务的执行
3. PROPAGATION_MANDATORY如果已经存在一个事务,支持当前事务。如果没有一个活动的事务,则抛出异常。
4. PROPAGATION_REQUIRES_NEW总是开启一个新的事务。如果一个事务已经存在,则将这个存在的事务挂起。
5. PROPAGATION_NOT_SUPPORTED总是非事务地执行,并挂起任何存在的事务
6. PROPAGATION_NEVER总是非事务地执行,如果存在一个活动事务,则抛出异常
7. PROPAGATION_NESTED如果一个活动的事务存在,则运行在一个嵌套的事务中. 如果没有活动事务, 则按TransactionDefinition.PROPAGATION_REQUIRED 属性执行

Propagation

Required需要 如果存在一个事务,则支持当前事务。如果没有事务则开启
Supports支持 如果存在一个事务,支持当前事务。如果没有事务,则非事务的执行
Mandatory必要的 如果已经存在一个事务,支持当前事务。如果没有一个活动的事务,则抛出异常。
required_new总是开启一个新的事务。如果一个事务已经存在,则将这个存在的事务挂起。
Not_support总是非事务地执行,并挂起任何存在的事务。
Never绝不 总是非事务地执行,如果存在一个活动事务,则抛出异常
Nested嵌套的 如果有就嵌套、没有就开启事务

demo

class ClassA{
        method(){
            //逻辑处理1
            classB.methodB();
            //逻辑处理2
        }
    }
 class ClassB{
 		//逻辑处理3
        method();
        //逻辑处理4
    }

1. PROPAGATION_REQUIRED

/**
    * Support a current transaction; create a new one if none exists.
    * Analogous to the EJB transaction attribute of the same name.
    * <p>This is typically the default setting of a transaction definition,
    * and typically defines a transaction synchronization scope.
    */
   int PROPAGATION_REQUIRED = 0;

如果事务不存在则创建,如果存在使用存在的事务

例如:

class ClassA{
	    @Transactional(propagation = Propagation.REQUIRED)
        method(){
            //逻辑处理1
            classB.methodB();
            //逻辑处理2
        }
    }
 class ClassB{
 		//逻辑处理3
 		@Transactional(propagation = Propagation.REQUIRED)
        method();
        //逻辑处理4
    }

classA.method()创建PROPAGATION_REQUIRED 事务后,classB.mehtod()会使用classA.method()创建的事务,不会创建新的事务。如果逻辑处理2失败抛出异常则classB.mehtod()方法会被回滚。当classA.method()没有打上PROPAGATION_REQUIRED 标志,那么如果classB.mehtod()回滚后,classA.method()逻辑处理1的内容不会被回滚。

2. PROPAGATION_SUPPORTS

/**
 * Support a current transaction; execute non-transactionally if none exists.
 * */
int PROPAGATION_SUPPORTS = 1;

如果当前存在事务,则使用存在的事务,如果不存在则使用非事务方式处理。

例如:

class ClassA{
	    @Transactional(propagation = Propagation.REQUIRED)
        method(){
            //逻辑处理1
            classB.methodB();
            //逻辑处理2
        }
    }
 class ClassB{
 		//逻辑处理3
 		@Transactional(propagation = Propagation.SUPPORTS)
        method();
        //逻辑处理4
    }

classA.method()创建PROPAGATION_REQUIRED 事务后,classB.mehtod()会使用classA.method()创建的事务.如果classA.method()没使用PROPAGATION_REQUIRED创建事务,则classB.mehtod()会以非事务方式运行。

3. PROPAGATION_MANDATORY

/**
	 * Support a current transaction; throw an exception if no current transaction
	 * exists. Analogous to the EJB transaction attribute of the same name.
	 * <p>Note that transaction synchronization within a {@code PROPAGATION_MANDATORY}
	 * scope will always be driven by the surrounding transaction.
	 */
	int PROPAGATION_MANDATORY = 2;

如果当前存在事务,则使用存在的事务,如果不存在则抛出异常。
例如:

class ClassA{
	    @Transactional(propagation = Propagation.REQUIRED)
        method(){
            //逻辑处理1
            classB.methodB();
            //逻辑处理2
        }
    }
 class ClassB{
 		//逻辑处理3
 		@Transactional(propagation = Propagation.MANDATORY)
        method();
        //逻辑处理4
    }

classA.method()创建PROPAGATION_REQUIRED 事务后,classB.mehtod()会使用classA.method()创建的事务.如果classA.method()没使用PROPAGATION_REQUIRED创建事务,则classB.mehtod()会以非事务方式运行。则classB.mehtod()会抛出异常。

4. PROPAGATION_REQUIRES_NEW

/**
	 * Create a new transaction, suspending the current transaction if one exists.
	 * Analogous to the EJB transaction attribute of the same name.
	 * <p><b>NOTE:</b> Actual transaction suspension will not work out-of-the-box
	 * on all transaction managers. This in particular applies to
	 * {@link org.springframework.transaction.jta.JtaTransactionManager},
	 * which requires the {@code javax.transaction.TransactionManager} to be
	 * made available it to it (which is server-specific in standard Java EE).
	 * <p>A {@code PROPAGATION_REQUIRES_NEW} scope always defines its own
	 * transaction synchronizations. Existing synchronizations will be suspended
	 * and resumed appropriately.
	 * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
	 */
	int PROPAGATION_REQUIRES_NEW = 3;

永远创建新的事务,如果当前存在事务则挂起
例如:

class ClassA{
	    @Transactional(propagation = Propagation.REQUIRED)
        method(){
            //逻辑处理1
            classB.methodB();
            //逻辑处理2
        }
    }
 class ClassB{
 		//逻辑处理3
 		@Transactional(propagation = Propagation.REQUIRES_NEW)
        method();
        //逻辑处理4
    }

当classB.method()打上PROPAGATION_REQUIRES_NEW的事务标志,执行classA.method()方法,当执行到classB.method()的时候,会检查上下文有没有事务,如果classA.method()有事务,则会挂起calssA.method()的事务,新建一个属于classB.method()的事务,当classB.method()的事务执行结束的时候,则会唤醒classA.method()的事务。PROPAGATION_REQUIRES_NEW和PROPAGATION_REQUIRED的差别在于回滚,当classB.method()的事务提交后,classA.method()执行失败,只会回滚classA.method不会回滚classB.method(),当classB.method()执行失败,异常被classA.methodA()方法 catch到的话,classA.method()事务不会回滚。

5. PROPAGATION_NOT_SUPPORTED

/**
	 * Do not support a current transaction; rather always execute non-transactionally.
	 * Analogous to the EJB transaction attribute of the same name.
	 * <p><b>NOTE:</b> Actual transaction suspension will not work out-of-the-box
	 * on all transaction managers. This in particular applies to
	 * {@link org.springframework.transaction.jta.JtaTransactionManager},
	 * which requires the {@code javax.transaction.TransactionManager} to be
	 * made available it to it (which is server-specific in standard Java EE).
	 * <p>Note that transaction synchronization is <i>not</i> available within a
	 * {@code PROPAGATION_NOT_SUPPORTED} scope. Existing synchronizations
	 * will be suspended and resumed appropriately.
	 * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
	 */
	int PROPAGATION_NOT_SUPPORTED = 4;

永远使用非事务方式执行,如果当前存在事务则挂起
例如:

class ClassA{
	    @Transactional(propagation = Propagation.REQUIRED)
        method(){
            //逻辑处理1
            classB.methodB();
            //逻辑处理2
        }
    }
 class ClassB{
 		//逻辑处理3
 		@Transactional(propagation = Propagation.NOT_SUPPORTED)
        method();
        //逻辑处理4
    }

当执行到classB.method()方法的时候,检查上下文中存在事务,则挂起classA的事务执行,classB.method()方法执行完成后,唤醒classA.method()的事务。

6. PROPAGATION_NEVER

/**
    * Do not support a current transaction; throw an exception if a current transaction
    * exists. Analogous to the EJB transaction attribute of the same name.
    * <p>Note that transaction synchronization is <i>not</i> available within a
    * {@code PROPAGATION_NEVER} scope.
    */
   int PROPAGATION_NEVER = 5;

不支持事务执行方式,如果存在事务则抛出异常
例如:

class ClassA{
	    @Transactional(propagation = Propagation.REQUIRED)
        method(){
            //逻辑处理1
            classB.methodB();
            //逻辑处理2
        }
    }
 class ClassB{
 		//逻辑处理3
 		@Transactional(propagation = Propagation.NEVER)
        method();
        //逻辑处理4
    }

classA.method() 打上了PROPAGATION_REQUIRED标志,创建了事务,当执行到classB.method()方法时,检查到存在活动的事务,则抛出异常。

7. PROPAGATION_NESTED

	/**
	 * Execute within a nested transaction if a current transaction exists,
	 * behave like {@link #PROPAGATION_REQUIRED} otherwise. There is no
	 * analogous feature in EJB.
	 * <p><b>NOTE:</b> Actual creation of a nested transaction will only work on
	 * specific transaction managers. Out of the box, this only applies to the JDBC
	 * {@link org.springframework.jdbc.datasource.DataSourceTransactionManager}
	 * when working on a JDBC 3.0 driver. Some JTA providers might support
	 * nested transactions as well.
	 * @see org.springframework.jdbc.datasource.DataSourceTransactionManager
	 */
	int PROPAGATION_NESTED = 6;

如果存在一个活动的事务,则运行在一个嵌套的事务中. 如果没有活动事务, 安按照PROPAGATION_REQUIRED 事务属性执行
例如:

class ClassA{
	    @Transactional(propagation = Propagation.REQUIRED)
        method(){
            //逻辑处理1
            classB.methodB();
            //逻辑处理2
        }
    }
 class ClassB{
 		//逻辑处理3
 		@Transactional(propagation = Propagation.NESTED)
        method();
        //逻辑处理4
    }

当classB.method()打上PROPAGATION_NESTED的事务标志后,开始执行classA.method()方法,当执行到classB.method()的时候,此时classA.method()方法有事务,会用当前事务,如果 classB.method()执行失败,只会回滚 classB.method(),不会回滚classA.method()。只有当classA.method()执行完成后才会提交classB.method()的事务,如果classA.method()方法没有事务,classB.method()就会新建一个事务,类似打PROPAGATION_REQUIRED标志的事务。

事务的隔离级别

级别说明
ISOLATION_DEFAULT这是一个PlatfromTransactionManager默认的隔离级别,使用数据库默认的事务隔离级别
另外四个与JDBC的隔离级别相对应
ISOLATION_READ_UNCOMMITTED这是事务最低的隔离级别,它充许令外一个事务可以看到这个事务未提交的数据。
ISOLATION_READ_COMMITTED保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据
ISOLATION_REPEATABLE_READ这种事务隔离级别可以防止脏读,不可重复读。但是可能出现幻像读它除了保证一个事务不能读取另一个事务未提交的数据外,还保证了避免下面的情况产生(不可重复读)。
ISOLATION_SERIALIZABLE这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。除了防止脏读,不可重复读外,还避免了幻像读。

未提交读级别容许A事务读取B事务未提交的数据,如果B事务进行了回滚,则A进行了脏读
例子:月末发工资5000RMB,你开启事务给你老婆打款5000RMB,当你还没提交事务的时候,你老婆查询自己的银行卡,发现余额多了5000块,你老婆心里美美的,想着晚上等你回来好好伺候你,但是你这时想留点私房钱,于是进行了事务回滚,修改2000RMB金额后进行了事务提交。最后你老婆收到的打款是2000RMB,这样子就知道你留了私房钱,心里会生气,你回家就被跪搓衣板。

版权声明:本文为博主原创文章,转载请附上博文链接!

其中的一些概念的说明:

脏读: 指当一个事务正在访问数据,并且对数据进行了修改,而这种修改还没有提交到数据库中,这时,另外一个事务也访问这个数据,然后使用了这个数据。因为这个数据是还没有提交的数据, 那么另外一 个事务读到的这个数据是脏数据,依据脏数据所做的操作可能是不正确的。
不可重复读: 指在一个事务内,多次读同一数据。在这个事务还没有结束时,另外一个事务也访问该同一数据。 那么,在第一个事务中的两次读数据之间,由于第二个事务的修改,那么第一个事务两次读到的数据可能是不一样的。这样就发生了在一个事务内两次读到的数据是不一样的,因此称为是不可重复读。
幻觉读: 指当事务不是独立执行时发生的一种现象,例如第一个事务对一个表中的数据进行了修改,这种修改涉及 到表中的全部数据行。同时,第二个事务也修改这个表中的数据,这种修改是向表中插入一行新数据。那么,以后就会发生操作第一个事务的用户发现表中还有没有修改的数据行,就好象发生了幻觉一样。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值