Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
A Harry
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
A Harry
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that Anthony, makes sense the way you put it!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic