Frontier: With a more in-depth study of spring transaction management, this article will not make examples, but specify specific classes and configuration files for explanation.
Content of this article:
1. Understand what is a declarative transaction?
2. What are the declarative transaction management?
3. What is the difference between these types of transaction management?
1. What is a declarative transaction?
Declarative transaction management is one of the ways to manage program transactions provided by spring. Spring's declarative transactions use the declarative method to handle transactions, and use the declarative processing of transactions in Spring configuration files to replace the code-based processing of transactions. The advantage of this is that transaction management does not invade the development components. Specifically, the business logic object will not realize that it is in transaction management. In fact, the same should be true, because transaction management is a system-level service, not a part of the business logic. If you want to change transaction management, you only need to modify the configuration in the defined configuration file. When no transaction management is needed, just modify it on the configuration file and remove the transaction management service without changing the code and recompiling it, which is extremely convenient to maintain.
2. Declarative transaction management method.
First, let’s take a look at the original transaction management before we can talk about the declarative transaction management method.
As can be seen from the above figure, first we need to create a new session.openSession(), secondly, we need to create and commit transactions through the session's getTransaction().begin() method and getTransaction().commit() method, and we need to close session.close(). In this way, every time we call this method, we will recreate a new session and do the same thing repeatedly, which does not conform to our software engineering design idea. Next, we use declarative transaction management to simplify and adjust the code.
1. Use springAOP declarative transaction management.
1.1. We need to import several packages of aop:
1.2. Then perform transaction configuration in the application.xml configuration file:
1.3. Modify the method of querying all data:
After the above explanation, we can see the modification of the showAllNews() method. First, we change the original openSession() method to getCurrentSession() method (Note: the screenshot has comments to illustrate their differences, so I will not repeat them). There is no need to manually create and commit transactions. Our springAOP will automatically manage the transactions and automatically close the session.
2. Use @Transactional annotation method.
2.1. First, we need to perform transaction configuration in the application.xml configuration file (notify Spring container to handle the bean processing of annotation @Transactional):
2.2. Then there is no need to do any operations or modifications in NewsDaoImpl:
2.3. Add annotations to NewsServiceImpl.
Here we can add @Transactional annotation to the class (which means that all methods are added to transaction management), or add annotation @Transactional to the specific method. If it is a method that does not require modifying the data, add an attribute.
For example: method to query all data @Transactional(readOnly=true)
3. The difference between declarative transaction management configured using SpringAOP and @Transactional annotation:
1) SpringAOP configuration declarative transaction management has low coupling, low readability, detailed expression, and high flexibility.
2) @Transactional annotation has high readability, and the content is dispersed and not conducive to unified management and maintenance, and has high coupling.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.