Recently encountered a problem, with spring management practice, in the service layer to process the data, save the data when the exception, but did not rollback, check, and found that because I use a try catch to catch the exception, no throw caused by the default spring transaction only in the event of an uncaught Runtimeexcetpion is not rolled back.
Treatment of the FA one: after catching the anomaly, the newborn into a runtimeexcetpion;
- try {
- Userdao.save (user);
- Userdao.update (user);
- } catch (Exception e) {
- Logger.info ("Exception information:" +e);
- throw new RuntimeException ();
- }
Processing method Two: After catching an exception, add Transactionaspectsupport.currenttransactionstatus (). setrollbackonly () Manual rollback
- try {
- Userdao.save (user);
- Userdao.update (user);
- } catch (Exception e) {
- Logger.info ("Exception information:" +e);
- Transactionaspectsupport.currenttransactionstatus (). Setrollbackonly ();
- }
Processing method Three: Capturing without a try catch; The method of exception will be rolled back by spring management
1. Userdao.save (user);
2. Userdao.update (user);
Spring transaction does not roll back