In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to solve the problem of try catch failure encountered by @ Transactional. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
@ Transactional encountered try catch failure
@ Transactional in Springboot encountered try catch failure and needs to be rolled back manually
Try {user.setExpire (Integer.parseInt (expire)); userService.updateById (user); Host host = hostService.getById (user.getHostId ()); / / query database host information updateResponse = proxmoxConfig.updateEffectiveTime (user.getUserName (), expire, host.getHostUrlRequest ()) } catch (Exception e) {e.printStackTrace (); TransactionAspectSupport.currentTransactionStatus () .setRollbackOnly ();} @ Transactional and try..catch experience at the same time
@ transactional is an annotation that we often use in java development, which helps us to complete transaction management very conveniently and quickly, especially when processing data in batches. Recently, we have encountered this situation in our business.
When we import data into the database in batches, some data types in the excel table meet the import requirements, and some data types do not meet the import requirements. When we import, either all of them are successfully imported, or we fail to import them at all. If we do not use transactions, the correct data will be imported into the database, and the incorrect ones will not be imported, resulting in data inconsistency.
But my business not only completes the transaction with @ transactional annotations, but also returns an error prompt when the data does not match, so I also have to tuberculosis try...catch blocks. The problem is that using try...catch is to catch exceptions and then deal with them, while @ transactional can only play the role of transaction rollback when the system throws exceptions.
Later, I checked some information and did the following:
The key point is the TransactionAspectSupport.currentTransactionStatus (). SetRollbackOnly method, which acts as an exception rollback, so that I can set the custom information to be returned after it is executed
Recently, I have encountered the problem that the @ transactional annotation does not work. This time it turns out that my mysql database uses the MyISAM engine by default, but this storage engine does not support transactions, so we need to do some operations on the database: type alter table xxx (table name) engine=InnoDB in the console, and then you can type show create table xxx (table name) to see if the modification is successful.
Thank you for reading! This is the end of this article on "how to solve the problem of try catch failure encountered by @ Transactional". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.