Example usage for javax.persistence EntityTransaction getRollbackOnly

List of usage examples for javax.persistence EntityTransaction getRollbackOnly

Introduction

In this page you can find the example usage for javax.persistence EntityTransaction getRollbackOnly.

Prototype

public boolean getRollbackOnly();

Source Link

Document

Determine whether the current resource transaction has been marked for rollback.

Usage

From source file:com.pocketgorilla.stripesem.TransactionFilter.java

private void doAfter() {
    EntityManager em = provider.getEntityManager(false);
    if ((em != null) && (em.isOpen())) {
        EntityTransaction tx = em.getTransaction();
        if (tx.isActive()) {
            if (tx.getRollbackOnly()) {
                tx.rollback();/*from w w w .j a  v a  2s  .  c  o  m*/
                log.info("Rolled back persistence transaction.");
            } else {
                tx.commit();
                log.debug("Committed persistence transaction.");
            }
        }
        em.close();
        provider.removeEntityManager();
    }
}

From source file:com.appdynamics.loan.persistence.BasePersistenceImpl.java

/**
 * The method to update the serailizable business objects into the database.
 * /*  w w  w .  j a v  a 2s  .co m*/
 * @param object --
 *            serializable object
 * @throws PersistenceException
 */
@Transactional
public void update(final Serializable object) {
    EntityManager entityManager = getEntityManager();
    EntityTransaction txn = entityManager.getTransaction();
    txn.begin();
    try {
        entityManager.merge(object);
    } catch (Exception ex) {
        txn.rollback();
    } finally {
        if (!txn.getRollbackOnly()) {
            txn.commit();
        }
    }
}

From source file:com.appdynamics.loan.persistence.BasePersistenceImpl.java

@Transactional
public void delete(Serializable object) {
    EntityManager entityManager = getEntityManager();
    EntityTransaction txn = entityManager.getTransaction();
    txn.begin();/*  w  ww  .j av  a2 s.c o m*/
    try {
        entityManager.remove(object);
    } catch (Exception ex) {
        txn.rollback();
    } finally {
        if (!txn.getRollbackOnly()) {
            txn.commit();
        }
    }
}

From source file:com.appdynamics.loan.persistence.BasePersistenceImpl.java

@Transactional
public void save(final Serializable object) {
    EntityManager entityManager = getEntityManager();
    EntityTransaction txn = entityManager.getTransaction();
    txn.begin();/*  www . j ava2 s . c o m*/
    try {
        entityManager.persist(object);
    } catch (Exception ex) {
        txn.rollback();
    } finally {
        if (!txn.getRollbackOnly()) {
            txn.commit();
        }
    }

}

From source file:com.appdynamicspilot.persistence.BasePersistenceImpl.java

/**
 * The method to update the serailizable business objects into the database.
 * //from   www  . j  a  v  a 2s  . c om
 * @param object --
 *            serializable object
 * @throws PersistenceException
 */
@Transactional
public void update(final Serializable object) {
    EntityManager entityManager = getEntityManager();
    EntityTransaction txn = entityManager.getTransaction();
    txn.begin();
    try {
        entityManager.merge(object);
    } catch (Exception ex) {
        logger.error(ex);
        txn.rollback();
    } finally {
        if (!txn.getRollbackOnly()) {
            txn.commit();
        }
    }
}

From source file:com.appdynamicspilot.persistence.BasePersistenceImpl.java

@Transactional
public void delete(Serializable object) {
    EntityManager entityManager = getEntityManager();
    EntityTransaction txn = entityManager.getTransaction();
    txn.begin();/*  ww  w .  j ava  2 s.c o m*/
    try {
        entityManager.remove(object);
    } catch (Exception ex) {
        logger.error(ex);
        txn.rollback();
    } finally {
        if (!txn.getRollbackOnly()) {
            txn.commit();
        }
    }
}

From source file:com.appdynamicspilot.persistence.BasePersistenceImpl.java

