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 Mavericks take you into the affairs of Spring

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

Share

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

Abstract

This paper extracts some theories related to Spring transactions, mainly about the characteristics of transactions, the propagation behavior of transactions, and the isolation rules of transactions.

Keywords: transaction characteristics, transaction propagation, transaction isolation

I. what is a transaction

Transactions are used to ensure the integrity and consistency of data, just like money transfers, the total amount of money will not increase or decrease.

Database transaction management has four features (ACID):

Feature description atomicity (Atomicity) transactions are executed as a whole, either all or none at all. Consistency transactions should ensure that the state of the data changes from one consistent state to another, and that the data should not be corrupted. Isolation (Isolation) when multiple transactions execute concurrently, the execution of one transaction should not affect the execution of other transactions. Durability) modifications to data by committed transactions should be permanent.

II. Spring transaction Manager

Transaction management interface

The general interface PlatformTransactionManager for transaction management is defined in Spring, which defines the following interfaces to manage transactions:

GetTransaction ()-gets the transaction status (TransactionStatus), which is defined by (TransactionDefinition)

Commit ()-commit transaction

Rollback ()-rollback the transaction

Spring does not manage transactions directly, but provides a transaction management interface, which is implemented by other platforms (such as JDBC, Hibernate).

Several specific transactions:

The transaction name manager class states that the JDBC transaction org.springframework.jdbc.datasource.DataSourceTransactionManager manages the transaction Hibernateorg.springframework.orm.hibernate3.HibernateTransactionManager transaction by calling java.sql.Connection. The org.hibernate.Transaction object is responsible for Java persistence API transaction (JPA) org.springframework.orm.jpa.JpaTransactionManagerJpaTransactionManager works with the JPA EntityManager generated by the factory (javax.persistence.EntityManagerFactory) to build the transaction Java native API transaction org.springframework.transaction.jta.JtaTransactionManagerJtaTransactionManager will transaction Responsibility for management is delegated to javax.transaction.UserTransaction and javax.transaction.TransactionManager objects (allowing multiple transactions to be managed between multiple databases)

About JTA, refer to transactions in Java-- JDBC transactions and JTA transactions

The Mavericks take you into the affairs of Spring

(Spring transaction Manager)

Transaction Management and AOP

Spring intercepts all business processing methods that require transaction management through AOP.

III. Spring transaction attributes

Spring transactions have several properties, which can be reflected by the TransactionDefinition interface:

Public interface TransactionDefinition {

/ / definition of transaction propagation behavior

Int PROPAGATION_REQUIRED = 0

Int PROPAGATION_SUPPORTS = 1

Int PROPAGATION_MANDATORY = 2

Int PROPAGATION_REQUIRES_NEW = 3

Int PROPAGATION_NOT_SUPPORTED = 4

Int PROPAGATION_NEVER = 5

Int PROPAGATION_NESTED = 6

/ / transaction isolation level definition

Int ISOLATION_DEFAULT =-1

Int ISOLATION_READ_UNCOMMITTED = Connection.TRANSACTION_READ_UNCOMMITTED

Int ISOLATION_READ_COMMITTED = Connection.TRANSACTION_READ_COMMITTED

Int ISOLATION_REPEATABLE_READ = Connection.TRANSACTION_REPEATABLE_READ

Int ISOLATION_SERIALIZABLE = Connection.TRANSACTION_SERIALIZABLE

/ / default timeout

Int TIMEOUT_DEFAULT =-1

/ / get transaction propagation behavior

Int getPropagationBehavior ()

/ / get the transaction isolation level

Int getIsolationLevel ()

/ / get the transaction timeout

Int getTimeout ()

/ / get whether the transaction is read-only

Boolean isReadOnly ()

/ / get the transaction name

String getName ()

}

With the definition of interface and rollback rules, transactions can have the following five basic configurations: transaction propagation behavior, transaction isolation level, transaction timeout, transaction read-only, and rollback rules.

Communication behavior

The so-called propagation behavior refers to how a transaction should be handled when one transaction method is called by another transaction method. The called method may run in an existing transaction or start a new transaction to run within its own transaction.

