Development of transaction management using _ side story

 

1) The default transaction management

  SpringData provides a default transaction mode so that all queries were declared as read-only transactions. Single request process ensures consistency of data.

For custom method, the transaction need to change the default way SpringData provided, can comment @Transactional statement Repository operations on multiple methods, they should be treated in the same transaction, in accordance with the layered architecture of thought, which part belongs to the business logic layer, therefore, call for the need to achieve more in the Service Repository layer, and a transaction statement on the appropriate method.

Repository concept: According to the original author of the presentation, which is the interface between a data link layer and the mapping domain, the domain acts as a collection of objects in memory. Client object to query some entities are combined, and submit them to the Repository. Object Repository can be removed from or added, like the objects in a line Collection object data manipulation, while layer may map the code corresponding to the respective data extracted from the database.

Conceptually, Repository data is a data storage area to the object and into a collection package provides a set of these operations.

We often say that can be understood as a broad sense of DAO

 

2) manually manage affairs

1) dao level code

 

Description: @Modifying comment

① in @Query annotations, written JPQL achieve DELETE and UPDATE operations, you must add @modifying comment to notify SpringData This is an UPDATE or DELETE operation.

②UPDATE or DELETE operations require the use of transaction, need to define the Service layer, operating on the method of adding the transaction layer Service.

③ Note JPQL does not support INSERT operations. 

2) service level code

Use @Transactional manually open transaction management

@Transactional annotation support 9 setting properties, of which more use of three attributes: readOnly, propagation, isolation, rollbackFor:

  • Propagation : to enumerate propagation behavior matters;

No assignment alone, then take the default value: Propagation.REQUIRED, support the current transaction, if no transaction, we create a new business.

即:@Transactional(propagation = Propagation.REQUIRED)

  • Isolation : set the transaction isolation level; the development of default.

Control transaction isolation level. Same as the default database with default isolation level, the default database is Read Committed

即:@Transactional(Isolation = READ_COMMITTED)

 

  • readOnly : read and write transaction control, because springdata has been enabled by default, developers can be ignored.
  • rollbackFor : development must be used, the overall anomalies, data rollback, ensure data consistency.

即:@Transactional(rollbackFor = Throwable.class)

@Transactional source

 

3) table

1. spring seven transaction propagation properties:

(1) PROPAGATION_REQUIRED - support the current transaction, if no transaction, we create a new business. This is the most common choice.

(2) PROPAGATION_SUPPORTS - support the current transaction, if no transaction is executed in a non-transactional way.

(3) PROPAGATION_MANDATORY - support the current transaction, if no transaction, throw an exception.

(4) PROPAGATION_REQUIRES_NEW - New Transaction, if the current transaction exists, the current transaction pending.

(5) PROPAGATION_NOT_SUPPORTED - performing operations in a non-transactional manner, if a current transaction is present, leave the current pending transaction.

(6)PROPAGATION_NEVER – 以非事务方式执行,如果当前存在事务,则抛出异常。

(7)PROPAGATION_NESTED – 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。

备注:常用的两个事务传播属性是1和4,即PROPAGATION_REQUIRED,PROPAGATION_REQUIRES_NEW

2. 五个隔离级别:

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

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

(2) ISOLATION_READ_UNCOMMITTED 
这是事务最低的隔离级别,它充许别外一个事务可以看到这个事务未提交的数据。 
这种隔离级别会产生脏读,不可重复读和幻像读。

(3) ISOLATION_READ_COMMITTED 
保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据。 
这种事务隔离级别可以避免脏读出现,但是可能会出现不可重复读和幻像读。

(4) ISOLATION_REPEATABLE_READ 
这种事务隔离级别可以防止脏读,不可重复读。但是可能出现幻像读。 
它除了保证一个事务不能读取另一个事务未提交的数据外,还保证了避免下面的情况产生(不可重复读)。

(5) ISOLATION_SERIALIZABLE 
这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。 
除了防止脏读,不可重复读外,还避免了幻像读。

 

 

Guess you like

Origin blog.csdn.net/jiahao1186/article/details/90779011