on 07-25-2007 9:38 AM
I don't get Application-Managed Entity Managers to work from servlet code. Does anyone know what I am doing wrong or if this is a bug? (NW7.1 SR1)
The following servlet code which uses a container managed EM works fine:
// @PersistenceUnit
// private EntityManagerFactory emf;
@PersistenceContext
public EntityManager em;
@Resource
UserTransaction utx;
protected void doPost(...) throws ServletException {
//EntityManager em = emf.createEntityManager();
Part p = new Part();
p.setDescription("description");
p.setName("firstPartName");
p.setId(3L);
utx.begin();
em.persist(p);
utx.commit();
while very similar code using application managed code does not work. It does not throw any exception, but simply does not persist anything to DB...
@PersistenceUnit
private EntityManagerFactory emf;
// @PersistenceContext
// public EntityManager em;
@Resource
UserTransaction utx;
protected void doPost(...) throws ServletException {
EntityManager em = emf.createEntityManager();
Part p = new Part();
p.setDescription("description");
p.setName("firstPartName");
p.setId(3L);
utx.begin();
em.persist(p);
utx.commit();
em.close();
Hi Clemens,
It's not a bug You have simply missed to call
em.joinTransaction();
after
utx.begin();
According to the javadoc of the EntityManager interface (section 3.1.1 of the JPA spec),
<i>"This method should be called on a JTA application
managed EntityManager that was created outside the scope
of the active transaction to associate it with the current
JTA transaction."</i>
Another option would be to call
utx.begin();
before
EntityManager em = emf.createEntityManager();
HTH!
-- Vladimir
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for the answers! Works like a charm now Points awarded...
Message was edited by:
Clemens Fehr
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Clemens,
in your first case, please be aware that you should not inject an entity manager into a servlet, as an <b>entity manager is not threadsafe</b>. Injecting an emf into a Servlet fine - entity manager factories are threadsafe.
In your second case, the em is not participating in the user transaction.
If you create the entity manager outside the user transaction, you have got to explicitly invoke <b>em.joinTransaction() </b> once the user transactgion is active:
<b>EntityManager em = emf.createEntityManager();</b>
Part p = new Part();
p.setDescription("description");
p.setName("firstPartName");
p.setId(3L);
<b>utx.begin();</b>
<b>em.joinTransaction();</b>
em.persist(p);
utx.commit();
em.close();
Alternatively, you can create the em inside the user transaction:
Part p = new Part();
p.setDescription("description");
p.setName("firstPartName");
p.setId(3L);
<b>utx.begin();
EntityManager em = emf.createEntityManager();</b>
em.persist(p);
utx.commit();
em.close();
Best regards,
Adrian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
78 | |
10 | |
9 | |
7 | |
6 | |
6 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.