Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of this invocation with Spring transaction failure

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "Spring transaction failure how to solve the problem of this call". In daily operation, I believe many people have doubts about how to solve the problem of this call when Spring transaction fails. The editor consulted all kinds of data and sorted out simple and useful operation methods. I hope it will be helpful to answer the doubt of "how to solve the problem of this call when Spring transaction fails." Next, please follow the editor to study!

PROPAGATION_REQUIRED: if there is a transaction, the current transaction is supported. Start a transaction if there is no transaction

PROPAGATION_REQUIRES_NEW: always start a new transaction. If a transaction already exists, suspend the existing transaction

Question:

When a method A without a transaction in Spring calls method B of a default transaction (PROPAGATION_REQUIRED), if method B is called with this, method B throws a RuntimeException, and the method B transaction is not in effect and will not be rolled back.

@ Servicepublic class EmployeeService {@ Autowired private EmployeeDao employeeDao; public void save () {try {this.saveEmployee (); / / the this call here will not open the transaction, and the data will be saved} catch (Exception e) {e.printStackTrace () } @ Transactional (propagation = Propagation.PROPAGATION_REQUIRED) / / neither PROPAGATION_REQUIRED nor PROPAGATION_REQUIRES_NEW is valid. Public void saveEmployee () {Employee employee = new Employee (); employee.setName ("zhangsan"); employee.setAge ("26"; employeeDao.save (employee); throw new RuntimeException ();}}

The cause of the problem:

Dynamic proxy for JDK. A transaction occurs only when it is called directly by a dynamic proxy. The object of the call returned in the SpringIoC container is a proxy object rather than a real object. The this here is an EmployeeService real object rather than a proxy object.

Solution:

Method 1. Open a transaction on method A, method B does not need a transaction or default transaction, and throw new RuntimeException () in the catch of method A; (when no rollbackFor is specified, the default rollback exception is RuntimeException), so that the transaction of method An is used. (be sure to throw new RuntimeException (); otherwise the exception will not be rolled back if it is caught and handled. ) as follows:

@ Transactional () / / Open transaction public void save () {try {this.saveEmployee (); / / here the this call will invalidate the transaction and the data will be saved} catch (Exception e) {e.printStackTrace (); throw new RuntimeException ();}}

Method 2. No transaction can be opened on method A, but transaction can be opened on method B. in method A, the this call can be changed to a dynamic proxy call (AopContext.currentProxy ()), as follows:

Public void save () {try {EmployeeService proxy = (EmployeeService) AopContext.currentProxy (); proxy.saveEmployee ();} catch (Exception e) {e.printStackTrace ();}} at this point, the study on "how to solve the problem of this invocation with Spring transaction failure" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report