We have:
@Transactional(propagation = Propagation.REQUIRED)
public class MyClass implementes MyInterface { ...
MyInterface has a single method: go()
.
When go() executes we start a new transaction which commits/rollbacks when the method is complete – this is fine.
Now let's say in go() we call a private method in MyClass that has @Transactional(propagation = Propagation.REQUIRES_NEW
. It seems that Spring "ignores" the REQUIRES_NEW annotation and does not start a new transaction. I believe this is because Spring AOP operates on the interface level (MyInterface) and does not intercept any calls to MyClass methods. Is this correct?
Is there any way to start a new transaction within the go() transaction? Is the only way to call another Spring managed bean that has transactions configured as REQUIRES_NEW?
Update: Adding that when clients execute go()
they do so via a reference to the interface, not the class:
@Autowired
MyInterface impl;
impl.go();
Best Answer
From the Spring reference 2.5:
So Spring ignores
@Transactional
annotation on non-public methods.Also,
So even if you make your method
public
, calling it from within a method of same class will not start a new transaction.You can use
aspectj
mode in transaction settings so that the transaction related code is weaved in the class and no proxy is created at runtime.See the reference document for more details.
Another possible way of doing this is fetching the spring proxy of the class in the class itself and call methods on it rather than
this
: