Eight reasons why Spring transactions do not take effect

1. Throwing a checked exception cannot roll back correctly

Reason: Spring will only roll unchecked exceptions
Solution: @Transational plus attribute rollbackFor(Exception.Class)

2. The try catch in the business method leads to the inability to roll back

Reason: The declarative transaction gets the proxy object, and the internal exception is not thrown and the external cannot receive
it. Solution: 1. Throw an exception 2. Manually throw TransationalStatus.setRollbackOnly()

3. The order of Aop slices will not roll

Reason: The aspect transaction has the lowest priority, but the custom aspect is in its inner layer
Solution: 1. Throw an exception upwards on the aspect 2. Manually throw an exception TransationalStatus.setRollbackOnly() 3. Configure @Order for loading and sorting

4. @Transational only works on public methods

Reason: @Transational is only valid for public methods
Solution: 1. Use publick to decorate 2. Configure the transaction bean object to make it valid for all methods

5. The scanning range of the child container package in the parent-child container is too large

Reason: The sub-container scan package range is too large
Solution: 1. Scan each package within its own scope 2. Do not use parent-child containers

6. Calling this class of methods causes transaction propagation to fail

Reason: The method of this class cannot be enhanced without the proxy class
Solution: 1. Inject yourself into this class 2. Use AopContext to obtain the proxy object for operation 3. Through CTW, LTW

7. @Transational does not guarantee atomic behavior

Reason: Atomic operations only include insert, update, delete, select...for update, and select does not guarantee the atomicity of transactions.
Solution: Use select..for update to check the result only when the first select transaction submits the second one

8、@Transational方法导致synchronized失效

原因:多线程情况下synchronized仅仅保证目标方法的原子性,环绕目标方法的还有commit操作并不在sync块内
解决方案:1、synchronized范围扩大到代理对象 2、使用select....for update

Guess you like

Origin juejin.im/post/7080520338798444557