Example usage for javax.persistence EntityTransaction isActive

List of usage examples for javax.persistence EntityTransaction isActive

Introduction

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

Prototype

public boolean isActive();

Source Link

Document

Indicate whether a resource transaction is in progress.

Usage

From source file:test.unit.be.fedict.eid.applet.beta.PersistenceTest.java

@After
public void tearDown() throws Exception {
    EntityTransaction entityTransaction = this.entityManager.getTransaction();
    LOG.debug("entity manager open: " + this.entityManager.isOpen());
    LOG.debug("entity transaction active: " + entityTransaction.isActive());
    if (entityTransaction.isActive()) {
        if (entityTransaction.getRollbackOnly()) {
            entityTransaction.rollback();
        } else {/*from  w ww  . j  av  a2s  .c o m*/
            entityTransaction.commit();
        }
    }
    this.entityManager.close();
}

From source file:org.apache.ranger.audit.destination.DBAuditDestination.java

private boolean commitTransaction() {
    boolean ret = false;
    EntityTransaction trx = null;

    try {//from   w  w w.j a  v  a2 s . c  o m
        trx = getTransaction();

        if (trx != null && trx.isActive()) {
            trx.commit();
            ret = true;
        } else {
            throw new Exception("trx is null or not active");
        }
    } catch (Throwable excp) {
        logger.error("DBAuditDestination.commitTransaction(): failed", excp);

        cleanUp(); // so that next insert will try to init()
    } finally {
        clearEntityManager();
    }

    return ret;
}

From source file:org.apache.ranger.audit.destination.DBAuditDestination.java

private boolean rollbackTransaction() {
    boolean ret = false;
    EntityTransaction trx = null;

    try {/*from ww  w.j  a va  2 s  .co m*/
        trx = getTransaction();

        if (trx != null && trx.isActive()) {
            trx.rollback();
            ret = true;
        } else {
            throw new Exception("trx is null or not active");
        }
    } catch (Throwable excp) {
        logger.error("DBAuditDestination.rollbackTransaction(): failed", excp);

        cleanUp(); // so that next insert will try to init()
    } finally {
        clearEntityManager();
    }

    return ret;
}

From source file:it.infn.ct.futuregateway.apiserver.resources.observers.TaskObserver.java

@Override
public final void update(final Observable obs, final Object arg) {
    if (!(obs instanceof Task)) {
        log.error("Wrong abject associated with the oserver");
    }//from ww  w . ja  va2s.c o m
    Task t = (Task) obs;
    if (t.getId() == null || t.getStatus() == null) {
        return;
    }
    log.debug("Task " + t.getId() + " updated");
    if (t.getStatus().equals(Task.STATUS.WAITING) && t.getApplicationDetail() != null) {
        if (t.getInputFiles() != null) {
            for (TaskFile tf : t.getInputFiles()) {
                if (tf.getStatus().equals(TaskFile.FILESTATUS.NEEDED)) {
                    return;
                }
            }
        }
        t.setStatus(Task.STATUS.READY);
        submit(t);
    }
    EntityManager em = emf.createEntityManager();
    EntityTransaction et = em.getTransaction();
    try {
        et.begin();
        em.merge(t);
        et.commit();
    } catch (RuntimeException re) {
        log.error("Impossible to update the task!");
        log.error(re);
        if (et != null && et.isActive()) {
            et.rollback();
        }
    } finally {
        em.close();
    }
}

From source file:com.busimu.core.dao.impl.UserMngDaoPolicyJpaImpl.java

/**
 * {@inheritDoc}//from  w ww. ja  v a  2s .  c  om
 */
@Override
public User activateUser(User user, String licenseCode) {
    EntityManager em = ((EntityManagerHolder) TransactionSynchronizationManager.getResource(emf))
            .getEntityManager();
    EntityTransaction tx = em.getTransaction();
    License l = getLicenseByCode(em, licenseCode);
    if (l != null) {
        if (!tx.isActive()) {
            tx.begin();
        }
        user.setType(User.Type.valueOf(l.getType().name()));
        user.setStatus(User.Status.ACTIVE);
        user = em.merge(user);
        em.remove(l);
        tx.commit();
    }
    return user;
}

