Luminous takes you into the field of management system development (2)

Luminous Prologue:

 

Sparrows always smell like lightning, and ants have the color of tree roots.

 

 

 

 
 
Text:
 
                                              Recognize the Tao with the Tao

用   springboot

For learners, you need to spend your time on technology, not on business. When you work in the future, you will spend a lot of time doing business, so calm down and study hard, especially for beginners. , Need to lay a solid foundation for technology, you can benefit a lot.

To do background development, the basic skills should be solid, the preparation of interfaces and implementation classes

Pay attention to writing specifications

Pay attention to the usage rules of some annotations

@Transactional (rollbackFor = Exception.class) property in Spring

 

First of all, we all know that if the runtime exception is not handled, then after the runtime exception occurs, either the thread is terminated or the main program is terminated. 


If you do not want to terminate, you must catch all runtime exceptions and never let this processing thread exit.

Abnormal data appears in the queue. The normal treatment should be to discard the abnormal data and then log it.

It should not affect the following processing of normal data due to abnormal data.



Non-runtime exceptions are exceptions other than RuntimeException, which belong to the Exception class and its subclasses.

Such as IOException, SQLException, etc. and user-defined Exception.

For this kind of exception, the JAVA compiler mandates that we must catch and deal with these exceptions, otherwise the program will not compile.

 

Therefore, in the process of actual development and project, no matter whether we are willing to face this kind of exception, we can only write a lot of catch blocks to deal with possible exceptions.


Here we need to emphasize transaction management

Transaction management is crucial for enterprise applications. Even if abnormal conditions occur, it can also ensure data consistency.

Spring supports two methods of programmatic transaction management and declarative transaction management.

   Programmatic transaction management uses TransactionTemplate or directly uses the underlying PlatformTransactionManager.

        For programmatic transaction management, Spring recommends using TransactionTemplate.


That one:  

     Declarative transaction management is built on AOP.

       The essence is to intercept the method before and after, and then create or join a transaction before the target method starts, and commit or roll back the transaction according to the execution after the target method is executed.

  There are also two common ways of declarative transaction management

      One is an XML configuration file based on the tx and aop namespace

     The other is based on the @Transactional annotation. Here: Obviously the annotation-based approach is simpler to use and more refreshing.

 

In the project, @Transactional (rollbackFor = Exception.class), if the class is annotated, then the method in this class throws an exception, it will be rolled back, and the data in the database will also be rolled back.

 

If you do not configure the rollbackFor attribute in the @Transactional annotation, then things will only roll back when they encounter RuntimeException, plus rollbackFor = Exception.class, you can make things roll back when they encounter non-runtime exceptions.

 

All properties of @Transactional annotation


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Published 1529 original articles · praised 305 · 180,000 views +

Guess you like

Origin blog.csdn.net/weixin_41987706/article/details/104674885