Propagate behavior meaning interpretation PROPAGATION_REQUIRED indicates that the current method must be run in a transaction. If the current transaction exists, the method will run in that transaction. Otherwise, a new transaction will be started with one and only one transaction PROPAGATION_SUPPORTS indicating that the current method does not require a transaction context, but if there is a current transaction, the method will run in this transaction if the method is called alone, it is a non-transactional execution, and if the method is called by another transactional method, the called method is added to the transaction PROPAGATION_MANDATORY indicating that the method must be run in the transaction If the current transaction does not exist, an exception is thrown. If a single non-transactional call throws an exception, it must be called by other transaction methods and added to the transaction. PROPAGATION_REQUIRED_NEW indicates that the current method must run in its own transaction. A new transaction will be started. If there is a current transaction, the current transaction is suspended during the execution of the method. If you use JTATransactionManager, you need to access TransactionManager to always open a new transaction, and if the original calling transaction exists, it will be suspended until the transaction is completed. PROPAGATION_NOT_SUPPORTED indicates that the method should not run in a transaction. If there is a current transaction, the current transaction is suspended while the method is running. If you use JTATransactionManager, you need to access TransactionManager to always execute the method non-transactionally, and it will be suspended if the calling transaction exists until the method execution completes PROPAGATION_NEVER indicates that the current method should not be running in the transaction context. If there is currently a transaction running, an exception is always thrown to execute a non-transactional method, otherwise throwing an exception PROPAGATION_NESTED indicates that if a transaction already exists, the method will run in a nested transaction. Nested transactions can be committed or rolled back independently of the current transaction. If the current transaction does not exist, it behaves like PROPAGATION_REQUIRED. Note that the support of different manufacturers for this kind of communication behavior is different. You can refer to the resource manager's documentation to confirm that they support nested transactions. a very important concept is that inner transactions depend on outer transactions. When the outer transaction fails, the actions made by the inner transaction are rolled back. The failure of the inner transaction operation does not cause the rollback of the outer transaction.

Isolation rule

There will be concurrency in the execution of transactions, and the isolation level defines the extent to which one transaction is affected by other concurrent transactions.

There are two forms:

Problems caused by concurrent transactions

Problem definition causes dirty read invalid data when one transaction reads data modified by another transaction but not committed, if the modification is rolled back, the transaction reads invalid data. Can not be read repeatedly in the same transaction twice in the same query, read different data one transaction during two queries, another concurrent transaction to update the data. Phantom reading the same transaction in two identical queries, one transaction reads data with different records, and during two queries, another concurrent transaction adds or deletes data.

Isolation level

Isolation level describes the problems that may be caused by concurrent transactions ISOLATION_DEFAULT uses the default isolation level of the back-end database to analyze the lowest isolation level of ISOLATION_READ_UNCOMMITTED, allows you to read data that has not been committed, such as dirty reading, non-repeatable reading, misreading isolation _ READ_COMMITTED, allows you to read data that has been committed by concurrent transactions that cannot be read repeatedly, misreading isolation _ REPEATABLE_READ has consistent results for multiple reads of the same field. Unless the field is modified by this transaction, the highest isolation level of isolation _ SERIALIZABLE, which is in full compliance with the ACID principle, is usually achieved (slowest) by locking the table.

Transaction timeout

Given a timeout for a transaction, if the transaction does not complete execution at a specific time, the transaction will be automatically rolled back.

Is it read-only?

If a read-only transaction is set, the modification of the database by other transactions is transparent to the transaction during the execution of the transaction, that is, the modified data of other transactions can not be seen. This is usually used to ensure the overall consistency of the data when performing multiple queries to count the information.

Rollback rule

The transaction rolls back which exceptions are defined by the rollback rule. By default, a transaction is rolled back only if it encounters a run-time exception, but not when it encounters a checked exception.

IV. Spring transaction management form

Programmatic transaction

Use TransactionTemplate or PlatformTransactionManager

TransactionTemplate tt = new TransactionTemplate ()

Tt.execute (status-> {

/ / perform the operation

DoSth ()

/ / return the operation result

Return obj

});

Declarative transaction

Reference: five ways to configure Spring transactions

VI. Summary

Spring defines the interface of the transaction manager, which is implemented by a specific platform.

Spring transactions have five properties: transaction propagation behavior, transaction isolation level, transaction timeout, transaction read-only, and rollback rules

Propagation behavior: required, supports, mandatory, required_new, not_support, never, nested

Isolation level: default, read_uncommited, read_committed, repeatable_read, serializable

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