Hi all,
In the helloworld example from hibernate tutorial, there is the following code to make two transactions (two units of work):
Code:
// First unit of work
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
(...)
tx.commit();
em.close();
// Second unit of work
EntityManager newEm = emf.createEntityManager();
EntityTransaction newTx = newEm.getTransaction();
newTx.begin();
(...)
newTx.commit();
newEm.close();
Is this mandatory or one can use always the same EntityManager object and create only new Transactions? Which is the best way?
Code:
(it would be like this)
// First unit of work
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
(...)
tx.commit();
//em.close();
// Second unit of work
//EntityManager newEm = emf.createEntityManager();
EntityTransaction newTx = newEm.getTransaction();
newTx.begin();
(...)
newTx.commit();
(more transactions....)
em.close();
Thanks in advance,
Rui