SpringBoot when using Transactional things, want to add @ EnableTransactionManagement notes on the main method statement development things, Annotate the public methods of the service layer with @Transactional (Spring) annotations.

Use Example 1

First let’s look at how the @Transactional annotation can be used, simply by adding it to a public method that needs to be added. But doing so requires you to throw an exception and let Spring control it.

Code examples:

@Transactional public boolean test1(User user) throws Exception { long id = user.getId(); System.out.println(" query data 1:" + udao.findById(id)); Udao. insert(user); System.out.println(" query data 2:" + udao.findById(id)); udao.insert(user); return false; }Copy the code

Use Example 2

If we want to handle exceptions ourselves while using Transactional Transactional, we can manually roll back the Transactional. In the catch with TransactionAspectSupport. CurrentTransactionStatus (.) setRollbackOnly (); Method to manually roll back. However, it is important to note that an exception needs to be rolled back manually in the first place, before the exception is thrown!

Code examples:

@Transactional public boolean test2(User user) { long id = user.getId(); Try {system.out. println(" query data 1:" + udao.findById(id)); Udao. insert(user); System.out.println(" query data 2:" + udao.findById(id)); udao.insert(user); } catch (Exception e) {system.out.println (" Exception occurred, manual rollback! ); / / manual rollback things TransactionAspectSupport currentTransactionStatus () setRollbackOnly (); e.printStackTrace(); } return false; }Copy the code

Example 3

If you call another child method to perform a database operation when you use Transactional Transactional, but you want it to work, you can use the rollbackFor annotation or throw an exception from that child to be processed by the Transactional method. Submethods must also be public methods!

Code examples:

@transactional public Boolean test3(User User) {try {system.out.println (" Transactional data 1:" + udao.findById(user.getId())); deal1(user); deal2(user); deal3(user); } catch (Exception e) {system.out.println (" Exception occurred, manual rollback! ); / / manual rollback things TransactionAspectSupport currentTransactionStatus () setRollbackOnly (); e.printStackTrace(); } return false; } public void deal1(User user) throws SQLException { udao.insert(user); Println (" query data 2:" + udao.findById(user.getid ())); } public void deal2(User User) throws SQLException{if(user.getage ()<20){//SQL exception udao.insert(User); }else{ user.setAge(21); udao.update(user); Println (" findById(user.getid ()) + udao.findById(user.getid ())); }} @transactional (rollbackFor = SQLException. Class) public void deal3(User User){if(user.getage ()>20){//SQL exception udao.insert(user); }}Copy the code

Example 4

If we don’t want to use the Transactional annotation @Transactional and want to do Transactional control ourselves (programming Transactional management), but we don’t want to write that much code ourselves, You can use springboot DataSourceTransactionManager and TransactionDefinition in these two classes to use, can achieve manually control the commit rollback of things. Make sure that the object is opened but not committed. If it is not opened or committed, an exception will occur in the catch.

Code examples:

@Autowired private DataSourceTransactionManager dataSourceTransactionManager; @Autowired private TransactionDefinition transactionDefinition; Public Boolean test4(User User) {/* * TransactionStatus TransactionStatus =null; boolean isCommit = false; try { transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition); Println (" findById(user.getid ()) + udao.findById(user.getid ())); // Add/modify udao.insert(user); Println (" query data 2:" + udao.findById(user.getid ())); if(user.getAge()<20) { user.setAge(user.getAge()+2); udao.update(user); Println (" findById(user.getid ()) + udao.findById(user.getid ())); }else {throw new Exception(" Simulate an Exception!") ); } / / manual submitted dataSourceTransactionManager.com MIT (transactionStatus); isCommit= true; System.out.println(" Manual submission succeeded!" ); Throw new Exception(" Simulate the second Exception!" ); } catch (Exception e) {// Roll back if(! IsCommit){system.out.println (" Error, roll back manually!" ); / / manual rollback things dataSourceTransactionManager. Rollback (transactionStatus); } e.printStackTrace(); } return false; }Copy the code