Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Spring transaction & what is the function of lt;tx:annotation-driven/>

Shulou Source: shulou.com Published: 2022-06-02 09:49:45 11月11日 Update

This article mainly introduces "what is the function of Spring transaction". In daily operation, I believe that many people have doubts about the role of Spring transaction. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "what is the function of Spring transaction?" Next, please follow the editor to study!

When using SpringMvc, we often see annotations like annotation-driven in the configuration file, which means to support annotations, which can be understood bluntly according to the prefixes tx, mvc and so on. Tx:annotation-driven/ supports transaction annotations (@ Transactional) and mvc:annotation-driven supports mvc annotations. To put it bluntly, various annotations of MVC can be used in Controller.

First, there will be an attribute that specifies which transaction manager to use, such as:. Then the transaction manager transactionManager references dataSource (if we use JPA or Hibernate, we also need to specify an entityManagerFactory), dataSouce must be directly against the database. This goes on layer by layer, so it's easy to understand that we can use the @ Transactionl annotation to control the transaction. Another thing to mention is that spring uses aop to manipulate java bytecode through asm to manage the transaction before and after the method. Now that we have a simple understanding of this, can we use @ Transactional annotations on all classes managed by spring in the program, and [@ Transactional] on Service (https://my.oschina.net/u/3770144) annotations are sure, so there are always some people who want to know if they can be used in Controller? The answer is obviously "not necessarily" (related to the time configuration), which is explained below:

There is a passage on the spring-framework-reference.pdf document:

Tx:annotation-driven/ only looks for @ Transactional on beans in the same application context it is defined in. This means that, if you put tx:annotation-driven/ in a WebApplicationContext for a DispatcherServlet, it only checks for @ Transactional beans in your controllers, and not your services.

Tx:annoation-driven/ will only look for @ Transactional annotations on the bean defined in the same application context. If you put it in the Dispatcher application context, it will only check the @ Transactional annotations on the Controller, not the @ Transactional annotations on your services.

So, it's certain that we can use transaction annotations on Controller, but we don't recommend it (and I've never done it before), just to illustrate the use of spring.

The general configuration method is to let Spring manage annotations in addition to Controller annotations, and let SpringMVC manage Controller annotations alone. In other words, Spring has a configuration file, which is configured to scan non-Controller bean,SpringMVC has a configuration file, which only scans Controller. This creates two contexts, the context of Spring and the context of SpringMVC, which manage two different heap bean.

At this time, you add a tx:annoation-driven to the configuration file of Spring, which actually tells Spring that there may be transaction support in the bean you manage. Then an annotation @ transactional is added to a bean in the scope of Spring management, which is actually used to help Spring identify that this bean requires transaction management.

Similarly, you do not care if you add tx:annoation-driven,SpringMVC to the configuration file of Spring. He only cares about what you have configured for him and what notes are on the bean managed by him.

Link

Https://www.cnblogs.com/darknebula/p/9963859.html

At this point, the study of "what is the role of Spring transactions" is over. I hope to be able to 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!

Tags: Comments transactions management configuration that is files functions upper and lower context transaction management learning support methods help time more Riga application control affirmation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Apple Shulou Tech Info Linux Redmi