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

What is Spring declarative transaction control

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what is Spring declarative transaction control". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what Spring declarative transaction control is.

Programmatic transaction control related objects

1. The PlatformTransactionManager interface is the transaction manager of spring, which provides us with common methods to operate transactions.

2 、 TransactionDefinition

TransactionDefinition is the definition information object of a transaction, which has the following methods:

(1) transaction isolation level: set the isolation level to solve the problems caused by transaction concurrency, such as dirty reading, non-repeatable reading and virtual reading.

(2) transaction propagation behavior

REQUIRED: if there is no current transaction, create a new transaction, and if one already exists, join it. General selection (default).

SUPPORTS: supports the current transaction and, if there is no transaction, executes in a non-transactional manner (no transaction).

MANDATORY: uses the current transaction and throws an exception if there is no current transaction.

REQUERS_NEW: create a new transaction and suspend the current transaction if it is currently in the transaction.

NOT_SUPPORTED: performs the operation in a non-transactional manner, suspending the current transaction if there is a current transaction.

NEVER: runs in a non-transactional manner, throwing an exception if a transaction currently exists.

NESTED: if a transaction currently exists, it is executed within a nested transaction. If there is no transaction currently, perform a similar operation to REQUIRED.

Timeout: the default value is-1 and there is no timeout limit. If so, set it in seconds.

Read-only: it is recommended to set it to read-only when querying.

3 、 TransactionStatus

The TransactionStatus interface provides the specific running status of the transaction, as described below.

Declarative transaction control based on XML

1. What is declarative transaction control?

Spring's declarative transaction, as its name implies, is to handle transactions in a declarative manner. The declaration here refers to the declaration in the configuration file, instead of code-based transaction processing, with declarative transactions in the Spring configuration file. Its role is to manage transactions without intruding into developed components. Specifically, the business logic object will not realize that it is in the process of transaction management, and it should be so, because transaction management is a service at the system level, not part of the business logic, and if you want to change the transaction management planning, you only need to reconfigure it in the definition file. And when transaction management is not needed, the transaction management service can be removed as long as it is modified on the configuration file, and there is no need to change the code recompilation, which is extremely convenient to maintain.

2. Realization of declarative transaction control.

(1) introduce tx namespace

(2) configuration transaction enhancement

(3) configure transaction AOP weaving

(4) Test the transaction control transfer business code

@ Override

Public void transfer (String outMan, String inMan, double money) {

AccountDao.out (outMan,money)

Int I = 1max 0

AccountDao.in (inMan,money)

}

Declarative transaction control based on annotations

1. Annotation configuration declarative transaction control

(1) write AccoutDao

@ Repository ("accountDao")

Public class AccountDaoImpl implements AccountDao {

@ Autowired

Private JdbcTemplate jdbcTemplate

Public void out (String outMan, double money) {

JdbcTemplate.update ("update account set money=money-? where name=?", money,outMan)

}

Public void in (String inMan, double money) {

JdbcTemplate.update ("update account set money=money+? where name=?", money,inMan)

}

}

(2) write AccoutService

@ Service ("accountService")

@ Transactional

Public class AccountServiceImpl implements AccountService {

@ Autowired

Private AccountDao accountDao

@ Transactional (isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)

Public void transfer (String outMan, String inMan, double money) {

AccountDao.out (outMan,money)

Int I = 1max 0

AccountDao.in (inMan,money)

}

}

(3) write applicationContext.xml configuration file

Omit the configuration of datsSource, jdbcTemplate, platform transaction manager before-- >

Thank you for your reading, the above is the content of "what is Spring declarative transaction control". After the study of this article, I believe you have a deeper understanding of what Spring declarative transaction control is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Development

Wechat

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

12
Report