In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the reasons for the failure of spring transactions". In the daily operation, I believe that many people have doubts about the causes of the failure of spring transactions. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what are the reasons for the failure of spring transactions?" Next, please follow the editor to study!
The implementation method and principle of 1.spring transaction
The essence of Spring transaction is the transaction support of the database. Without the transaction support of the database, spring can not provide transaction function. The real transaction commit and rollback in the database layer is committed after the binlog commit and redone through redo log, and undo log rolls back and forth.
Generally what we use in the program is to add @ Transactional annotation to the method, which is a declarative transaction.
The essence of declarative transaction is to intercept the method before and after the AOP function, weave the function of transaction processing into the intercepting method, that is, add a transaction before the target method starts, commit or roll back the transaction according to the execution situation after the target method is executed.
two。 The database itself does not support transactions
Here, take MySQL as an example. Its MyISAM engine does not support transaction operations. InnoDB is the engine that supports transactions. InnoDB is generally used to support transactions.
3. The call to the current class @ Servicepublic class UserServiceImpl implements UserService {public void update (User user) {updateUser (user);} @ Transactional (rollbackFor = Exception.class) public void updateUser (User user) {/ / update user}}
In this case, there will be no transaction management operation.
By looking at the principle of declarative transactions, we can see that spring uses AOP aspects, and in essence uses dynamic proxies to achieve the purpose of transaction management. it is useless to add @ Transactional to the methods called by the current class, because this method is called by this.
OK, we are looking at an example below.
@ Servicepublic class UserServiceImpl implements UserService {@ Transactional (rollbackFor = Exception.class) public void update (User user) {updateUser (user);} @ Transactional (propagation = Propagation.REQUIRES_NEW) public void updateUser (User user) {/ / update user}}
This time, we added @ Transactional,updateUser and REQUIRES_NEW to the update method to open a new transaction, so does the new transaction work?
The answer is: it doesn't work!
Because they make their own calls, they adjust their own methods of this class without passing through the Spring proxy class. By default, only externally invoked transactions will take effect, which is also a clich é classic problem.
4. Method is not @ Servicepublic class UserServiceImpl implements UserService {@ Transactional (rollbackFor = Exception.class) private void updateUser (User user) {/ / update user}} of public
The private method is not proxied by spring, so no transactions are generated, which is invalid.
5. Not managed by spring / / @ Servicepublic class UserServiceImpl implements UserService {@ Transactional (rollbackFor = Exception.class) public void updateUser (User user) {/ / update user}}
For bean that is not managed by spring, spring cannot even generate proxy objects, which is of course invalid.
6. There is a problem with transaction propagation configured @ Servicepublic class UserServiceImpl implements UserService {@ Transactional (propagation = Propagation.NOT_SUPPORTED) public void update (User user) {/ / update user}}
Review the transaction propagation behavior of spring
The propagation behavior of Spring transactions refers to how Spring handles multiple transactions when they exist at the same time.
PROPAGATION_REQUIRED: if there is no transaction currently, create a new transaction, and if there is a transaction, join the transaction. This setting is the most commonly used setting.
PROPAGATION_SUPPORTS: supports the current transaction. If there is a transaction, join it. If there is no transaction, execute it as a non-transaction.
PROPAGATION_MANDATORY: support the current transaction, join the transaction if there is a transaction, and throw an exception if no transaction exists.
PROPAGATION_REQUIRES_NEW: create a new transaction, regardless of whether it currently exists or not.
PROPAGATION_NOT_SUPPORTED: performs the operation in a non-transactional manner, suspending the current transaction if there is a current transaction.
PROPAGATION_NEVER: executes in a non-transactional manner, throwing an exception if a transaction currently exists.
PROPAGATION_NESTED: if a transaction currently exists, it is executed within a nested transaction. If there is no transaction currently, press the REQUIRED property to execute
When the propagation behavior is set to PROPAGATION_NOT_SUPPORTED,PROPAGATION_NEVER,PROPAGATION_SUPPORTS, it is possible that the transaction will not take effect.
7. You caught the exception @ Servicepublic class UserServiceImpl implements UserService {@ Transactional (rollbackFor = Exception.class) public void update (User user) {try {/ / update user} catch (Execption e) {log.error ("exception", e)}
The exception is caught, so the agent class has no way to know if you have any mistakes and whether you need to roll back, so there is no way to roll back this situation.
8. The interface layer declarative transaction uses the cglib proxy public interface UserService {@ Transactional (rollbackFor = Exception.class) public void update (User user)} @ Servicepublic class UserServiceImpl implements UserService {public void update (User user) {/ / update user}}
Controls whether interface-based or class-based proxies are created by the value of the "proxy-target-class" attribute of the element. If the "proxy-target-class" attribute value is set to "true", then the class-based proxy will work (in this case, the CGLIB library cglib.jar is required in CLASSPATH). If the "proxy-target-class" attribute value is set to "false" or if this property is omitted, then the standard JDK interface-based proxy will work
The biggest difference between @ Transactional cglib and java dynamic proxy is that the proxy target object does not need to implement the interface, so if the annotation is written on the interface method, if the cglib proxy is used, the annotation transaction will be invalidated. In order to maintain compatibility, annotations should be written on the implementation class methods.
9.rollbackFor exception specified error @ Servicepublic class UserServiceImpl implements UserService {@ Transactional public void update (User user) {/ / update user}} at this point, the study of "what are the reasons for the failure of spring transactions" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.