Propagation properties and isolation levels of Spring transactions

1. Propagation (propagation properties of transactions)

 

Propagation : The key property determines which method the proxy should add transactional behavior to. The most important part of such a property is the propagation behavior. The following options are available: PROPAGATION_REQUIRED -- support the current transaction, if there is no current transaction, create a new transaction. This is the most common choice.

PROPAGATION_SUPPORTS --Support the current transaction, if there is no current transaction, it will be executed in a non-transactional manner.

PROPAGATION_MANDATORY --Supports the current transaction, if there is no current transaction, an exception is thrown.

PROPAGATION_REQUIRES_NEW --Create a new transaction, if there is a current transaction, suspend the current transaction.

PROPAGATION_NOT_SUPPORTED --Perform the operation in a non-transactional manner, suspending the current transaction if there is a current transaction.

PROPAGATION_NEVER --Execute in a non-transactional manner, throwing an exception if there is currently a transaction.

 

1PROPAGATION_REQUIRED

   If the transaction currently being executed is not in another transaction, then start a new transaction. For example, the transaction level of ServiceB.methodB is defined as PROPAGATION_REQUIRED, then because ServiceA.methodA has already started a transaction when ServiceA.methodA is executed, At this time, ServiceB.methodB is called , and ServiceB.methodB sees that it has already been running in the transaction of ServiceA.methodA , and will not start a new transaction. And if ServiceA.methodA runs and finds that he is not in a transaction, he will assign himself a transaction. This way, if an exception occurs in ServiceA.methodA or anywhere within ServiceB.methodB , the transaction will be rolled back. Even if the transaction of ServiceB.methodB has been committed, but ServiceA.methodA will fail to be rolled back next , ServiceB.methodB will also be rolled back

 

2PROPAGATION_SUPPORTS

    If it is currently in a transaction, it runs in the form of a transaction, and if it is not currently in a transaction, it runs in a non-transactional form.

 

3PROPAGATION_MANDATORY

    必须在一个事务中运行。也就是说,他只能被一个父事务调用。否则,他就要抛出异常

 

4PROPAGATION_REQUIRES_NEW

    这个就比较绕口了。 比如我们设计ServiceA.methodA的事务级别为PROPAGATION_REQUIREDServiceB.methodB的事务级别为PROPAGATION_REQUIRES_NEW

那么当执行到ServiceB.methodB的时候,ServiceA.methodA所在的事务就会挂起,ServiceB.methodB会起一个新的事务,等待ServiceB.methodB的事务完成以后,他才继续执行。他与PROPAGATION_REQUIRED 的事务区别在于事务的回滚程度了。因为ServiceB.methodB是新起一个事务,那么就是存在两个不同的事务。如果ServiceB.methodB已经提交,那么ServiceA.methodA失败回滚,ServiceB.methodB是不会回滚的。如果ServiceB.methodB失败回滚,如果他抛出的异常被ServiceA.methodA捕获,ServiceA.methodA事务仍然可能提交。

 

5PROPAGATION_NOT_SUPPORTED

当前不支持事务。比如ServiceA.methodA的事务级别是PROPAGATION_REQUIRED ,而ServiceB.methodB的事务级别是PROPAGATION_NOT_SUPPORTED ,那么当执行到ServiceB.methodB时,ServiceA.methodA的事务挂起,而他以非事务的状态运行完,再继续ServiceA.methodA的事务。

 

6PROPAGATION_NEVER

不能在事务中运行。假设ServiceA.methodA的事务级别是PROPAGATION_REQUIRED, 而ServiceB.methodB的事务级别是PROPAGATION_NEVER ,那么ServiceB.methodB就要抛出异常了。

 

7PROPAGATION_NESTED

理解Nested的关键是savepoint。他与PROPAGATION_REQUIRES_NEW的区别是,PROPAGATION_REQUIRES_NEW另起一个事务,将会与他的父事务相互独立,而Nested的事务和他的父事务是相依的,他的提交是要等和他的父事务一块提交的。也就是说,如果父事务最后回滚,他也要回滚的。而Nested事务的好处是他有一个savepoint

 

 

Spring事务的隔离级别

 1. ISOLATION_DEFAULT: 这是一个PlatfromTransactionManager默认的隔离级别,使用数据库默认的事务隔离级别.

      另外四个与JDBC的隔离级别相对应

 2. ISOLATION_READ_UNCOMMITTED: 这是事务最低的隔离级别,它充许令外一个事务可以看到这个事务未提交的数据。

      这种隔离级别会产生脏读,不可重复读和幻像读。

 3. ISOLATION_READ_COMMITTED: 保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据

 4. ISOLATION_REPEATABLE_READ: 这种事务隔离级别可以防止脏读,不可重复读。但是可能出现幻像读。

      它除了保证一个事务不能读取另一个事务未提交的数据外,还保证了避免下面的情况产生(不可重复读)

 5. ISOLATION_SERIALIZABLE 这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。

      除了防止脏读,不可重复读外,还避免了幻像读。

 

什么是脏数据,脏读,不可重复读,幻觉读?

脏读: 指当一个事务正在访问数据,并且对数据进行了修改,而这种修改还没有提交到数据库中,这时,另外一个事务也访问这个数据,然后使用了这个数据。因为这个数据是还没有提交的数据,那么另外一个事务读到的这个数据是脏数据,依据脏数据所做的操作可能是不正确的。

    

不可重复读: 指在一个事务内,多次读同一数据。在这个事务还没有结束时,另外一个事务也访问该同一数据。那么,在第一个事务中的两次读数据之间,由于第二个事务的修改,那么第一个事务两次读到的数据可能是不一样的。这样就发生了在一个事务内两次读到的数据是不一样的,因此称为是不可重复读。

            

幻觉读: 指当事务不是独立执行时发生的一种现象,例如第一个事务对一个表中的数据进行了修改,这种修改涉及到表中的全部数据行。同时,第二个事务也修改这个表中的数据,这种修改是向表中插入一行新数据。那么,以后就会发生操作第一个事务的用户发现表中还有没有修改的数据行,就好象发生了幻觉一样。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326562305&siteId=291194637