Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the method of separate rollback of Spring transaction for loop

Shulou Source: shulou.com Published: 2022-06-01 23:58:03 10月10日 Update

This article mainly explains "what is the method of separate rollback of Spring transaction for loop". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the method of separate rollback of Spring transaction for loop".

@ Transactional (rollbackFor = Exception.class) public void aaa (List toDoIds) {for (String toDoId: toDoIds) {try {/ / inner layer new transaction, single transaction within for loop commit or roll back sissCommonService.aaa (toDoId);} catch (Exception e) {e.printStackTrace ();}

In the second generation code, the original method aaa () is called with this, that is, transaction methods are nested. After nesting, the nested methods will not have transactions. Therefore, I built a new public Service to solve the problem of transaction nesting, added transactions to the separately proposed methods, and worked out the transaction propagation mechanism as Propagation.REQUIRES_NEW, specific code.

@ Transactional (rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW) public void updateOrderInfoPersonOutOrgan (String toDoId) throws Exception {}

Propagation.REQUIRES_NEW means that if the caller currently has a transaction, it suspends the transaction and restarts the transaction. After calling this method, the method opens the transaction separately, commits each time, and rolls back the exception separately when it encounters an exception. It is good that the external method receives the thrown exception and does not handle it, so that the internal and external transactions do not affect each other.

Thank you for your reading, the above is the content of "what is the method of rolling back Spring transaction for cycle separately". After the study of this article, I believe you have a deeper understanding of what the method of separate rollback of Spring transaction for cycle is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Transactions methods loops learning code content single problem that is the inner layer that is ideas situations meaning articles more mechanisms users knowledge knowledge points Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Docker MySQL Shulou Technology Microsoft