Analysis of spring declarative transaction management

Cutting edge: through in-depth study of spring transaction management, this article will not do an example, but specify specific classes and configuration files to explain.

Content of this article:

1. Understand what is declarative transaction?

2. What are the types of declarative transaction management?

3. What are the differences between these kinds of transaction management?

1、 What is a declarative transaction?

Declarative transaction management is one of the methods of program transaction management provided by spring. Spring's declarative transaction is to process transactions in a declarative way, and the declarative transaction in the spring configuration file is used to replace the code transaction. The advantage of this is that transaction management does not invade the developed components. Specifically, business logic objects will not realize that they are in transaction management. In fact, it should also be so, because transaction management is a system level service, not a part of business logic. If you want to change transaction management, you only need to modify the configuration in the defined configuration file, When transaction management is not required, the transaction management service can be removed by modifying the configuration file without changing the code and recompiling, which is extremely convenient for maintenance.

2、 Declarative transaction management.   

First, let's look at the original transaction management before stating the explicit transaction management method.

As can be seen from the figure above, first we need to create a new session Opensession(), followed by gettransaction() of session The begin () method and gettransaction () The commit () method is used for transaction creation and transaction submission, and the session needs to be closed Close(), so that every time we call this method, we will re create a new session and do the same thing repeatedly, which is not in line with our software engineering design idea. Next, we use declarative transaction management to simplify and adjust the code.

1. Use spring AOP declarative transaction management.

1.1. We need to import several AOP packages:

1.2. Then in application Transaction configuration in XML configuration file:

1.3. Modify the method of querying all data:

After the above explanation, we can see the modification of showallnews() method. First, we change the original opensession() method to getcurrentsession() method (Note: there are comments on the screenshot to explain their differences, so I won't repeat them). There is no need to manually create and submit transactions. Our spring AOP will automatically manage transactions Automatically close the session.

2. Use @ transactional annotation.

2.1. First of all, we need to be in application Transaction configuration in the XML configuration file (notify the spring container to process the bean annotated @ transactional):

2.2. No operation or modification is required in newsdaoimpl:

2.3. Add comments to newsserviceimpl.

Here, we can add @ transactional annotation to the class (indicating that all methods are added to transaction management), or add @ transactional annotation to specific methods. If it is a method that does not need to modify the data, we need to add an attribute.

For example, the method @ transactional (readonly = true) to query all data

3. The difference between declarative transaction management configured with spring AOP and @ transactional annotation:

1) spring AOP configuration declarative transaction management has low coupling, low readability, detailed expression and high flexibility.

2) @ transactional annotation has high readability and scattered content, which is not conducive to unified management and maintenance, and has high coupling.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>