users@glassfish.java.net

Thread-safety of EJB references in GlassFish.

From: <glassfish_at_javadesktop.org>
Date: Wed, 07 May 2008 23:10:07 PDT

Hello,

In GlassFish, are EJB references injected by @EJB annotations thread safe?

I could not find any clear description about thread-safety of EJB references. So I guess that it depends on application server implementations.


According to the EJB spec., Stateless Session Beans can be accessed by multiple clients concurrently. Now, how about EJB references? For example, can we write codes like the following?

public class FooServlet extends HttpServlet {
    @EJB private MySessionBean myEJB;
    // Getter method to ensure visiblity of 'myEJB'.
    private synchronized MySessionBean getMyEJB() { return this.myEJB; }
    // HTTP Service method
    public void doGet(..) {
        getMyEJB().someMethod(...); // Accessing 'myEJB' without any synchronizations.
    }
}

Or, we have to "synchronize(myEJB)" or get EJB references for each HTTP requests?


Best regards,
- Ryosuke
[Message sent by forum member 'isana' (isana)]

http://forums.java.net/jive/thread.jspa?messageID=273260