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:de.iai.ilcd.model.dao.AbstractDao.java

/**
 * Default remove: bring back to persistence context if required and delete
 * /*w ww . j ava  2s  . c  o m*/
 * @param obj
 *            object to remove
 * @return remove object
 * @throws Exception
 *             on errors (transaction is being rolled back)
 */
public T remove(T obj) throws Exception {
    if (obj == null) {
        return null;
    }
    EntityManager em = PersistenceUtil.getEntityManager();
    EntityTransaction t = em.getTransaction();

    try {
        t.begin();
        T tmp = em.contains(obj) ? obj : em.merge(obj);
        em.remove(tmp);
        t.commit();
        return tmp;
    } catch (Exception e) {
        t.rollback();
        throw e;
    }
}

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

/**
 * Default merge/*  w ww.ja v a 2  s.  co m*/
 * 
 * @param objs
 *            objects to merge
 * @return list of merged/managed objects
 * @throws MergeException
 *             on errors (transaction is being rolled back)
 */
public Collection<T> merge(Collection<T> objs) throws MergeException {
    if (objs == null || objs.isEmpty()) {
        return null;
    }
    final Collection<T> tmp = new ArrayList<T>();
    EntityManager em = PersistenceUtil.getEntityManager();
    EntityTransaction t = em.getTransaction();

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

From source file:org.apache.juddi.api.impl.UDDIv2PublishImpl.java

private String getUsername(String authinfo) {
    String user = "N/A";

    EntityManager em = PersistenceManager.getEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {//from  ww  w . ja va2s  .  co m

        tx.begin();
        user = publishService.getEntityPublisher(em, authinfo).getAuthorizedName();
        tx.commit();
    } catch (Exception ex) {
        logger.error(ex);
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        em.close();
    }
    return user;
}

From source file:info.san.books.app.model.listener.LivreListener.java

@EventHandler
public void handle(LivreAssignedAuteurEvent e) {
    EntityManager em = Persistence.getInstance().createEntityManager();

    EntityTransaction t = em.getTransaction();

    t.begin();//from   w w  w  .j a  v a2  s  . c om

    LivreEntry livre = em.find(LivreEntry.class, e.getIsbn());
    AuteurEntry auteur = em.find(AuteurEntry.class, e.getAuteurId());

    livre.getAuteurs().add(auteur);
    auteur.getLivres().add(livre);

    t.commit();
}

From source file:org.eclipse.jubula.client.core.persistence.Persistor.java

/**
 * // w  w w  .ja  v  a  2  s .  c  o m
 * @param em
 *            entity manager
 * @throws PersistenceException
 *             if we blow up
 */
private static void createOrUpdateDBGuard(EntityManager em) throws PersistenceException {
    EntityTransaction tx = null;
    try {
        tx = em.getTransaction();
        tx.begin();

        try {
            em.createQuery("select guard from DbGuardPO as guard").getSingleResult(); //$NON-NLS-1$
        } catch (NoResultException nre) {
            LockManager.initDbGuard(em);
        }

        tx.commit();
    } catch (PersistenceException pe) {
        if (tx != null) {
            tx.rollback();
        }
        throw pe;
    }
}

From source file:facades.PersonFacadeDB.java

@Override
public Person delete(Integer id) throws NotFoundException {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceFileName);
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();//from   w  ww .j  a va 2 s .  co m

    Person p = em.find(Person.class, id);

    if (p == null) {
        transaction.rollback();
        throw new NotFoundException("No person for the given id");
    } else {
        em.remove(p);
        transaction.commit();
    }

    return p;
}

From source file:info.san.books.app.model.listener.LivreListener.java

@EventHandler
public void handle(LivreUnassignedAuteurEvent e) {
    EntityManager em = Persistence.getInstance().createEntityManager();

    EntityTransaction t = em.getTransaction();

    t.begin();//from  www .j  a  va 2 s.  c o  m

    LivreEntry livre = em.find(LivreEntry.class, e.getIsbn());
    AuteurEntry auteur = em.find(AuteurEntry.class, e.getAuteurId());

    livre.getAuteurs().remove(auteur);
    auteur.getLivres().remove(livre);

    t.commit();
}

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

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

    try {// ww  w. java  2  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 (Exception excp) {
        logDbError("DbAuditProvider.commitTransaction(): failed", excp);

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

    return ret;
}

From source file:org.apache.james.user.jpa.JPAUsersRepository.java

/**
 * Update the repository with the specified user object. A user object with
 * this username must already exist.//from   ww w  .jav  a  2s  .  c o m
 * 
 * @throws UsersRepositoryException
 */
public void updateUser(User user) throws UsersRepositoryException {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    final EntityTransaction transaction = entityManager.getTransaction();
    try {
        if (contains(user.getUserName())) {
            transaction.begin();
            entityManager.merge(user);
            transaction.commit();
        } else {
            getLogger().debug("User not found");
            throw new UsersRepositoryException("User " + user.getUserName() + " not found");
        }
    } catch (PersistenceException e) {
        getLogger().debug("Failed to update user", e);
        if (transaction.isActive()) {
            transaction.rollback();
        }
        throw new UsersRepositoryException("Failed to update user " + user.getUserName(), e);
    } finally {
        entityManager.close();
    }
}

From source file:com.edugility.substantia.substance.TestCasePerson.java

@Test
public void testingJPA() throws Exception {
    final EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
    assertNotNull(emf);/*w  ww  .j  a  va  2 s .  c  o  m*/

    final EntityManager em = emf.createEntityManager();
    assertNotNull(em);

    final EntityTransaction et = em.getTransaction();
    assertNotNull(et);
    et.begin();

    final Person p = new Person();
    em.persist(p);
    em.flush();
    assertFalse(p.isTransient());
    assertTrue(p.isVersioned());
    assertEquals(Long.valueOf(1L), p.getId());
    assertEquals(Integer.valueOf(1), p.getVersion());

    et.commit();
    et.begin();

    final Person p2 = em.find(Person.class, 1L, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
    assertNotNull(p2);
    assertFalse(p2.isTransient());
    assertTrue(p2.isVersioned());
    assertEquals(Long.valueOf(1L), p2.getId());
    assertEquals(Integer.valueOf(1), p2.getVersion());

    et.commit();
    et.begin();

    final Person p3 = em.getReference(Person.class, 1L);
    assertNotNull(p3);
    assertFalse(p3.isTransient());
    assertTrue(p3.isVersioned());
    assertEquals(Long.valueOf(1L), p3.getId());
    assertEquals(Integer.valueOf(2), p3.getVersion());

    et.commit();
    et.begin();

    assertTrue(em.contains(p));
    em.remove(p);

    et.commit();

    em.close();

    emf.close();
}