Example usage for javax.persistence EntityTransaction commit

List of usage examples for javax.persistence EntityTransaction commit

Introduction

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

Prototype

public void commit();

Source Link

Document

Commit the current resource transaction, writing any unflushed changes to the database.

Usage

From source file:com.sixsq.slipstream.persistence.Run.java

public static Run updateRunState(Run run, States newState, boolean retry) {
    EntityManager em = PersistenceUtil.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();// w  w w  .  j av  a  2 s .c  o  m
    try {
        run = Run.loadFromUuid(run.getUuid(), em);
        run.setState(newState);
        transaction.commit();
        em.close();
    } catch (Exception e) {
        String error = "error setting run state: " + newState;
        if (retry) {
            Logger.getLogger("restlet").warning(error + " retrying...");
        } else {
            Logger.getLogger("restlet").severe(error);
        }
        // retry once
        if (retry) {
            updateRunState(run, newState, false);
        }
    }
    return run;
}

From source file:com.google.constructor.cip.orm.jpa.BaseJPAService.java

public void update(T entity, boolean refresh) {
    Validate.notNull(entity, "Null Entity parameter");
    EntityTransaction tx = getEntityManager().getTransaction();
    tx.begin();/*  www  .java  2 s . c  o m*/
    getEntityManager().merge(entity);
    tx.commit();
}

From source file:com.google.constructor.cip.orm.jpa.BaseJPAService.java

public void delete(T entity) {
    Validate.notNull(entity, "Null Entity parameter");
    EntityTransaction tx = getEntityManager().getTransaction();
    tx.begin();/*from ww  w.j  a va 2  s  . c om*/
    getEntityManager().remove(entity);
    tx.commit();
}

From source file:com.google.constructor.cip.orm.jpa.BaseJPAService.java

public void insert(T entity) {
    Validate.notNull(entity, "Null Entity parameter");
    EntityTransaction tx = getEntityManager().getTransaction();
    tx.begin();//from   w  w w  .j ava  2 s  .c  o m
    getEntityManager().persist(entity);
    tx.commit();
}

From source file:org.nuxeo.ecm.core.persistence.PersistenceProvider.java

protected void doCommit(EntityManager em) {
    try {//from ww  w . j a  va 2  s.  co m
        em.flush();
    } catch (TransactionRequiredException e) {
        // ignore
    }
    EntityTransaction et = getTransaction(em);
    if (et == null || !et.isActive()) {
        return;
    }
    et.commit();
}

From source file:com.google.constructor.cip.orm.jpa.BaseJPAService.java

public void delete(PK pk) {
    Validate.notNull(pk, "Null pk parameter");
    EntityTransaction tx = getEntityManager().getTransaction();
    tx.begin();//from  ww  w.j a va 2 s  .  co  m
    getEntityManager().remove(find(pk));
    tx.commit();
}

From source file:org.apache.openjpa.persistence.event.TestBeforeCommit.java

@Override
public void setUp() throws Exception {
    if (emf == null) {
        emf = createEMF(AnEntity.class);
    }/*w w w.  j  a  v a 2s.c  o  m*/
    dict = ((JDBCConfiguration) emf.getConfiguration()).getDBDictionaryInstance();
    EntityManager em = emf.createEntityManager();
    EntityTransaction tran = em.getTransaction();

    tran.begin();
    em.createQuery("Delete from AnEntity").executeUpdate();
    tran.commit();

    tran.begin();
    ae = new AnEntity();
    ae.setId(PKID);
    ae.setName("");
    em.persist(ae);
    tran.commit();
    em.close();
}

From source file:facades.PersonFacadeDB.java

@Override
public Person addPerson(String json) {
    //make person from Json
    Person p = gson.fromJson(json, Person.class);

    EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceFileName);
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();// w  w  w .jav  a 2 s  . co  m

    try {
        em.persist(p);
        transaction.commit();
    } catch (Exception e) {
        transaction.rollback();
    } finally {
        em.close();
    }
    return p;
}

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.//w w w  .  ja  v a  2  s.  c om
 *
 * @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.opencastproject.comments.persistence.CommentDatabaseImpl.java

@Override
public CommentDto storeComment(Comment comment) throws CommentDatabaseException {
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {//from   ww w .  ja  v  a  2s  .  co  m
        tx.begin();
        CommentDto mergedComment = CommentDatabaseUtils.mergeComment(comment, em);
        tx.commit();
        return mergedComment;
    } catch (Exception e) {
        logger.error("Could not update or store comment: {}", ExceptionUtils.getStackTrace(e));
        if (tx.isActive())
            tx.rollback();

        throw new CommentDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}