You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@geronimo.apache.org by tornike <to...@gmail.com> on 2009/08/31 12:48:13 UTC
EntityManager & Servlets
Hi,
I was trying to follow the examples that are given in the Geronimo
documentation to understand JPA. In lot of sources I have read that
EntityManager is not thread safe and it should not be injected into the
servlet. (container managed one) However in the example of ejb-JPA
http://cwiki.apache.org/GMOxDOC21/container-managed-persistence-with-jpa.html
http://cwiki.apache.org/GMOxDOC21/container-managed-persistence-with-jpa.html
it is being injected in the servlet.
[
public class Test extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
@PersistenceContext(unitName="AccountUnit")
private EntityManager em;
]
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?
Thanks.
--
View this message in context: http://www.nabble.com/EntityManager---Servlets-tp25221488s134p25221488.html
Sent from the Apache Geronimo - Users mailing list archive at Nabble.com.
Re: EntityManager & Servlets
Posted by tornike <to...@gmail.com>.
Thanks for replying to my question.
I have figured out how EJB works with servlets now, I am not a very
experienced developer and I learn through examples. I inject session beans
(@EJB) into my servlets and database manupulations are kept in session
beans.
While reading about these issues I came across Java 6EE where as I
understood EJBs are introduced in web container as web beans. Should be
easier to figure out for newcomers like me:)
Thanks
Jay D. McHugh-3 wrote:
>
> 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
> persistence.
>
> 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,
>
> Jay
>
> 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 <25...@talk.nabble.com> 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
>>>>
>>>> http://cwiki.apache.org/GMOxDOC21/container-managed-persistence-with-jpa.html
>>>>
>>>> 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 this message in context: http://www.nabble.com/EntityManager---Servlets-tp25221488s134p25461383.html
Sent from the Apache Geronimo - Users mailing list archive at Nabble.com.
Re: EntityManager & Servlets
Posted by Fredrik Jonson <fr...@myrealbox.com>.
Jay D. McHugh wrote:
> Geronimo should not allow you to add container managed entity
> managers to servlets. They would not be thread safe.
Then please examine the example again. You're correct that there's a
stateless bean acting as a accessor in _part_ of the example. But
there is also a servlet class called Test - search for Test.java in
that page.
http://cwiki.apache.org/GMOxDOC21/container-managed-persistence-with-jpa.html
You'll see that the servlet Test has a injected EntityManager. Now, I
didn't notice before but the servlet actually only accesses the entity
manager in a UserTransaction which it looks up per request. I'm not
sure if and how the transaction would make access to the single
instance entity manager thread safe though?
The blog linked below seems to suggest that even when using user
transactions - which on the other side seems very servlet injection
friendly - it is still wrong to share a entity manager instance in
servlets.
http://weblogs.java.net/blog/ss141213/archive/2005/12/dont_use_persis_1.html
--
Fredrik Jonson
Re: EntityManager & Servlets
Posted by "Jay D. McHugh" <ja...@gmail.com>.
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
persistence.
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,
Jay
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 <25...@talk.nabble.com> 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
>>>
>>> http://cwiki.apache.org/GMOxDOC21/container-managed-persistence-with-jpa.html
>>>
>>> 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
>>
>>
>>
>
Re: EntityManager & Servlets
Posted by tornike <to...@gmail.com>.
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 <25...@talk.nabble.com> 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
>>
>> http://cwiki.apache.org/GMOxDOC21/container-managed-persistence-with-jpa.html
>>
>> 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 this message in context: http://www.nabble.com/EntityManager---Servlets-tp25221488s134p25226546.html
Sent from the Apache Geronimo - Users mailing list archive at Nabble.com.
Re: EntityManager & Servlets
Posted by Fredrik Jonson <fr...@myrealbox.com>.
In <25...@talk.nabble.com> 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
> http://cwiki.apache.org/GMOxDOC21/container-managed-persistence-with-jpa.html
>
> 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