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.github.jrh3k5.membership.renewal.mailer.service.jpa.JpaMembershipService.java

private void updateMembership(EntityManager entityManager, JpaMembershipRecord membership) {
    final EntityTransaction transaction = entityManager.getTransaction();
    try {//  www  .  j  a  va 2 s  .co  m
        transaction.begin();
        entityManager.merge(membership);
        transaction.commit();
    } catch (RuntimeException e) {
        transaction.rollback();
        throw e;
    }
}

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

public void update(Collection<T> entities) {
    EntityTransaction tx = getEntityManager().getTransaction();
    tx.begin();/* w w w.  j  a va  2s.c  om*/
    for (T entity : entities) {
        getEntityManager().merge(entity);
    }
    tx.commit();
}

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

public void delete(Collection<T> entities) {
    EntityTransaction tx = getEntityManager().getTransaction();
    tx.begin();//from w  w w .  j  ava 2 s .co  m
    for (T entity : entities) {
        getEntityManager().remove(entity);
    }
    tx.commit();
}

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

public void insert(Collection<T> entities) {
    EntityTransaction tx = getEntityManager().getTransaction();
    tx.begin();//from  ww  w  .  j  a va  2 s  . c  o m
    for (T entity : entities) {
        getEntityManager().persist(entity);
    }
    tx.commit();
}

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

@Transactional
public void deleteAllCartItems(Long userId) {
    EntityManager em = getEntityManager();
    EntityTransaction txn = em.getTransaction();
    txn.begin();/*from  w ww  . j  a  v a  2  s  .c  o  m*/
    Query q = em.createQuery("DELETE FROM Cart c where c.user.id=:id");
    q.setParameter("id", userId);
    q.executeUpdate();
    txn.commit();
}

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();// ww 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:it.infn.ct.futuregateway.apiserver.v1.InfrastructureCollectionService.java

/**
 * Retrieve the application list./*from  w  w w. j a  v  a  2  s  . 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:facades.PersonFacadeDB.java

@Override
public RoleSchool addRole(String json, Integer id) throws NotFoundException {
    Person p = gson.fromJson(getPerson(id), Person.class);
    HashMap<String, String> map = new Gson().fromJson(json, new TypeToken<HashMap<String, String>>() {
    }.getType());//from   ww w .j  a  v a  2 s.  c o m

    String roleName = map.get("roleName");
    RoleSchool role;

    switch (roleName) {
    case "Teacher Assistant":
        //Create role
        RoleSchool ta = new TeacherAssistant();
        ta.setPerson(p);
        role = ta;
        break;
    case "Teacher":

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        String dateInString = map.get("date");
        Date date = new Date();

        try {
            date = formatter.parse(dateInString);
        } catch (ParseException e) {
            e.printStackTrace(System.out);
        }

        RoleSchool t = new Teacher(date, map.get("degree"));
        t.setPerson(p);
        role = t;
        break;
    case "Student":
        RoleSchool s = new Student(map.get("semester"));
        s.setPerson(p);
        role = s;
        break;
    default:
        throw new IllegalArgumentException("no such role");
    }
    //save this info
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceFileName);
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();

    try {
        em.persist(role);
        transaction.commit();
        em.getEntityManagerFactory().getCache().evictAll();
    } catch (Exception e) {
        throw new NotFoundException("Couldnt add role");
    } finally {
        em.close();
    }

    return role;

}

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

/**
 * The method to update the serailizable business objects into the database.
 * /*from w w  w.  j  av a2  s .  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) {
        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  w w .j  ava2s  .co m*/
    try {
        entityManager.remove(object);
    } catch (Exception ex) {
        txn.rollback();
    } finally {
        if (!txn.getRollbackOnly()) {
            txn.commit();
        }
    }
}