Example usage for javax.persistence EntityTransaction rollback

List of usage examples for javax.persistence EntityTransaction rollback

Introduction

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

Prototype

public void rollback();

Source Link

Document

Roll back the current resource transaction.

Usage

From source file:br.com.blackhouse.internet.bindings.intercept.TransactionalInterceptor.java

@AroundInvoke
public Object invoke(InvocationContext context) throws Exception {
    EntityTransaction transaction = entityManager.getTransaction();

    try {/* ww  w  .  j a  va2 s .  co  m*/
        if (!transaction.isActive()) {
            transaction.begin();
        }

        return context.proceed();

    } catch (Exception e) {
        logger.error("Exception in transactional method call", e);

        if (transaction != null) {
            transaction.rollback();
        }

        throw e;

    } finally {
        if (transaction != null && transaction.isActive()) {
            transaction.commit();
        }
    }

}

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

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

    try {//w  w w  .  ja va2 s. c  o  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  .j  ava  2 s . co  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: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 va 2 s .  co  m
 *
 * @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:de.iai.ilcd.model.dao.AbstractDao.java

/**
 * Default persist//  w w  w. j av  a 2s.  co  m
 * 
 * @param obj
 *            object to persist
 * @throws PersistException
 *             on errors (transaction is being rolled back)
 */
public void persist(T obj) throws PersistException {
    if (obj == null) {
        return;
    }
    EntityManager em = PersistenceUtil.getEntityManager();
    EntityTransaction t = em.getTransaction();

    try {
        t.begin();
        em.persist(obj);
        t.commit();
    } catch (Exception e) {
        t.rollback();
        throw new PersistException(e.getMessage(), e);
    }
}

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

/**
 * Register a new infrastructure.// w ww  . j a v  a2  s. c o 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 ww .  j  a va 2s .c  o  m*/
 * 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;
}

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

/**
 * Retrieve the application list./*from www. j av a  2s.  c o m*/
 * The fields not requested for the list are cleaned.
 * @return The list of applications
 */
private List<Application> retrieveApplicationList() {
    List<Application> lstApps;
    EntityManager em = getEntityManager();
    EntityTransaction et = null;
    try {
        et = em.getTransaction();
        et.begin();
        lstApps = em.createNamedQuery("applications.all", Application.class).getResultList();
        et.commit();
    } catch (RuntimeException re) {
        if (et != null && et.isActive()) {
            et.rollback();
        }
        log.error("Impossible to retrieve the Application list");
        log.error(re);
        throw new RuntimeException("Impossible to access the " + "application list");
    } finally {
        em.close();
    }
    if (lstApps != null) {
        for (Application ap : lstApps) {
            ap.setDescription(null);
            ap.setParameters(null);
        }
    }
    return lstApps;
}

From source file:de.iai.ilcd.model.dao.AbstractDao.java

/**
 * Default merge/*w  w w .ja va  2s  . com*/
 * 
 * @param obj
 *            object to merge
 * @throws MergeException
 *             on errors (transaction is being rolled back)
 * @return managed object
 */
public T merge(T obj) throws MergeException {
    if (obj == null) {
        return null;
    }
    EntityManager em = PersistenceUtil.getEntityManager();
    EntityTransaction t = em.getTransaction();

    try {
        t.begin();
        obj = em.merge(obj);
        t.commit();
        return obj;
    } catch (Exception e) {
        t.rollback();
        throw new MergeException("Cannot merge changes to " + String.valueOf(obj) + " into the database", e);
    }
}

From source file:de.iai.ilcd.model.dao.AbstractDao.java

/**
 * Default persist//from w  w w. j a v a2  s  .com
 * 
 * @param objs
 *            objects to persist
 * @throws PersistException
 *             on errors (transaction is being rolled back)
 */
public void persist(Collection<T> objs) throws PersistException {
    if (objs == null || objs.isEmpty()) {
        return;
    }
    EntityManager em = PersistenceUtil.getEntityManager();
    EntityTransaction t = em.getTransaction();

    try {
        t.begin();
        for (T obj : objs) {
            em.persist(obj);
        }
        t.commit();
    } catch (Exception e) {
        t.rollback();
        throw new PersistException(e.getMessage(), e);
    }
}