Is it safe to inject an EJB into a servlet as an instance variable?

前端 未结 4 1245

We all know that in the web tier there is the possibility that only a single instance of a given Servlet exists which services multiple requests. This can lead to threading issu

相关标签:
4条回答
  • 2021-02-07 21:55

    It's a mixed bag.

    Stateless session beans may be injected and are safe. This is because even if a single instance of a stub is used, access to the methods will be serialized by the container.

    I think what inferreddesign says is not true. It doesn't matter if the stateless session bean uses a persistence context. Only one caller will ever access a single bean instance at the same time, so even though the persistence context is not thread safe, the EJB guards against multiple access to it. Think of it as if every session bean method has the synchronized keyword applied to it.

    The main problem with injecting an EJB in a Servlet I think is performance. The single stub instance will become a major area of contention when multiple requests are queuing up while waiting for a session bean method to be executed for them.

    0 讨论(0)
  • 2021-02-07 21:56

    It is safe to inject an EJB in a Servlet as a Servlet instance variable, as long as the EJB is Stateless. You MUST NEVER inject a Stateful Bean in a Servlet.

    You must implement your EJB stateless in that it doesn't hold any instance variable which itself holds a stateful value (like Persistence Context). If you need to use the persistence context, then you must get an instance of it IN the methods of the EJB. You can do that by having a PersistenceContextFactory as a EJB instance Variable and then you get an instance of the entity manager from the Factory in the method of the EJB.

    The PersistenceContextFactory is thread-safe, thus it can be injected in an instance variable.

    As long as you comply to the above mentioned rules, it should be thread-safe to inject a Stateless Bean in a Servlet

    0 讨论(0)
  • 2021-02-07 22:07

    Your reference "Don't inject to servlets" mentions nothing about ejbs or @ejb annotation. It talks about not thread safe objects such as PersistenceContext.

    Per EJB spec you can access ejbs from variety of remote clients including servlets (EJB 3.0 Specification (JSR-220) - Section 3.1). Injecting ejb using @EJB annotation is a method of obtaining EJB interface via dependency injection (section 3.4.1) which is alternative to looking up ejb objects in the JNDI namespace. So there is nothing special about @EJB annotation with respect to EJBs obtained.

    So, based on EJB 3.0 Spec, it's a standard practice to obtain ejbs from servlets using @EJB annotation.

    0 讨论(0)
  • 2021-02-07 22:13

    I think the simple answer is that you aren't guaranteed that it is safe.

    The reason for this is that there is nothing explicit in the EJB specification that says EJB home interfaces have to be thread safe. The spec outlines the behaviour of the server side part only. What you will probably find is that the client skeletons are actually thread safe but you would need to look at how they are implemented by the library you are using. The annotation part will just expand into a service locator so that doesn't buy you anything.

    0 讨论(0)
提交回复
热议问题