Environment :
JAVA EE 7
CDI 1.2
WildFly 8.2.0
Code :
I have a JAVA class having following method.
@SessionScoped
@Named("orgBean")
public class OrgBean{
@Transactional
public void doSomething(){
//EM.persist();
//call private method
innerMethod();
}
private void innerMethod(){
EM.persist(); //why is this working ?
}
}
1)The method doSomething() runs inside a transaction. This method calls another private method innerMethod().
2)The innerMethod() uses EM.persist() call.
Issue/Query :
1)How is EM.persist() working ?
2)I am trying to relate this to Spring framework. Since CDI uses proxies (method invocation OrgBean.doSomething will be through PROXY) and innerMethod is self invocation , how EM.persist() call will work since innerMethod() won't be running inside transaction ?
3)Please correct me if I am wrong.
The code in innerMethod()
runs inside the transaction started by the call to doSomething()
, and ending when the doSomething()
method returns.
doSomething()
method is calleddoSomething()
methoddoSomething()
does whatever it wants, including calling other methods. The proxy doesn't care, and can't even know about itdoSomething()
method returns to proxy's doSomething() method wrapperIn pseudo-code, the proxy basically does the following:
public void doSomething() {
startTransaction();
try {
orgBean.doSomething();
commitTransaction();
}
catch (RuntimeException e) {
rollbackTransaction();
throw e;
}
}
It's a bit more complex than that in reality, but you should get the idea.