Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling a transactional method from non-transactional method in EJB  RSS feed

 
Ahmed Basheer
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In EJB 3.0 Stateless session bean with CMT how can transactional method be called from non-transactional method of the same bean. Obviously a call has to to go through the container for called method to get transaction context. I tried using sessionContext.getLocalObject and tried casting it to Local interface but it throws ClassCastException. I did something like this.

MYEJB implements MYEJBLocal, MYEJBRemote
{

@Resource
SessionContext ctx;

//TransactionAttributeType=NEVER
method1()
{

MYEJBLocal local = (MYEJBLocal)ctx.getEJBLocalObject(); // throws exception as shown below
local.method2(); // doesn't reach here
}

//TransactionalAttributeType=REQUIRED
method2()
{
}

}


java.lang.IllegalStateException: java.lang.ClassCastException: $Proxy274 cannot be cast to javax.ejb.EJBLocalObject
at org.jboss.ejb3.BaseSessionContext.getEJBLocalObject(BaseSessionContext.java:330)

Any insights are appreciated,
Ahmed
 
Musab Al-Rawi
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the API.
you can't perform that type of casting as the method returns an EJBLocalObject.

I can't see why there would be a problem with transaction if you just call method2() directly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!