Use of transactions in Springboot:

The startup class enables transaction support (which is enabled by default).

Annotate public methods (or classes – equivalent to all public methods of that class) that use transactions with the @Transactional annotation.

Transactional in a controller->service process:

If the controller does not start a transaction, the service starts a transaction, the service executes successfully, and the Controller is not automatically rolled back if an exception (error) occurs during the subsequent operation.

That is, if an exception occurs in a method that starts a transaction (by default only non-detected exceptions take effect -RuntimeException) (error-error), the rollback will be automatic.

If you want to throw any exceptions are automatically roll back, not only for www.cungun.comRuntimeException, only need to use (rollbackFor = Exception. Class).

Transaction rollback cases in methods that start transactions:

(1) If an exception is not found, the program will automatically throw a RuntimeException or a subclass of RuntimeException. The game program will be terminated and automatically rolled back.

(2) using TransactionAspectSupport. CurrentTransactionStatus (.) setRollbackOnly (); Perform manual rollback.

If a RuntimeException is handled in a try-catch statement and no manual exception is thrown, Spring considers the method to be executed successfully and does not roll back. In this case, you need to call the method in step 2 to roll back manually. If relative electron has more intuitive understanding, it can also refer to its format as follows:

Game: www.cungun.com

In addition, if a try-catch statement returns in finally, exceptions thrown manually in the catch are overwritten and will not be rolled back automatically. try{

throw new RuntimeException(); }catch(RuntimeException e){

e.printStackTrace(); }finally{} // Will automatically rollback try{

throw new RuntimeException(); }catch(RuntimeException e){

e.printStackTrace(); throw new RuntimeException(); }finally{ }