cancel
Showing results for 
Search instead for 
Did you mean: 

Application-Managed Entity Manager fails to persist in Servlets

0 Kudos

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();

Accepted Solutions (1)

Accepted Solutions (1)

Vlado
Advisor
Advisor
0 Kudos

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

Answers (2)

Answers (2)

0 Kudos

Thanks for the answers! Works like a charm now Points awarded...

Message was edited by:

Clemens Fehr

adrian_goerler
Employee
Employee
0 Kudos

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