In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "Spring's management and support of affairs". In daily operation, I believe that many people have doubts about the management and support of Spring's affairs. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "Spring's management and support of affairs". Next, please follow the editor to study!
The advantage of spring transaction management is declarative transaction management, that is, boundaries and transaction attributes can be specified in the IOC configuration, so that spring automatically applies transaction attributes to the specified transaction boundaries.
In spring transaction management, there are three main interfaces, they are: PlatformTransactionManager, TransactionDefinition, TransactionStatus. Their three specific relationships are as follows:
Let's take a closer look at the specific functions of the above three interfaces.
TransactionDefinition: the user describes the transaction attributes that control the specific behavior of the transaction, such as the isolation level of the transaction, the timeout, whether it is a read-only transaction and transaction propagation rules. PlatformTransactionManager creates a transaction based on the transaction attribute configuration information provided by TransactionDefinition and uses TransactionStatus to describe the state of the active transaction for this transaction. We have introduced the relevant attributes that TransactionDefinition uses to describe transactions above, so let's take a look at which transaction attributes are included in TransactionDefinition.
Transaction isolation: the degree of isolation between the current transaction and other transactions. The same isolation levels are defined in TransactionDefinition as in the java.sql.Connection interface: ISOLATION_READ_UNCOMMITTED, ISOLATION_READ_COMMITTED, ISOLATION_REPEATABLE_READ, ISOLATION_SERIALIZABLE. The constants described above correspond to the isolation levels of the databases mentioned in other articles. In addition, TransactionDefinition includes the default isolation level ISOLATION_DEFAULT. It indicates that the default isolation level of the underlying database is used.
Transaction propagation: under normal circumstances, all code executed in a transaction runs in a single transaction, but spring provides us with several optional transaction propagation types: simply participate in an existing transaction, or suspend the current transaction, or create a new transaction.
Transaction timeout: how long the transaction can run before the timeout, after which the transaction will be rolled back.
Read-only state: because read-only transactions do not modify any data, read-only transactions can be optimized when there are read-only operations to improve running performance.
TransactionStatus:TransactionStatus represents the specific running state of the transaction. That is, the status information of the run time of the transaction can be obtained through TransactionStatus, and the transaction can be rolled back indirectly through this interface. This is because the interface inherits SavepointManager, while the SavepointManager interface supports segmented transaction control of the new SavePoint in JDBC3.0. Let's take a brief look at the methods included in SavepointManager.
Object createSavepoint (): create a SavePoint object to ensure that the transaction can be rolled back to the SavePoint, or you can call the releaseSavepoint () method to release a SavePoint that has been determined not to be used.
Void rollbackToSavepoint (Object var1): rolls back the transaction to a specific SavePoint, and the SavePoint being rolled back is automatically released.
Void releaseSavepoint (Object var1): release a SavePoint, and if the transaction commits, all Savepoints will be released.
In addition to the existing methods mentioned above, TransactionStatus extends methods that are not found in the following SavepointManager:
Boolean hasSavepoint (): determines whether the current transaction has created a SavePoint.
Boolean isNewTransaction (): determines whether the current transaction is a new transaction. If false is returned, the current transaction is an existing transaction, or the current operation is not running in the transaction environment.
Boolean isCompleted (): determines whether the current transaction has ended, whether the transaction has been committed or the transaction rollback task has ended.
Boolean isRollbackOnly (): determines whether the current transaction has been identified as rollback-only.
Void setRollbackOnly (): sets the current transaction to rollback-only. Roll back this transaction through the identity spring.
PlatformTransactionManager: let us know in other articles that JDBC transactions can only be committed or rolled back. The role of PlatformTransactionManager in spring is to do the above functions. Let's take a look at the methods involved in PlatformTransactionManager and their functions.
TransactionStatus getTransaction (TransactionDefinition var1): this method returns an existing transaction from the transaction environment based on the information defined by the transaction, or creates a new transaction and records the running state of the transaction with TransactionStatus.
Void commit (TransactionStatus var1): commits the transaction based on the state of the transaction, and if the transaction is already identified as rollback-only, this method performs a rollback operation.
Void rollback (TransactionStatus var1): rolls back the transaction, and if an exception is thrown by calling the commit () method, spring calls the rollback () method by default.
Above, we introduced the main transaction-related interfaces in spring. Let's continue to learn about the specific implementation of these interfaces in spring. In fact, spring provides different implementations for different persistence technologies. The related implementation classes are as follows:
Spring is through this implementation class to achieve spring transaction management, introduced so much, let's take a look at how to use code in spring, that is, declarative transaction management.
In this way our data source supports transaction management.
At this point, the study on "Spring's management and support of affairs" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.