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