From source file:org.apache.ranger.audit.provider.DbAuditProvider.java

private boolean isInTransaction() {
    EntityTransaction trx = getTransaction();

    return trx != null && trx.isActive();
}

From source file:it.infn.ct.futuregateway.apiserver.v1.TaskService.java

/**
 * Removes the task. Task is deleted and all the associated activities and
 * or files removed./*from w ww. ja v  a 2 s .  com*/
 *
 * @param id Id of the task to remove
 */
@DELETE
public final void deleteTask(@PathParam("id") final String id) {
    Task task;
    EntityManager em = getEntityManager();
    try {
        task = em.find(Task.class, id);
        if (task == null) {
            throw new NotFoundException();
        }
        EntityTransaction et = em.getTransaction();
        try {
            et.begin();
            em.remove(task);
            et.commit();
        } catch (RuntimeException re) {
            if (et != null && et.isActive()) {
                et.rollback();
            }
            log.error(re);
            log.error("Impossible to remove the task");
            em.close();
            throw new InternalServerErrorException("Errore to remove " + "the task " + id);
        }
        try {
            Storage store = getStorage();
            store.removeAllFiles(Storage.RESOURCE.TASKS, id);
        } catch (IOException ex) {
            log.error("Impossible to remove the directory associated with " + "the task " + id);
        }
    } catch (IllegalArgumentException re) {
        log.error("Impossible to retrieve the task list");
        log.error(re);
        throw new BadRequestException("Task '" + id + "' has a problem!");
    } finally {
        em.close();
    }
}

From source file:org.apache.ranger.audit.provider.DbAuditProvider.java

private boolean beginTransaction() {
    EntityTransaction trx = getTransaction();

    if (trx != null && !trx.isActive()) {
        trx.begin();/* w  w  w . ja  va 2s.  co m*/
    }

    if (trx == null) {
        LOG.warn("DbAuditProvider.beginTransaction(): trx is null");
    }

    return trx != null;
}

From source file:it.infn.ct.futuregateway.apiserver.v1.InfrastructureCollectionService.java

/**
 * Register a new infrastructure./*w ww .j av a  2s .  co m*/
 *
 * @param infra The infrastructure to register
 * @return The registered infrastructure
 */
@POST
@Status(Response.Status.CREATED)
@Consumes({ MediaType.APPLICATION_JSON, Constants.INDIGOMIMETYPE })
@Produces(Constants.INDIGOMIMETYPE)
public final Infrastructure createInfrastructure(final Infrastructure infra) {
    Date now = new Date();
    infra.setDateCreated(now);
    EntityManager em = getEntityManager();
    EntityTransaction et = null;
    try {
        et = em.getTransaction();
        et.begin();
        em.persist(infra);
        et.commit();
        log.debug("New infrastructure registered: " + infra.getId());
    } catch (RuntimeException re) {
        if (et != null && et.isActive()) {
            et.rollback();
        }
        log.error("Impossible to create the infrastructure");
        log.error(re);
    } finally {
        em.close();
    }
    return infra;
}

From source file:it.infn.ct.futuregateway.apiserver.v1.InfrastructureCollectionService.java

/**
 * Retrieve the application list./* w w w  .jav  a2  s.  c  om*/
 * The fields not requested for the list are cleaned.
 * @return The list of applications
 */
private List<Infrastructure> retrieveInfrastructureList() {
    List<Infrastructure> lstInfras;
    EntityManager em = getEntityManager();
    EntityTransaction et = null;
    try {
        et = em.getTransaction();
        et.begin();
        lstInfras = em.createNamedQuery("infrastructures.all", Infrastructure.class).getResultList();
        et.commit();
    } catch (RuntimeException re) {
        if (et != null && et.isActive()) {
            et.rollback();
        }
        log.error("Impossible to retrieve the infrastructure list");
        log.error(re);
        throw new RuntimeException("Impossible to access the " + "infrastructures list");
    } finally {
        em.close();
    }
    if (lstInfras != null) {
        for (Infrastructure in : lstInfras) {
            in.setDescription(null);
            in.setParameters(null);
        }
    }
    return lstInfras;
}