[Turn] @Transactional reason annotation does not take effect

1. Check your methods are not public. @Transactional annotation method can only be applied to public visibility, if used in the method protected, private or package visibility, not an error, but the transaction settings will not work.

2. Check your exception types are not unchecked exception. By default, Spring will be unchecked exceptions transaction rollback, if a checked exception is rolled back. The null pointer exception, the arithmetic exception and the like, will be rolled back; file read and write, network problems, spring would not be able to roll back up. If you want to check abnormal rollback also how to do, notes stated above exception types can be:

@Transactional(rollbackFor = Exception.class)

There are types of norollbackFor, custom does not roll back abnormal.

3, whether the try ... catch in service in operation, due to the abnormal has been captured, the story works will not be rolled back. If you have to try ... catch in service in abnormal, they want to roll back the transaction, can run in the catch block throws an exception:

try{
    ....  
}catch(Exception e){
    logger.error("",e);
    throw new RuntimeException;
}

Such a method is inadequate, the return clause is not present in the catch block, if you want to roll back the transaction exception is captured, while the return message, can use manual rollback:

try{
    ...
}catch(Exception e){
    logger.error("",e);
    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    return ERROR_MESSAGE;
}

PS: Stated otherwise, the controller layer to capture the abnormal service layer, the transaction will be rolled back yet? The answer is yes, as long as you service layer throw an exception, and you add transactions can handle the exception, which is in line with this rollbackFor = Exception.class you throw exceptions, no matter the outside there is no capture can be rolled back.

4, whether to open the parsing of annotations:

<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
          id="transactionManager">
        <property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

5, the database engine to support the transaction, if it is mysql, pay attention to the table to use engine support services, such as innodb, if it is myisam, the transaction does not work.

6, spring whether to scan to you this package, the following is scanned org.test the following package:

<context:component-scan base-package="org.test" ></context:component-scan>

7, the check is not in the same class method calls (such as A method @Transactional no comment, calling the method a @Transactional annotation), so that the transaction is not in force. The reason may refer to the following article:

  https://blog.csdn.net/levae1024/article/details/82998386

  https://blog.csdn.net/gx_hxl/article/details/80808088

  https://blog.csdn.net/m0_38027656/article/details/84190949

 

reference:

" The @Transactional reason not to roll back in force ."

" SpringMVC in the controller layer to capture the abnormal service layer, the transaction will be rolled back yet ."

Guess you like

Origin www.cnblogs.com/codingmengmeng/p/12111392.html