In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you what the principle of Spring affairs is, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
The principle of 1.Spring transaction
Spring transaction management is divided into two ways: coding and declarative. Programmatic transactions refer to the implementation of transactions by coding; declarative transactions are based on AOP and decouple specific business logic from transaction processing. Declarative transaction management makes the business code logic uncontaminated, so declarative transactions are often used in practical use. There are two ways for declarative transactions, one is to declare the relevant transaction rules in the configuration file, and the other is based on the @ Transactional annotation.
Using @ Transactional compared to the traditional we need to manually open the transaction and then commit the transaction. It provides the following convenience
Set whether to start the transaction automatically according to your configuration
Automatically commit transactions or automatically roll back when an exception is encountered
The basic principles of declarative transactions (@ Transactional) are as follows:
The configuration file opens the annotation driver and identifies the related classes and methods with the annotation @ Transactional.
When spring starts, it parses and generates the relevant bean. At this time, it looks at the classes and methods with relevant annotations, generates proxies for these classes and methods, and injects relevant configuration according to the relevant parameters of @ Transaction. In this way, the relevant transactions are processed for us in the broker (normal commit transaction is enabled, exception rollback transaction is rolled back).
The real transaction commit and rollback in the database layer is achieved through binlog or redo log.
Pay attention to the use of 2.@Transactional
The @ Transactional annotation triggers the rollback of the transaction only when RuntimeException or Error is thrown. The usual non-RuntimeException does not trigger the rollback of the transaction. However, when we do business processing, we need to catch exceptions, so we can manually throw RuntimeException exceptions or add rollbackFor = Exception.class (you can also specify the corresponding exceptions)
/ *
* when catching an exception, to make the transaction effective, you need to manually throw a RuntimeException exception or add rollbackFor = Exception.class
, /
@ Override
@ Transactional
Public Long addBook (Book book) {
Long result = null
Try {
Result = bookDao.addBook (book)
Int I = 1max 0
} catch (Exception e) {
E.printStackTrace ()
Throw new RuntimeException ()
}
Return result
}
@ Override
@ Transactional (rollbackFor = Exception.class)
Public Long addBook (Book book) {
Long result = null
Try {
Result = bookDao.addBook (book)
Int I = 1max 0
} catch (Exception e) {
E.printStackTrace ()
Throw e
}
Return result
}
Only the method modified by public will take effect
The transaction caused by the self-call in the method does not take effect.
There are several situations as follows:
/ *
* case 1: all have transaction comments. The exception occurs in the sub-method and the transaction takes effect.
, /
@ Override
@ Transactional
Public Long addBook (Book book) {
Long result = add (book)
Return result
}
@ Transactional
Public Long add (Book book) {
Long result = bookDao.addBook (book)
Int I = 1max 0
Return result
}
/ *
* case 2: all have transaction comments. The exception occurs in the main method and the transaction takes effect.
, /
@ Override
@ Transactional
Public Long addBook (Book book) {
Long result = add (book)
Int I = 1max 0
Return result
}
@ Transactional
Public Long add (Book book) {
Long result = bookDao.addBook (book)
Return result
}
/ *
* case 3: only the main method has transaction comments, the exception occurs in the sub-method, and the transaction takes effect.
, /
@ Override
@ Transactional
Public Long addBook (Book book) {
Long result = add (book)
Return result
}
Public Long add (Book book) {
Long result = bookDao.addBook (book)
Int I = 1max 0
Return result
}
/ *
* case 4: only the main method has transaction comments, the exception occurs in the main method, and the transaction takes effect.
, /
@ Override
@ Transactional
Public Long addBook (Book book) {
Long result = add (book)
Int I = 1max 0
Return result
}
Public Long add (Book book) {
Long result = bookDao.addBook (book)
Return result
}
/ *
* case 5: only sub-methods have transaction comments, exceptions occur in sub-methods, and transactions do not take effect.
, /
@ Override
Public Long addBook (Book book) {
Long result = add (book)
Return result
}
@ Transactional
Public Long add (Book book) {
Long result = bookDao.addBook (book)
Int I = 1max 0
Return result
}
These are the principles of Spring transactions. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.