Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @Transactional method - participating transaction

in one dao I have 2 @Transactional methods.

if i do not provide any explicit properties,

then what will happen, if

I run one method in the body of another?

Both methods will run within THE SAME ONE TRANSACTION?

like image 608
EugeneP Avatar asked May 19 '10 11:05

EugeneP


People also ask

What does @transactional do in Spring?

The @Transactional annotation makes use of the attributes rollbackFor or rollbackForClassName to rollback the transactions, and the attributes noRollbackFor or noRollbackForClassName to avoid rollback on listed exceptions. The default rollback behavior in the declarative approach will rollback on runtime exceptions.

What does the @transactional annotation mean?

The @Transactional annotation is metadata that specifies that an interface, class, or method must have transactional semantics; for example, "start a brand new read-only transaction when this method is invoked, suspending any existing transaction".

Can @transactional annotation only be used at class level?

Annotation Type Transactional. Describes a transaction attribute on an individual method or on a class. When this annotation is declared at the class level, it applies as a default to all methods of the declaring class and its subclasses.


1 Answers

Proxies in Spring AOP

When using Transactional, you're dealing with proxies of classes, so in this scenario:

@Transactional
public void doSomeThing(){ // calling this method targets a proxy

    doSomeThingElse(); // this method targets the actual class, not the PROXY,
                       // so the transactional annotation has no effect
}

@Transactional
public void doSomeThingElse(){
}

you are calling the proxy from outside, but the second method call is made from inside the proxied object and therefor has no transactional support. So naturally, they run in the same transaction, no matter what the values of the @Transactional annotation in the second method are

so if you need separate transactions, you have to call

yourservice.doSomething();
yourservice.doSomethingElse();

from outside.

The whole scenario is explained pretty well in the chapter Spring AOP > Understanding AOP proxies, including this "solution":

Accessing the Current AOP Proxy object from the inside

public class SimplePojo implements Pojo {

   public void foo() {
      // this works, but... gah!
      ((Pojo) AopContext.currentProxy()).bar();
   }

   public void bar() {
      // some logic...
   }
}
like image 137
Sean Patrick Floyd Avatar answered Sep 17 '22 22:09

Sean Patrick Floyd