-1

This code work fine and deployed. But after some time or some days the flushed data not reflected in DB

 protected boolean update(Object entity) {
    EntityManager entityManager = null;
    try {
        entityManager = this.createEntityManager();
        EntityTransaction tx = entityManager.getTransaction();
        try {
            tx.begin();
            entityManager.merge(entity);
            entityManager.flush();
            tx.commit();
            return true;
        } catch (Exception e) {
            logger.error("Exception thrown in update(): " + e.getMessage());
            tx.rollback();
            return false;
        }
    } catch (Exception e1) {
        logger.error("Exception thrown in create(): " + e1.getMessage(), e1);        
        return false;
    } finally {
        if (entityManager != null && entityManager.isOpen()) {
            this.closeEntityManager(entityManager);
        }
    }
}

this my parent dao update method.

tx in advance.

Uppicharla
  • 494
  • 8
  • 18
kannan
  • 11
  • 1
  • 3
  • 1
    flush call is pointless, since the commit will flush. Look in the LOG for your JPA provider for what happens at commit (or flush). Also you should add a finally to your try so that you can rollback in the finally (and NOT in the catch) – Neil Stockton Feb 06 '17 at 08:59
  • there is no error in update transaction but not reflect in DB – kannan Feb 06 '17 at 09:11
  • which is not what I asked. Your finally should be on the inner try, so then you always rollback with an Error being thrown. And the LOG for your JPA provider would tell you what happens (SQL invoked etc). aka DEBUGGING – Neil Stockton Feb 06 '17 at 09:12
  • @NeilStockton i implement the tx.rollback with in the finally block. that always failed to rollback. the problem only on related entity update such as if we update a user entity that will works good but a relation list department new added departments not reflect in DB. i am stuck in this problem. – kannan Feb 08 '17 at 11:13

1 Answers1

0

Why are you creating a new entityManager on each update ?

According the documentation :

If this method is called when the entity manager is joined to an active transaction, the persistence context remains managed until the transaction completes.

https://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#close--

  • that line of docs is for when the transaction is JTA, i.e UserTransaction. The OP is using a RESOURCE_LOCAL transaction (since it has calls to EntityTransaction.commit). Creating a new EM for all updates is not a problem in itself since it is cheap. – Neil Stockton Feb 06 '17 at 09:01
  • 1
    Can we have more informations on the relying database ? http://stackoverflow.com/questions/8105618/entitymanager-does-not-write-to-database – Sylvain Leroy Feb 06 '17 at 09:05