aspose file tools*
The moose likes Struts and the fly likes Using EJB's from Action method - is this safe? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Using EJB Watch "Using EJB New topic
Author

Using EJB's from Action method - is this safe?

A Harry
Ranch Hand

Joined: Jan 23, 2002
Posts: 124
I have an Action class that calls methods on an EJB (stateless session bean acting as session facade).

To improve performance I'm thinking to store the reference to the EJB object (i.e stub) as an instance variable like so -

public class TestAction extends Action
{
EJB1Local oEJB1 = null;

public TestAction ()
{
EJB1LocalHome home = ... get home object
oEJB1 = homeDataEngine.create();
}

then when each time the action's called do this -

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
{
oEJB1.method1(........);
oEJB1.method1(........);
}

I can't determine from the EJB doc's if this is safe or not - should I just be storing the home ref (EJB1LocalHome) & doing a .create everytime before I want to use it or what?

Any ideas?

Hopefully I've explained it ok?

thanks

harry
Anthony Watson
Ranch Hand

Joined: Sep 25, 2003
Posts: 327
You can't use the same EJBObject in multiple threads at the same time. Like you said, you should cache only the EJBHome object and call create() every time your action is executed. Better yet, you could use the Service Locator design pattern to cache your EJBHome references in one centralized location.


Anthony W.<br />MCP, SCJP 1.4, SCJD, SCWCD 1.3, SCWCD 1.4, SCBCD
A Harry
Ranch Hand

Joined: Jan 23, 2002
Posts: 124
Thanks for that Anthony, makes sense the way you put it!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Using EJB's from Action method - is this safe?