Spring事务的传播特性和隔离级别(持续更新中)


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

传播特性

例子:

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标志的事务。

隔离级别

未提交读

/**
	 * Indicates that dirty reads, non-repeatable reads and phantom reads
	 * can occur.
	 * <p>This level allows a row changed by one transaction to be read by another
	 * transaction before any changes in that row have been committed (a "dirty read").
	 * If any of the changes are rolled back, the second transaction will have
	 * retrieved an invalid row.
	 * @see java.sql.Connection#TRANSACTION_READ_UNCOMMITTED
	 */
	int ISOLATION_READ_UNCOMMITTED = Connection.TRANSACTION_READ_UNCOMMITTED;

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

已提交读

	/**
	 * Indicates that dirty reads are prevented; non-repeatable reads and
	 * phantom reads can occur.
	 * <p>This level only prohibits a transaction from reading a row
	 * with uncommitted changes in it.
	 * @see java.sql.Connection#TRANSACTION_READ_COMMITTED
	 */
	int ISOLATION_READ_COMMITTED = Connection.TRANSACTION_READ_COMMITTED;

可重复读

/**
	 * Indicates that dirty reads and non-repeatable reads are prevented;
	 * phantom reads can occur.
	 * <p>This level prohibits a transaction from reading a row with uncommitted changes
	 * in it, and it also prohibits the situation where one transaction reads a row,
	 * a second transaction alters the row, and the first transaction re-reads the row,
	 * getting different values the second time (a "non-repeatable read").
	 * @see java.sql.Connection#TRANSACTION_REPEATABLE_READ
	 */
	int ISOLATION_REPEATABLE_READ = Connection.TRANSACTION_REPEATABLE_READ;

可序列化

/**
	 * Indicates that dirty reads, non-repeatable reads and phantom reads
	 * are prevented.
	 * <p>This level includes the prohibitions in {@link #ISOLATION_REPEATABLE_READ}
	 * and further prohibits the situation where one transaction reads all rows that
	 * satisfy a {@code WHERE} condition, a second transaction inserts a row
	 * that satisfies that {@code WHERE} condition, and the first transaction
	 * re-reads for the same condition, retrieving the additional "phantom" row
	 * in the second read.
	 * @see java.sql.Connection#TRANSACTION_SERIALIZABLE
	 */
	int ISOLATION_SERIALIZABLE = Connection.TRANSACTION_SERIALIZABLE;
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值