How does Spring Transaction Management Work?
Spring defines an Interface PlatformTransactionManager. All Transaction Managers (e.g. JDBC, Hibernate, JMS, JTA, etc) implements this (SPI) providing Transaction Management.
How does PlatformTransactionManager work?
Spring provides different ways (AOP, Annotations, programmatic??) to call the PlatformTransactionManager's methods. Below is one example:
When a method is annotated as @Transactional (value="beanIDOfSomeTransactionManager")
Spring calls beanIDOfSomeTransactionManager.getTransaction, when the annotated call is started and commit/rollback at the end of that method.
Ok, this is fine, but what is the link between the Transaction Manager and the underlying connection/session?
This is interesting. This happens through ThreadLocal. E.g. in case of DataSourceTransactionManager for JDBC, getTransacation of this transaction manager adds the data-source in Thread Local (synchronizations). Now, in the actual actual code, we call Special Utility Method of Spring - Tempate/JdbcTemplate, which takes parameter of the datasource. This checks it in the ThreadLocal. Attached is the sample project.
How does JTA work?
How spring (declarative) transaction management works?
Transaction management would meaning managing the start and end (commit or rollback) of a transaction. Declarative would mean the ability to control this transaction boundary using some configuration(and not the actual code).
Spring does it using AOP. Using AOP, you define the function for which transaction needs to be managed. You also define the rule which will tell this AOP on when to commit or rollback.
So, when the "transaction function" is called, its AOP-proxy is being invoked. This proxy would start a transaction (on ??) and put the transaction context in the ThreadLocal. When this "transaction function" call is ended, AOP-proxy would call commit or rollback based on some configured rules (e.g. if some type of exception is thrown, rollback). This proxy executes this rollback and commit on the transaction context in ThreadLocal.
Now, how the AOP code(which is AOP Advice- Transaction advice) calls the transaction start/commit/rollback, since these would mean different for different sources( e.g. Hibernate, JDBC, JMS, etc). Thus, Spring has a concept of TransactionManager. The underlying source has its own defined transaction manager (e.g. Spring.DataSourceTranactionManager, Spring.HibernateTransactionManager, Spring.JMSTransactionManager). These transaction manager classes are supplied to AOP advice (tx advice) and call the respective(commit/rollback) using this transaction manager. The transaction managers uses underlying JDBC datasoruce/hibernate session/jms connection, etc to manipulate the the call (commit/rollback).
Now, how our code gets handle of the connection to perform activties? e.g. connection.createStatement, connection.getSession, etc? Answer is, we do not need to. Spring has provided template classes, whose core is "execute" method, which checks for the transaction context in Thread Local to perform transaction handling.
References:
1. http://static.springsource.org/spring/docs/2.0.8/reference/transaction.html