Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use annotations to declare transactions in Spring

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use annotations to declare transactions in Spring. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Specific use of @ Transactional annotations.

Because the @ Transactional annotation has default transaction attributes, you only need to add the above annotation to manage the transaction. But there is one problem we should pay attention to, that is, although the above annotation has the default transaction function, we know that the underlying layer of transaction annotation implements transaction management through AOP, so we also need to configure xml in spring to make spring support @ Transactional annotation. The specific configuration is as follows:

The above configuration is the simplest configuration. In fact, there are two other attributes for the tag. Let's take a look at the specific functions of these two attributes.

Let's take a look at the relevant default properties of the @ Transactional annotation.

Transaction propagation behavior--: PROPAGATION_REQUIRED

Transaction isolation level--: ISOLATION_DEFAULT

Read / write transaction attributes-read / write transaction

Timeout -: depends on the default value of the underlying transaction attribute

Rollback setting -: run-time exception rollback

The default settings described above are the @ Transactional annotations, which are commonly used, but may not necessarily meet all our needs, so spring also provides us with the ability to manually set the above default annotations. The specific configuration is as follows:

Let's take a look at the notes for @ Transactional.

The @ Transactional annotation can be applied to interface definition interface methods, class definitions, and class public methods. But spring suggests that it's best to annotate @ Transactional on the business class. This is because annotations cannot be inherited, so if we mark @ Transactional annotations on the interface, its subclasses cannot inherit, so spring will not manage transactions for subclasses at this time.

If a class already uses @ Transactional annotations, spring will add transaction management to all public methods in the class. If we add method-level @ Transactional annotations to public methods at this time, spring will overwrite the @ Transactional annotations above the class with method annotations by default.

On how to use annotations in Spring to declare transactions here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report