spring的几种事物

注解@Transactional内的propagation属性

先看看Propagation这个枚举的源码

/**
 * Enumeration that represents transaction propagation behaviors for use
 * with the {@link Transactional} annotation, corresponding to the
 * {@link TransactionDefinition} interface.
 *
 * @author Colin Sampaleanu
 * @author Juergen Hoeller
 * @since 1.2
 */
public enum Propagation {

	/**
	 * Support a current transaction, create a new one if none exists.
	 * Analogous to EJB transaction attribute of the same name.
	 * <p>This is the default setting of a transaction annotation.
	 */
	REQUIRED(TransactionDefinition.PROPAGATION_REQUIRED),

	/**
	 * Support a current transaction, execute non-transactionally if none exists.
	 * Analogous to EJB transaction attribute of the same name.
	 * <p>Note: For transaction managers with transaction synchronization,
	 * PROPAGATION_SUPPORTS is slightly different from no transaction at all,
	 * as it defines a transaction scope that synchronization will apply for.
	 * As a consequence, the same resources (JDBC Connection, Hibernate Session, etc)
	 * will be shared for the entire specified scope. Note that this depends on
	 * the actual synchronization configuration of the transaction manager.
	 * @see org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization
	 */
	SUPPORTS(TransactionDefinition.PROPAGATION_SUPPORTS),

	/**
	 * Support a current transaction, throw an exception if none exists.
	 * Analogous to EJB transaction attribute of the same name.
	 */
	MANDATORY(TransactionDefinition.PROPAGATION_MANDATORY),

	/**
	 * Create a new transaction, suspend the current transaction if one exists.
	 * Analogous to EJB transaction attribute of the same name.
	 * <p>Note: Actual transaction suspension will not work on out-of-the-box
	 * on all transaction managers. This in particular applies to JtaTransactionManager,
	 * which requires the {@code javax.transaction.TransactionManager} to be
	 * made available it to it (which is server-specific in standard J2EE).
	 * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
	 */
	REQUIRES_NEW(TransactionDefinition.PROPAGATION_REQUIRES_NEW),

	/**
	 * Execute non-transactionally, suspend the current transaction if one exists.
	 * Analogous to EJB transaction attribute of the same name.
	 * <p>Note: Actual transaction suspension will not work on out-of-the-box
	 * on all transaction managers. This in particular applies to JtaTransactionManager,
	 * which requires the {@code javax.transaction.TransactionManager} to be
	 * made available it to it (which is server-specific in standard J2EE).
	 * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
	 */
	NOT_SUPPORTED(TransactionDefinition.PROPAGATION_NOT_SUPPORTED),

	/**
	 * Execute non-transactionally, throw an exception if a transaction exists.
	 * Analogous to EJB transaction attribute of the same name.
	 */
	NEVER(TransactionDefinition.PROPAGATION_NEVER),

	/**
	 * Execute within a nested transaction if a current transaction exists,
	 * behave like PROPAGATION_REQUIRED else. There is no analogous feature in EJB.
	 * <p>Note: Actual creation of a nested transaction will only work on specific
	 * transaction managers. Out of the box, this only applies to the JDBC
	 * DataSourceTransactionManager when working on a JDBC 3.0 driver.
	 * Some JTA providers might support nested transactions as well.
	 * @see org.springframework.jdbc.datasource.DataSourceTransactionManager
	 */
	NESTED(TransactionDefinition.PROPAGATION_NESTED);


	private final int value;


	Propagation(int value) { this.value = value; }

	public int value() { return this.value; }

}



 

1.事务的传播行为

事务的使用过程中,用的最多的传播行为是require,在大部分的mis系统里,可以对整个业务层切一个require的事务就可以满足需要。

但spring提供的不仅如此,对于复杂的业务,Spring也提供了相应的事务传播行为来满足业务需要。

Spring中的传播行为如下:

Require:支持当前事务,如果没有事务,就建一个新的,这是最常见的;

Supports:支持当前事务,如果当前没有事务,就以非事务方式执行;

Mandatory:支持当前事务,如果当前没有事务,就抛出异常;

RequiresNew:新建事务,如果当前存在事务,把当前事务挂起;

NotSupported:以非事务方式执行操作,如果当前存在事务,就把事务挂起;

Never:以非事务方式执行,如果当前存在事务,则抛出异常。

Nested:新建事务,如果当前存在事务,把当前事务挂起。与RequireNew的区别是与父事务相关,且有一个savepoint。

其中,Require、Supports、NotSupported、Never两个看文字也就能了解,就不多说了。而Mandatory是要求所有的操作必须在一个事务里,较Require来说,对事务要求的更加严格。

RequireNew:当一个Require方法A调用RequireNew方法B时,B方法会新new一个事务,并且这个事务和A事务没有关系,也就是说B方法出现异常,不会导致A的回滚,同理当B已提交,A再出现异常,B也不会回滚。

Nested:这个和RequireNew的区别是B方法的事务和A方法的事务是相关的。只有在A事务提交的时候,B事务都会提交。也就是说当A发生异常时,A、B事务都回滚,而当B出现异常时,B回滚,而A回滚到savepoint,如下代码所示:

public void A(){
    //操作1
    //操作2
    //操作3
    try{
        //savepoint
        B();//一个Nested的方法
    } catch{
        //出现异常,B方法回滚,A方法回滚到
        //savepoint,也就是说操作1、2、3 都还在
       C();
    } finally{

    }

}
2.事务的隔离级别
说完了事务传播的行为,现在再说下事务隔离级别,事务隔离级别的出现,是为了使你在性能与数据的有效性之间做一个平衡,不是说级别越高越好,只有合适才是最好的。

事务隔离级别如下:

Serializable:最严格的级别,事务串行执行,资源消耗最大;

Repeatable Read:保证了一个事务不会修改已经由另一个事务读取但未提交(回滚)的数据。

Read Committed:大多数主流数据库的默认事务等级,保证了一个事务不会读到另一个并行事务已经修改但未提交的数据。适用于大多数系统

Read Uncommitted:保证了读取过程中不会读取到非法数据。

想要理解这四个级别,还需要知道三种不讨人喜欢的事情:

dirty reads:脏读,就是说事务A未提交的数据被事务B读走,如果事务A失败回滚,将导致B所读取的数据是错误的。

non-repeatable reads:不可重复读,就是说事务A中两处读取数据,第一次读时是100,然后事务B把值改成了200,事务A再读一次,结果就发现值变了,造成A事务数据混乱。

phantom read:幻读,和不可重复读相似,也是同一个事务中多次读不一致的问题。但是不可重复读的不一致是因为它所要取的数据集被改变了,而幻读所要读的数据不一致却不是他所要读的数据改变,而是它的条件数据集改变。比如:Select id where name="ppgogo*",第一次读去了6个符合条件的id,第二次读时,由于事务B把第一个贴的名字由"dd"改成了“ppgogo9”,结果取出来7个数据。

而事务的隔离级别会导致读取到非法数据的情况如下表示:

  


脏读 不可重复读 幻读
Serializable 不会 不会 不会
Repeatable Read 不会 不会
Read Committed 不会
Read Uncommitted

 

转载于:https://my.oschina.net/bughope/blog/406990

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值