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.peterspan.csv2db.converter.Converter.java

@Override
protected Void doInBackground() throws Exception {
    EntityTransaction tx = null;
    try {//from   w  ww  .j  a v  a2  s. co  m
        tx = entityManager.getTransaction();
        tx.begin();
        List<String[]> allLines = readFile();

        double increment = 100.0 / allLines.size();
        double progress = 0.0;
        for (String[] line : allLines) {
            progress = progress + increment;
            setProgress((int) Math.round(progress));

            if (line[0].equals("locnumber")) {
                //This is the header! Create a single instance header object
                Header.getInstance().init(line);
                continue;
            }
            if (line[0].equals("")) {
                continue;
            }
            readLine(line);
        }

        entityManager.flush();
        tx.commit();

    } catch (HibernateException he) {
        log.error("An error occured. Tx will be rolledback!", he);
        if (tx != null) {
            tx.rollback();
        }
    }

    return null;
}

From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java

public SubscribedResource updateSubscribedResource(SubscribedResource sm) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();/*from  ww  w  .j  a  v  a 2 s . c om*/
    SubscribedResource resis = entityManager.merge(sm);
    entityTransaction.commit();

    return resis;
}

From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java

public DeploymentDescriptor updateDeploymentDescriptor(DeploymentDescriptor d) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();/*ww w.jav a2s .co m*/
    DeploymentDescriptor resis = entityManager.merge(d);
    entityTransaction.commit();

    return resis;
}

From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java

public void saveProperty(BakerProperty p) {
    logger.info("Will BakerProperty = " + p.getName());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();//from  w ww. j a  va 2  s  .c o m

    entityManager.persist(p);
    entityManager.flush();
    entityTransaction.commit();

}

From source file:edu.kit.dama.mdm.core.jpa.MetaDataManagerJpa.java

/**
 * Finalize any operation which affected the EntityManager. This method has
 * the following tasks:/* w w  w.  j a  v a 2  s .c o  m*/
 *
 * <ul>
 * <li>Commit the provided transaction</li>
 * <li>If the commit fails, rollback the transaction</li>
 * <li>Clear the cache of the EntityManager (make all entities
 * unmanaged)</li>
 * </ul>
 *
 * If no transaction is provided (after an operation which did not affect
 * the data backend, e.g. find()), only the cache is cleared.
 *
 * @param <T> Generic entity type.
 * @param methodName Description of the method which was performed (for
 * debugging).
 * @param transaction The transaction to finalize.
 * @param entity Affected entity or entity class (for debugging).
 */
private <T> void finalizeEntityManagerAccess(String methodName, EntityTransaction transaction, T entity) {
    if (transaction != null) {
        try {
            LOGGER.debug("Flushing entityManager");
            entityManager.flush();
            LOGGER.debug("Committing current transaction");
            transaction.commit();
        } catch (Exception e) {
            LOGGER.warn("Failed to commit transaction for operation '" + methodName + "'", e);
        } finally {
            if (transaction.isActive()) {
                LOGGER.debug("Transaction is still active. Performing rollback.");
                transaction.rollback();
                LOGGER.error("Method '" + methodName + "' fails for entity/class '" + entity + "'");
            }
            LOGGER.debug("Clearing entityManager cache");
            entityManager.clear();
            LOGGER.debug("Cache cleared.");
        }
    } else {
        LOGGER.debug("Clearing entityManager cache");
        entityManager.clear();
        LOGGER.debug("Cache cleared.");
    }
}

From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java

public void saveInstalledBun(InstalledBun is) {
    logger.info("Will create InstalledBun = " + is.getUuid());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();//  w  ww.  jav a  2s.c om

    entityManager.persist(is);
    entityManager.flush();
    entityTransaction.commit();
}

From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java

public void saveSubscribedResource(SubscribedResource sm) {
    logger.info("Will save SubscribedResource = " + sm.getURL());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();//from ww w  . ja v a  2 s  . c om

    entityManager.persist(sm);
    entityManager.flush();
    entityTransaction.commit();

}

From source file:gr.upatras.ece.nam.baker.impl.BakerJpaController.java

public void saveUser(BakerUser bu) {
    logger.info("Will save BakerUser = " + bu.getName());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();/*w  ww.j a va  2 s.com*/

    entityManager.persist(bu);

    entityManager.flush();
    entityTransaction.commit();

}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public UserSession saveUserSession(UserSession userSession) {
    logger.info("Will userSession = " + userSession.getUsername());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();//  w  w  w.ja  v a2 s.c om

    FStoreUser u = entityManager.find(FStoreUser.class, userSession.getUser().getId());
    userSession.setUser(u);

    logger.info("Will userSession = " + u.toString());

    entityManager.persist(u);
    entityManager.persist(userSession);
    entityManager.flush();
    entityTransaction.commit();

    return userSession;

}

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

/**
 * Concrete implementation required for saving of digital files
 *//* w w w  .java2 s  .c  o m*/
@Override
public boolean checkAndPersist(Source source, PersistType pType, PrintWriter out) {
    EntityManager em = PersistenceUtil.getEntityManager();

    Source existingSource = this.getByUuid(source.getUuid().getUuid());
    if (existingSource != null) {
        if (pType == PersistType.ONLYNEW) {
            out.println(
                    "Warning: source data set with this uuid already exists in database; will ignore this data set");
            return false;
        }
    }

    EntityTransaction t = em.getTransaction();
    try {
        t.begin();
        if (existingSource != null && (pType == PersistType.MERGE)) {
            // delete first the existing one, we will use the new one
            if (out != null) {
                out.println(
                        "Notice: source data set with this uuid already exists in database; will merge this data set");
            }
            em.remove(existingSource);
            this.deleteDigitalFiles(source);
        }

        em.persist(source);

        t.commit();

        if (!super.setMostRecentVersionFlags(source.getUuidAsString())) {
            return false;
        }

        if (source != null && source.getId() != null) {
            if (!this.saveDigitalFiles(source, out)) {
                if (out != null) {
                    out.println(
                            "Warning: couldn't save all files of this source data set into database file directory: see messages above");
                }
            }
        }
        return true;

    } catch (Exception e) {
        if (out != null) {
            out.println("Can't save source data file to database because of: " + e.getMessage());
        }
        t.rollback();
        return false;
    }
}