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.appdynamicspilot.persistence.BasePersistenceImpl.java

@Transactional
public void save(final Serializable object) {
    EntityManager entityManager = getEntityManager();
    EntityTransaction txn = entityManager.getTransaction();
    txn.begin();/*from  ww w.j  a  v  a2s .com*/
    try {
        entityManager.persist(object);
    } catch (Exception ex) {
        logger.error(ex);
        txn.rollback();
    } finally {
        if (!txn.getRollbackOnly()) {
            txn.commit();
        }
    }

}

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

/**
 * Retrieve a task list for the user.// ww  w  . j  av a  2s . c o m
 * Tasks are retrieved from the storage for the user performing the request.
 *
 * @return A list of tasks
 */
private List<Task> retrieveTaskList() {
    List<Task> lstTasks = new LinkedList<>();
    EntityManager em = getEntityManager();
    EntityTransaction et = null;
    List<Object[]> taskList = null;
    try {
        et = em.getTransaction();
        et.begin();
        taskList = em.createNamedQuery("tasks.userAll").setParameter("user", getUser()).getResultList();
        et.commit();
    } catch (RuntimeException re) {
        if (et != null && et.isActive()) {
            et.rollback();
        }
        log.error("Impossible to retrieve the task list");
        log.error(re);
        throw new RuntimeException("Impossible to access the task list");
    } finally {
        em.close();
    }
    if (taskList != null && !taskList.isEmpty()) {
        for (Object[] elem : taskList) {
            int idElem = 0;
            Task tmpTask = new Task();
            tmpTask.setId((String) elem[idElem++]);
            tmpTask.setDescription((String) elem[idElem++]);
            tmpTask.setStatus((Task.STATUS) elem[idElem++]);
            tmpTask.setDateCreated((Date) elem[idElem]);
            lstTasks.add(tmpTask);
        }
    }
    return lstTasks;
}

From source file:test.unit.be.fedict.hsm.entity.PersistenceTest.java

@Test
public void testApplicationCredential() throws Exception {
    ApplicationEntity applicationEntity = new ApplicationEntity("app");

    EntityTransaction entityTransaction = this.entityManager.getTransaction();
    entityTransaction.begin();//from www  .jav  a 2s  .  co  m
    this.entityManager.persist(applicationEntity);
    long appId = applicationEntity.getId();
    entityTransaction.commit();

    entityTransaction.begin();
    applicationEntity = this.entityManager.find(ApplicationEntity.class, appId);
    CredentialEntity credentialEntity = new CredentialEntity("hash", "credential".getBytes(),
            applicationEntity);
    this.entityManager.persist(credentialEntity);
    entityTransaction.commit();

    entityTransaction.begin();
    List<CredentialEntity> credentialList = CredentialEntity.getCredentials(this.entityManager,
            applicationEntity);
    assertNotNull(credentialList);
    assertEquals(1, credentialList.size());
}

From source file:name.livitski.tools.persista.schema.example.CustomerCRUDTest.java

@Before
public void createRecord() throws ApplicationBeanException {
    if (null != storedRecordId)
        return;/*from  w w w . j  av a  2  s  . c  om*/
    setUpJPA();
    final EntityManager db = getEntityManager();
    final EntityTransaction txn = db.getTransaction();
    txn.begin();
    Customer customer = new Customer();
    customer.setName(CUSTOMER_NAME);
    customer.setAddress(CUSTOMER_ADDRESS);
    customer.setPhone(CUSTOMER_PHONE);
    customer.setEmail(CUSTOMER_EMAIL);
    db.persist(customer);
    txn.commit();
    storedRecordId = customer.getId();
}

From source file:com.github.jinahya.persistence.ShadowTest.java

@Test(enabled = false, invocationCount = 1)
public void testPersist() {
    final EntityManager manager = LocalPU.createEntityManager();
    try {/* w  ww  .  j  a  va2 s .  com*/
        final EntityTransaction transaction = manager.getTransaction();
        transaction.begin();
        try {
            final Shadow shadow = persistInstance(manager, null, null);
            transaction.commit();
        } catch (Exception e) {
            LocalPU.printConstraintViolation(e);
            transaction.rollback();
            e.printStackTrace(System.err);
            Assert.fail(e.getMessage());
        }
    } finally {
        manager.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 a  2  s .co 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:de.iai.ilcd.model.dao.AbstractDao.java

/**
 * Default persist//  w  ww  .  ja  va  2  s  .c om
 * 
 * @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);
    }
}

From source file:com.espirit.moddev.examples.uxbridge.newswidget.test.CommandITCase.java

/**
 * Before test./*w  ww . j av a  2s  .c o m*/
 *
 * @throws Exception the exception
 */
@Before
public void beforeTest() throws Exception {
    EntityManager em = emf.createEntityManager();

    EntityTransaction tx = em.getTransaction();
    tx.begin();
    em.createQuery("DELETE FROM article").executeUpdate();
    tx.commit();
    em.close();
}

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

/**
 * {@inheritDoc}/*from   www  .j  a  v  a2s .c  o  m*/
 */
@Override
public void storeLicenses(Set<License> licenses) {
    EntityManager em = ((EntityManagerHolder) TransactionSynchronizationManager.getResource(emf))
            .getEntityManager();
    EntityTransaction tx = em.getTransaction();
    if (!tx.isActive()) {
        tx.begin();
    }
    for (License l : licenses) {
        em.persist(l);
    }
    tx.commit();
}

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

/**
 * {@inheritDoc}//  w  w w.java2s . c o m
 */
@Override
public LoginHistory addLoginHistory(User user, Date loginDate, Date logoutDate) {
    EntityManager em = ((EntityManagerHolder) TransactionSynchronizationManager.getResource(emf))
            .getEntityManager();
    EntityTransaction tx = em.getTransaction();
    if (!tx.isActive()) {
        tx.begin();
    }
    LoginHistory history = user.addLoginHistory(loginDate, logoutDate);
    tx.commit();
    return history;
}