@Transactional
public void save(final Serializable object) {
    EntityManager entityManager = getEntityManager();
    EntityTransaction txn = entityManager.getTransaction();
    txn.begin();//from ww w  .ja v  a 2 s .co  m
    try {
        entityManager.persist(object);
    } catch (Exception ex) {
        logger.error(ex);
        txn.rollback();
    } finally {
        if (!txn.getRollbackOnly()) {
            txn.commit();
        }
    }

}

From source file:name.livitski.tools.persista.TransactionalWork.java

/**
 * Performs the {@link #code database operations} provided by
 * the subclass in JPA transactional brackets. If there is currently
 * no active transaction at the entity manager, a new transaction
 * is started.//from w  w w . j  a  v a 2s  . c  om
 * If the code finishes normally and does not request the rollback,
 * the transaction is committed if it was started by this method
 * (local transaction).
 * If the implementor's code throws an exception or requests the
 * rollback, the local transaction is rolled back and any ongoing
 * transaction is marked
 * {@link EntityTransaction#setRollbackOnly() rollback-only}.
 * @param db the entity manager the operation will be performed with
 * @return <code>true</code> if the transaction has or may still
 * be committed
 * @throws AbstractStorageException indicates an error during
 * the operation
 * @throws RuntimeException any unchecked implementor's exceptions
 * will be rethrown
 * @see EntityManager#getTransaction()
 */
public boolean exec(final EntityManager db) throws AbstractStorageException {
    final EntityTransaction transaction = db.getTransaction();
    boolean commit = false;
    Exception status = null;
    boolean localTransaction;
    if (transaction.isActive())
        localTransaction = false;
    else {
        transaction.begin();
        localTransaction = true;
    }
    try {
        commit = code(db);
        return commit;
    } catch (AbstractStorageException fault) {
        status = fault;
        throw fault;
    } catch (RuntimeException fault) {
        status = fault;
        throw fault;
    } finally {
        if (!localTransaction) {
            if (commit)
                db.flush();
            else
                transaction.setRollbackOnly();
        } else if (!commit || transaction.getRollbackOnly()) {
            try {
                transaction.rollback();
            } catch (RuntimeException fault) {
                if (null != status)
                    log().error("Transaction rollback failed", fault);
                else
                    throw fault;
            }
        } else // commit local transaction
        {
            try {
                transaction.commit();
            } catch (RuntimeException fault) {
                throw fault;
            }
        }
    }
}

From source file:de.zib.gndms.logic.model.TaskAction.java

/**
 * Invokes a rollback on an entity transaction and a following {@code begin()},
 * only if it has been marked (using {@code setRollbackOnly()}).
 *
 * @param txParam a transaction to be rewinded
 *//*from  w ww  .  j  a va2s  . com*/
private void rewindTransaction(final EntityTransaction txParam) {
    if (txParam.isActive()) {
        if (txParam.getRollbackOnly()) {
            txParam.rollback();
            txParam.begin();
        }
    } else
        txParam.begin();
}

From source file:com.espirit.moddev.examples.uxbridge.newswidget.jpa.ArticleHandler.java

/**
 * Deletes every article older than the creationTime of the UXBEntity
 *
 * @param entity Entity containing the expireDate (= createTime of the entity)
 *///w ww.j  a v a2 s  .  c  o  m
public void cleanup(UXBEntity entity) throws Exception {

    EntityManager em = null;
    EntityTransaction tx = null;
    try {

        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();

        Query query = em.createQuery(new StringBuilder()
                .append("SELECT x FROM article x WHERE x.lastmodified<:expiredate ").toString());
        query.setParameter("expiredate", entity.getCreateTime());

        if (!query.getResultList().isEmpty()) {
            for (Object obj : query.getResultList()) {
                Article art = (Article) obj;
                em.remove(art);
            }
        }
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.setRollbackOnly();
            throw e;
        }
        logger.error("Failure while deleting from the database", e);
    } finally {
        if (tx != null && tx.isActive()) {
            if (tx.getRollbackOnly()) {
                tx.rollback();
            }
        }
        if (em != null) {
            em.close();
        }
    }

}