In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to use the @ Transactive attribute in Spring". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Preface
Today, when I was writing code, I saw it. A note @ Transactional (rollbackFor = Exception.class). Today I would like to share with you the usage of this note.
Abnormal
As shown in the following figure, we all know that Exception is divided into runtime exception RuntimeException and non-runtime exception
Error is bound to roll back.
If the runtime exception is not handled, either the thread aborts or the main program terminates after the runtime exception occurs.
If you do not want to terminate, you must catch all run-time exceptions and never let the processing thread exit. There is abnormal data in the queue, the normal processing should be to discard the abnormal data, and then log. The processing of normal data should not be affected by abnormal data.
Non-runtime exceptions are exceptions other than RuntimeException and belong to the Exception class and its subclasses in type. Such as IOException, SQLException, etc., and user-defined Exception exceptions. For this kind of exception, the JAVA compiler forces us to catch and handle these exceptions, otherwise the program will not be compiled. So, faced with this kind of exception, whether we like it or not, we have to write a lot of catch blocks to handle possible exceptions.
Transaction management mode
Transaction management is very important for enterprise applications, even if there are anomalies, it can also ensure the consistency of data.
Spring supports 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.
Declarative transaction management is based on AOP. Its essence is to intercept before and after the method, then create or join a transaction before the target method starts, and commit or roll back the transaction according to execution after the target method is executed.
There are also two common ways of declarative transaction management, one is xml configuration files based on tx and aop namespaces, and the other is based on @ Transactional annotations. Obviously, the annotation-based approach is easier to use and more refreshing.
instructions
When acting on a class, all public methods of that class will have transactional properties of that type, and we can also use this annotation at the method level to override the class-level definition.
In the project, @ Transactional (rollbackFor=Exception.class), if this annotation is added to the class, the method in the class throws an exception and the data in the database is rolled back.
If the rollbackFor attribute is not configured in the @ Transactional annotation, things will only be rolled back when they encounter RuntimeException, and rollbackFor=Exception.class will allow things to roll back when they encounter non-runtime exceptions
Detailed explanation of all attributes of @ Transactional annotations
@ Transactionalattribute
Attribute type describes the valueString optional qualifying descriptor, specifies the transaction manager propagationenum: Propagation optional transaction propagation behavior settings isolationenum: Isolation optional transaction isolation level settings readOnlyboolean read-write or read-only transactions, default read-write timeoutint (in seconds granularity) transaction timeout sets the rollbackForClass object array, must inherit from the Throwable-induced transaction rollback exception class array rollbackForClassName class name array Must inherit from the exception class name array noRollbackForClass object array that Throwable causes transaction rollback, must inherit from Throwable exception class array noRollbackForClassName class name array that will not cause transaction rollback, and must inherit from Throwable exception class name array "how to use @ Transactionalattribute in Spring". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.