File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Creating another Instance of same Session EJB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Creating another Instance of same Session EJB" Watch "Creating another Instance of same Session EJB" New topic
Author

Creating another Instance of same Session EJB

Gus Winds
Greenhorn

Joined: Feb 04, 2003
Posts: 15
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 !!

thx
Singh
Brian Tinnel
Ranch Hand

Joined: Aug 25, 2003
Posts: 69
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:
Gus Winds
Greenhorn

Joined: Feb 04, 2003
Posts: 15
Thx a bunch !!
Gus Winds
Greenhorn

Joined: Feb 04, 2003
Posts: 15
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 ?
Brian Tinnel
Ranch Hand

Joined: Aug 25, 2003
Posts: 69
I think the only difference might be performance. I don't believe that the end result would be any different from a transaction standpoint.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating another Instance of same Session EJB
 
Similar Threads
Understandly entities ejb life-cicle deeply... need some help.
how to access already created bean in jsp
rendered problem
singleton stateless session bean
Can i create a session bean without using create() method??