Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use annotations to declare transactions in Spring

Shulou Source: shulou.com Published: 2022-06-02 05:56:51 11月13日 Update

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.

Tags: Comments transactions properties methods configuration management transaction management functions interfaces two content subclasses underlying articles more knowledge articles levels support good Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Microsoft Linux vpn Apple