Forums Register Login

Calling a transactional method from non-transactional method in EJB

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1629 times.
Similar Threads
Transactional context
Reentrant okay in BDOM?
Can a MailSender participate in a transaction?
RemoveException or EJBException
EJB Notes I prepared during my preparation for SCBCD
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 03:59:13.