Let's make some basic statements.
- A transactional context is an environment in which some special properties (database session) are made available to the application runtime, which otherwise are not available. A transaction context is typically used to span a transaction.
- Spring uses AOP Proxies and XML metadata to achieve declarative transaction management.
- Annotations are used to indicate Transaction Propagation behavior for a particular method.
- Spring uses an interceptor mechanism to apply transactional methods.
Here I am reusing the example provided by @stacker above
MyClass{ @Transactional public void sequence() { method1(); method2(); } @Transactional void method1() { } @Transactional(propagation=Propagation.REQUIRES_NEW) void method2() { } }
You can also implement the same functionality using the xml configuration. Let's consider this as popular and widely used.
During deployment
- The Spring framework checks the xml configuration files (the famous
applicationContext.xml ) and, depending on the configuration, looks at the code for the @Transactional annotation (provided that the configuration is referred to as annotation-based). - After that, it generates AOP proxies for the methods marked for the transaction. In simple words, these proxies are nothing more than a wrapper around the appropriate methods.
- Within these wrapper methods, before and after the code of the transaction advisor is also generated depending on the configuration (namely, the propagation of the transaction).
- Now that these wrapper methods are called, the transaction advisor enters the image before and after the actual method call.,
Representing the same in pseudo code for the example above
ProxyMyClass{ MyClass myclass; . . . sequence(){ //Transaction Advisor code (Typically begin/check for transaction) myclass.sequence(); //Transaction Advisor code(Typically rollback/commit) } . . . }
This is how spring manages the transaction. However, a slight simplification.
Now, to answer your questions,
. How is spring known to commit database updates on a successful transaction? Is there a link to a spring implementation that does transaction management?
Whenever you call a method on a transaction, you actually call the proxy server that first executes the transaction advisor (which will start the transaction), then you call the actual business method, as soon as this completes, another transaction advisor (which depends on return path method, commit transaction, or rollback).
Since we have a transaction hierarchy: Transaction around the web request-> Transaction with Propagation = RequestNew for Method1-> Transaction with propagation = Required for Method2, how does spring perform transaction management to ensure that the transactions are executed in the right context with the correct order?
In the case of a transaction hierarchy, the spring structure generates the corresponding checks of the transaction advisor. For example, you mentioned
- for method 1 (RequestNew), the Transaction Code (or Transaction Advice) should always be to create a new transaction.
- for method2 (required), the Transaction Advisor code (or transaction advice) will check for an existing transaction and use it if it exists, or create a new transaction.
There is a spring page that summarizes these aspects very well.

Hope this helps.
source share