Spring Source code parsing (I) Hibernate for Spring transaction control

Source: Internet
Author: User

 

As we all know, Spring's transaction control is implemented based on AOP. A method that declares transaction management (such as a Service method) will be intercepted during execution, the "additional" operations executed during interception are concentrated in:
Org. springframework. transaction. interceptor. TransactionInterceptor. invoke (MethodInvocation)
As a peripheral plane, this method is mainly responsible for starting a transaction before the execution of the target method and committing the transaction after the execution of the method.

 

Let's take a deeper look at how transactions are created. From the createTransactionIfNecessary () method, we can see that the main method to create a transaction is:

Org. springframework. transaction. support. AbstractPlatformTransactionManager. getTransaction (TransactionDefinition)

As an abstract class method, getTransaction () only processes some universal checks and settings. Both substantive transaction creation and transaction opening operations call abstract methods separately:

Org. springframework. transaction. support. AbstractPlatformTransactionManager. doGetTransaction ()
And
Org. springframework. transaction. support. AbstractPlatformTransactionManager. doBegin (Object, TransactionDefinition)

That is to say, these key tasks must be implemented by the specific transaction manager. For the hibernate Transaction Manager, the method to obtain the transaction object is as follows:

 

The procedure for starting a transaction is as follows:

 

The above is the code about the beginning of the transaction. Let's take a look at the code when the transaction is committed:
Similarly, from the commitTransactionAfterReturning () method, we can see that the method for executing transaction commit mainly uses callback.
Org. springframework. orm. hibernate3.HibernateTransactionManager. doCommit (DefaultTransactionStatus)
.

 

Supplement:
Methods
Org. springframework. transaction. support. TransactionSynchronizationManager. getResource (Object key)
As mentioned in the annotation of this method, it mainly finds the corresponding resource through the given key, especially when these resource instances are bound to the thread, that is, spring ensures that a key on a thread corresponds to a resource instance. Different threads bind different resource instances. For Hibernate, the key is sessionFactory and the resource is sessionHolder!
Author: bluishglc

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.