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

The usage of spring transaction and declarative transaction

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article mainly introduces the use of spring transactions and declarative transactions, the text is very detailed, has a certain reference value, interested friends must read!

This article brings you the content of how to understand the use of spring transactions and declarative transactions, including the isolation level of things in the database, there is a certain reference value, there are friends who need to refer to it, I hope to help you.

Use of spring and declarative transactions

(Students, start reviewing the database knowledge given back to teachers at university!)

Transaction: A program execution unit that accesses and possibly updates various data items in a database.

Transactions have four attributes: (ACID)

Atomicity: A transaction is an indivisible unit of work in which all operations are done or none are done.

Consistency; a transaction must change the database from one consistent state to another. Closely related to atomicity.

Isolation: The execution of a transaction cannot be disturbed by other things. That is, the internal operations and data used by a transaction are isolated from other concurrent things, and the transactions executed concurrently cannot interfere with each other.

Persistence: Persistence, also known as permanence, means that once a transaction is committed, its changes to the data in the database should be permanent.

Transaction purpose: To maintain data consistency and integrity.

Consistency: The data state of a business chain is consistent and cannot be partially changed or partially unchanged.

Integrity: The data of a business chain is complete, either completed together and failed together, and cannot be partially written successfully and partially written failed.

Simply understand consistency and integrity of transactions as either living together or dying together, not living alone. (It's like sad love...^_^)  

The isolation level of things in a database

Before we look at transaction isolation levels, let's take a look at several situations that often occur in data that can lead to business logic failure.

dirty reads

When a transaction is accessing data and making changes to the data that have not yet been committed to the database, another transaction accesses the data and uses it.

For example: Zhang San's bank account now has 1000, now Zhang San deposited 200, then when Zhang San clicked submit, his daughter-in-law (hard Zhang San is saving pocket money for his daughter-in-law) spent 500 shopping in the mall. Zhang San checked the balance and found that there was only 500 left. Then the two of them quarreled over 200. That's what it means to have a family war.

unrepeatable reading

Non-repeatable reading: reading the same data multiple times within a transaction. Before the transaction ends, another transaction accesses the data. Between the two reads of the first transaction, the data read by the first transaction may not be the same due to the modification of the second transaction. So what happens is that the data read twice within a transaction is different. (i. e. cannot read the same data)

phantom reading

A transaction modifies data in a table, involving all rows of data in the table, while a second transaction inserts a new row of data into the table. The user operating on the first transaction finds that there are still unmodified rows in the table. It's like a hallucination.

Five isolation levels of spring ISOLATION_DEFAULT

Represents the default isolation level of the underlying database, and for most databases the typical value is: ISOLATION _READ _COMMITTED

ISOLATION _READ _UNCOMMITTED

Indicates that a transaction can read data modified by another transaction but not yet committed, and cannot prevent dirty reads and non-repeatable reads.

ISOLATION _READ _COMMITTED

A transaction can only read data committed by another transaction, preventing dirty reads, but not non-repeatable reads. (Recommended for most cases)

ISOLATION _REPEATABLE _READ

A transaction can execute a query multiple times throughout the process and return the same record each time. Even if there is new data between queries that satisfy the query, these new records are ignored. Dirty reading and non-repeatable reading can be prevented.

ISOLATION _SERIALIZBLE

All transactions are executed one by one, so that there is no possibility of interference between transactions. It can prevent dirty reading, non-repetitive reading and phantom reading.

Transaction propagation (Spring provides seven)

A relationship between transactions, e.g., one transaction contains another transaction, and propagation is used to determine mutual execution.

TransationDefinition.PROPAGETION.REQUIRED

If a transaction currently exists, it is added; if no transaction currently exists, a new transaction is created.

Default transaction in spring. Suitable for most situations.

TransationDefinition.PROPAGETION.REQUIRED_NEW

Creates a new transaction and suspends the current transaction if one exists.

This means creating a new transaction that has nothing to do with the original transaction.

TransationDefinition.PROPAGETION.SUPPORTS

If a transaction currently exists, join the transaction; if no transaction currently exists, continue running non-transactionally.

This way is very casual, there is no, there is, a little indifferent attitude.

TransationDefinition.PROPAGATION.NOT_SUPPORTED

Runs in a non-transactional manner, suspending the current transaction if one exists.

This way is very tough, there is no, there is no support, hang up, regardless of it.

TransationDefinition.PROPAGETION_NEVER

Runs non-transactionally, throwing an exception if a transaction currently exists.

This way is tougher. If there is no one, there will be no one. If there is, he will report mistakes. He declared to everyone: I never support affairs.

TransationDefinition.PROPAGETION_MANDATORY

Join a transaction if it currently exists; throw an exception if it does not.

This way can be said to be the toughest, there is no business to report directly wrong, it says to the world: I must have business.

TransationDefinition.PROPAGETION_NESTED

If a transaction currently exists, create a transaction to run as a nested transaction of the current transaction; if no transaction currently exists, this value is equivalent to

TransationDefinition.PROPAGETION_REQUIRED

Declarative transactional use

Now in springboot, if you use declarative transactions:

@Transactionalpublic void save(Object ob){}

A method can be transactionally managed simply by adding the @Transactional annotation to it.

source code

Take a look at the source code for Transactional:

@Target({ElementType.METHOD, ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Inherited@Documentedpublic @interface Transactional {@AliasFor("transactionManager")String value() default "";@AliasFor("value")String transactionManager() default "";Propagation propagation() default Propagation.REQUIRED;Isolation isolation() default Isolation.DEFAULT;int timeout() default TransactionDefinition.TIMEOUT_DEFAULT;boolean readOnly() default false;Class

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

Database

Wechat

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

12
Report