I recently inherited a code in which there is stateless session bean's method is creating a new instance ( using home.create ) of the same bean. And then on the new bean it is calling another method. Can someone think of why this could be useful ? There has been a comment left that this was done so that method could be run in a separate transaction !!
Let's say you have a Stateless Session Bean with two methods methodA and methodB. Both have RequiresNew as their transaction attribute.
If a client calls methodA or methodB, then a new transaction will be started (because of the RequiresNew). However, if methodA calls methodB directly, a new transaction is not created. That is why the other person probably created a new object, so that the call to methodB would get it's own new transaction.
A simpler method of doing that would be to use the getEJBObject method of the SessionContext to object the remote interface, or getEJBLocalObject to obtain the local interface and use that interface to call the other method. That way the container will intercept the call and do the necessary transactional actions.
For example. If I have a remote interface called TestRemote and my ejb class is called TestBean I could do this:
What would be the difference between using the sessionContext and getting the EjbOjbect vs. Creating a new new instance using EJbHome.create(). How would that affect the transaction if at all ? Will there be any issues with performance at all ?