Spring nested transactions

前端 未结 5 1293
野趣味
野趣味 2021-02-01 03:05

In my Spring Boot project I have implemented following service method:

@Transactional
public boolean validateBoard(Board board) {
    boolean result = false;
            


        
5条回答
  •  梦毁少年i
    2021-02-01 03:53

    The basic thumb rule in terms of nested Transactions is that they are completely dependent on the underlying database, i.e. support for Nested Transactions and their handling is database dependent and varies with it. In some databases, changes made by the nested transaction are not seen by the 'host' transaction until the nested transaction is committed. This can be achieved using Transaction isolation in @Transactional (isolation = "")

    You need to identify the place in your code from where an exception is thrown, i.e. from the parent method: "validateBoard" or from the child method: "update".

    Your code snippet shows that you are explicitly throwing the exceptions.

    YOU MUST KNOW::

    In its default configuration, Spring Framework’s transaction infrastructure code only marks a transaction for rollback in the case of runtime, unchecked exceptions; that is when the thrown exception is an instance or subclass of RuntimeException.

    But @Transactional never rolls back a transaction for any checked exception.

    Thus, Spring allows you to define

    • Exception for which transaction should be rollbacked
    • Exception for which transaction shouldn't be rollbacked

    Try annotating your child method: update with @Transactional(no-rollback-for="ExceptionName") or your parent method.

提交回复
热议问题