In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail about Java Spring AOP source code analysis in the transaction implementation principle is what, the quality of the article content is high, so Xiaobian share for everyone to make a reference, I hope you read this article after the relevant knowledge has a certain understanding.
Don't use Spring to manage transactions?
Let's first look at how various frameworks manage transactions without spring.
Using JDBC to Manage Transactions
Use Hibernate to manage transactions
The business logic and transaction code are coupled together and bound to the framework's specific api. When we switch to another framework, the code that controls transactions will have to be rewritten, and there is no guarantee that the replaced api will behave the same as the previous api.
Based on these problems, Spring abstracts some transaction-related top-level interfaces, we are interface-oriented programming, as long as we change the framework for specific implementation. It's kind of like the JDBC API.
Common api interface PlatformTransactionManager manages transactions TransactionDefinition defines the relevant attributes of transactions, such as isolation level, propagation behavior TransactionStatus saves transaction status
For different data access technologies, use the unused PlatformTransactionManager class
Data Access Technology PlatformTransactionManager Implementation Class JDBC/MybatisDataSourceTransactionManager HibernateHibernateTransactionManager JpaJpaTransactionManager JmsJmsTransactionManager Programmatic Transaction Management Using PlatformTransactionManager
Using TransactionTemplate
When we use PlatformTransactionManager directly to manage transactions, there are a lot of template code. For example, the business code executes normally, the transaction is committed, otherwise the transaction is rolled back. We can encapsulate this part of the template code into a template class, which is very convenient to use, as shown below
TransactionTemplate#execute method is a typical template method, as shown in the following figure
We can pass in the following two interface implementation classes to execute business logic: TransactionCallback (need to return execution results) or TransactionCallbackWithoutResult (need not return results)
declarative transaction management
In order to make the use more concise, Spring directly puts the execution of transaction code into the aspect, we only need to add a @Transactional annotation to the business code method, this way we most commonly use Ha
Use @Transactional annotation
At this point, we can set the transaction-related definition through the @Transactional annotation.
Attribute name Type description Default value (alias with transactionManager) String When there are more than one PlatformTransactionManager in the configuration file, use this attribute to specify which transaction manager to select Empty string ""propagation enumeration: propagation behavior of Propagation transactions REQUIREDissociation enumeration: isolation of Isolation transactions DEFAULTtimeoutint timeout of transactions. If the time limit is exceeded but the transaction has not completed, the transaction is automatically rolled back-1readOnlyboolean Specifies whether the transaction is a read-only transaction falserollForClass [] Empty array of exceptions requiring rollback {}rollbackForClassNameString[] Empty array of exception classes requiring rollback {} noRollbackForClass[] Empty array of exceptions requiring no rollback {}noRollbackForClassNameString[] Empty array of exception classes requiring no rollback {}
Source code analysis
We need to add @EnableTransactionManagement annotation to the configuration class to enable spring transaction management function. The main function of @EnableTransactionManagement is to inject a TransactionInterceptor interceptor to control transaction opening, submission or rollback.
ProxyTransactionManagementConfiguration
TransactionInterceptor#invoke
TransactionAspectSupport#invokeWithinTransaction
TransactionAspectSupport#createTransactionIfNecessary
When the transaction is opened, you can see the behavior of various propagation attributes.
AbstractPlatformTransactionManager#getTransaction
The propagation behavior of Spring transactions is defined in the Propagation enumeration class as follows
Support current transactions
REQUIRED: Join a transaction if it currently exists. If there is no current transaction, create a new one
SUPPORTS: Join a transaction if it currently exists. If there are no transactions currently, continue running non-transactionally
MANDATORY: Join a transaction if it currently exists. Throw an exception if there is no current transaction
Current transaction not supported
REQUIRES_NEW: Creates a new transaction and suspends the current transaction if one exists
NOT_SUPPORTED: Run in non-transactional mode, suspending the current transaction if one exists
NEVER: Runs in a non-transactional manner, throwing an exception if a transaction currently exists
other cases
NESTED: If a transaction currently exists, creates a transaction to execute as a nested transaction of the current transaction. If there is no transaction, this value is equivalent to REQUIRED.
A transaction initiated with NESTED is embedded in an outer transaction (if one exists), in which case the embedded transaction is not a separate transaction, it depends on the outer transaction. Commit an inner transaction only by committing an outer transaction; nested child transactions cannot commit separately
About Java Spring AOP source code analysis transaction implementation principle is what to share here, I hope the above content can have some help for everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
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.