geronimo-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jay D. McHugh" <>
Subject Re: EntityManager & Servlets
Date Tue, 15 Sep 2009 18:45:03 GMT
Sorry for the late reply to this.

Geronimo should not allow you to add container managed entity managers
to servlets.  They would not be thread safe.

I haven't looked at the example in a while - but I believe it is using a
stateless session bean to access the entity manager.  That is perfectly
acceptable (and correct)...

I just took a look at the example and it does in fact use a stateless
bean (AccountBean).  So, that is why it is able to use container managed

I don't think that it would actually make projects simpler to move the
data access logic into each of the servlets that need it.  For me at
least, centralizing the logic into session beans means that I am forced
to consider exactly what I will be doing with the data and only write
the code once to do it.

Hope that helps,


tornike wrote:
> Yes, but if it's in their example maybe it's OK for Geronimo to use container
> managed EntityManager with the servlet and there won't be any threading
> issues. If it's true then we wouldn't need any EJB's in the project and
> simply communicate to JPA entities even when using container managed
> EntityManager. As far as I know this method is discouraged but if it's ok to
> do in Geronimo it would greatly simplify many projects. 
> Fredrik Jonson-3 wrote:
>> In <> tornike wrote:
>>>  EntityManager is not thread safe and it should not be injected into the
>>>  servlet. (container managed one) However in the example of ejb-JPA 
>>>  I have tested the example works without problems so are there conditions
>>>  when it can be done or am I understanding it the wrong way?
>> I'd say the example is wrong or at least careless. So I'm also interested
>> if someone thinks otherwise.
>> If I would have done it I'd inject a EntityManagerFactory in the servlet
>> and
>> acquired a new EntityManager from the pool for each request to doGet
>> method:
>> public class Example extends HttpServlet {
>>   @PersistenceUnit
>>   private EntityManagerFactory emf;
>>   @Override
>>   protected void doGet(HttpServletRequest request, HttpServletResponse
>> response)
>>     throws ServletException, IOException {
>>       EntityManager em = emf.createEntityManager();
>>       // do stuff
>> -- 
>> Fredrik Jonson

View raw message