Example usage for javax.persistence EntityTransaction begin

List of usage examples for javax.persistence EntityTransaction begin

Introduction

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

Prototype

public void begin();

Source Link

Document

Start a resource transaction.

Usage

From source file:portal.api.impl.PortalJpaController.java

public void deleteAllInstalledVxFs() {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();

    Query q = entityManager.createQuery("DELETE FROM InstalledVxF ");
    q.executeUpdate();/*  w  ww . ja  va 2  s  .c  om*/
    entityManager.flush();

    entityTransaction.commit();
}

From source file:portal.api.impl.PortalJpaController.java

public void deleteAllUsers() {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();

    Query q = entityManager.createQuery("DELETE FROM PortalUser ");
    q.executeUpdate();/* www  . ja v a  2s  . com*/
    entityManager.flush();

    entityTransaction.commit();

}

From source file:portal.api.impl.PortalJpaController.java

public void deleteAllMANOplatforms() {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();

    Query q = entityManager.createQuery("DELETE FROM MANOplatform");
    q.executeUpdate();/* w ww . j av  a 2s . c  o m*/
    entityManager.flush();

    entityTransaction.commit();

}

From source file:portal.api.impl.PortalJpaController.java

public void deleteAllMANOproviders() {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();

    Query q = entityManager.createQuery("DELETE FROM MANOprovider");
    q.executeUpdate();//  www  . ja v  a2  s .c  om
    entityManager.flush();

    entityTransaction.commit();

}

From source file:portal.api.impl.PortalJpaController.java

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

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();

    entityManager.persist(bu);//from www. j  ava2  s.c  o  m

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

}

From source file:uk.ac.ebi.bioinvindex.utils.datasourceload.DataSourceLoader.java

private void persistLocations(ReferenceSource isaTabSource, Collection<AssayTypeDataLocation> locations) {
    EntityTransaction transaction = getEntityManager().getTransaction();

    Timestamp ts = new Timestamp(System.currentTimeMillis());
    DaoFactory daoFactory = DaoFactory.getInstance(getEntityManager());

    DataLocationPersister locPersister = new DataLocationPersister(daoFactory, ts);
    ReferenceSourcePersister srcPersister = new ReferenceSourcePersister(daoFactory, ts);

    IdentifiableDAO<AssayTypeDataLocation> dao = daoFactory.getIdentifiableDAO(AssayTypeDataLocation.class);

    List<AssayTypeDataLocation> dataLocations = dao.getAll();

    boolean needsCommit = false;
    for (AssayTypeDataLocation dataLocation : dataLocations) {
        // TODO: Playing this way with serialize transactions is dangerous and we should fix this
        // PLEASE LEAVE THIS transaction commands here until we find a workaround, THEY ARE NEEDED in the ISATAB loader 
        if (!transaction.isActive())
            transaction.begin();
        UnloadManager unloadManager = new UnloadManager(daoFactory, dataLocation.getSubmissionTs());
        unloadManager.queue(dataLocation);
        unloadManager.delete();/* w w  w  . jav a 2 s .  com*/
        needsCommit = true;
    }

    if (needsCommit)
        transaction.commit();
    if (!transaction.isActive())
        transaction.begin();

    needsCommit = false;
    for (AssayTypeDataLocation location : locations) {
        locPersister.persist(location);
        needsCommit = true;
    }

    if (needsCommit)
        transaction.commit();
    if (!transaction.isActive())
        transaction.begin();

    // Gets the old isaTabSource and replace with the new one in case it's already there
    // 
    AccessibleDAO<ReferenceSource> daoRef = DaoFactory.getInstance(entityManager)
            .getAccessibleDAO(ReferenceSource.class);
    ReferenceSource oldIsaTabSrc = daoRef.getByAcc(ReferenceSource.ISATAB_METADATA);
    if (oldIsaTabSrc != null) {
        UnloadManager unloadManager = new UnloadManager(DaoFactory.getInstance(entityManager),
                oldIsaTabSrc.getSubmissionTs());
        unloadManager.queue(oldIsaTabSrc);

        unloadManager.delete();
        transaction.commit();
        // At the end we have another initiated transaction
        transaction.begin();
    }

    srcPersister.persist(isaTabSource);
    transaction.commit();

    // Leave an opened transaction, so that it's possible to rejoin the one opened by an invoker
    // TODO: Playing this way to serialize transactions is dangerous and we should fix this
    // 
    transaction.begin();
}

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

/**
 * Generic persist method for data set objects.
 * /*from   w  w  w  .j  a  v a2s  .c o m*/
 * @param dataSet
 *            data set to persist
 * @param pType
 *            which type of persistence operation, new, update (i.e. overwrite existing data set), ...
 * @param out
 *            PrintWriter to log error messages which can be presented to the end user
 * @return true if persist is successful, false otherwise
 * @see #preCheckAndPersist(DataSet)
 */
public boolean checkAndPersist(T dataSet, PersistType pType, PrintWriter out) {
    // TODO: check if version shall be excluded for some types

    if (dataSet.getRootDataStock() == null) {
        out.println("Error: root data stock must not be null!");
        return false;
    }

    // perform pre-persist actions
    this.preCheckAndPersist(dataSet);

    EntityManager em = PersistenceUtil.getEntityManager();

    // locate existing method by UUID
    T existingDataSet = this.getByUuidAndVersion(dataSet.getUuidAsString(), dataSet.getVersion());

    // if existing found ...
    if (existingDataSet != null) {
        // ... and mode is set to only new ...
        if (PersistType.ONLYNEW.equals(pType)) {
            if (out != null) {
                out.println("Warning: " + this.getAccessedClass().getSimpleName() + " data set with uuid "
                        + dataSet.getUuidAsString() + " and version " + dataSet.getVersion().getVersionString()
                        + " already exists in database; will ignore this data set.");
            }
            // ... just ignore it
            return false;
        }
    }

    EntityTransaction t = em.getTransaction();
    // now the DB interaction
    try {
        t.begin();
        // delete existing for merge operation
        if (existingDataSet != null && PersistType.MERGE.equals(pType)) {
            if (out != null) {
                out.println("Notice: " + this.getAccessedClass().getSimpleName() + " method data set with uuid "
                        + existingDataSet.getUuidAsString() + " and version "
                        + existingDataSet.getVersion().getVersionString()
                        + " already exists in database; will replace it with this data set");
            }
            em.remove(existingDataSet);
        }
        // persist the new method
        em.persist(dataSet);

        // actual write to DB
        t.commit();

        // set the most recent version flags correctly
        if (!this.setMostRecentVersionFlags(dataSet.getUuidAsString())) {
            return false;
        }

        // and return with success :)
        return true;
    } catch (Exception e) {
        DataSetDao.LOGGER.error(
                "Cannot persist " + this.getAccessedClass().getSimpleName() + " data set with uuid {}",
                dataSet.getUuidAsString());
        DataSetDao.LOGGER.error("Exception is: ", e);
        if (out != null) {
            out.println("Error: " + this.getAccessedClass().getSimpleName() + " data set with uuid "
                    + dataSet.getUuidAsString() + " and version " + dataSet.getVersion().getVersionString()
                    + " could not be saved into database; unknown database error; Exception message: "
                    + e.getMessage());
        }
        t.rollback();
    }

    return false;
}

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

/**
 * Get a transaction for a session. If there is already a transaction
 * active, this transaction will be used. Otherwise a new transaction will
 * be started./*w  ww .  j  ava  2 s.c o  m*/
 * 
 * @param s
 *            Session for this transaction
 * @return A already active or a fresh transaction
 */
public EntityTransaction getTransaction(EntityManager s) {
    EntityTransaction result = s.getTransaction();
    if (result.isActive()) {
        if (log.isDebugEnabled()) {
            log.debug(Messages.JoiningTransaction);
        }
    } else {
        result.begin();
        if (log.isDebugEnabled()) {
            log.debug(Messages.StartingTransaction);
        }
    }

    return result;
}

From source file:fr.natoine.dao.annotation.DAOAnnotation.java

/**
 * Retrieves all the existing AnnotationStatus
 * @return//from   ww w. j a  v  a  2  s  .c o  m
 */
public List<AnnotationStatus> retrieveAnnotationStatus() {
    //   EntityManagerFactory emf = this.setEMF();
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        List<AnnotationStatus> annotationstatus = em.createQuery("from AnnotationStatus").getResultList();
        tx.commit();
        return annotationstatus;
    } catch (Exception e) {
        tx.rollback();
        //em.close();
        System.out.println(
                "[RetrieveAnnotationStatus.retrieveAnnotationStatus] unable to retrieve AnnotationStatus"
                        + " cause : " + e.getMessage());
        return new ArrayList<AnnotationStatus>();
    }
}

From source file:fr.natoine.dao.annotation.DAOAnnotation.java

/**
 * Retrieves an AnnotationStatus in the database according to the specified label
 * @param label//from   w  w w .  j  ava2 s .  c om
 * @return an AnnotationStatus that may be a new UriStatus with no value.
 */
public AnnotationStatus retrieveAnnotationStatus(String label) {
    //EntityManagerFactory emf = this.setEMF();
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        AnnotationStatus annotationstatus = (AnnotationStatus) em
                .createQuery("from AnnotationStatus where label = ?").setParameter(1, label).getSingleResult();
        tx.commit();
        return annotationstatus;
    } catch (Exception e) {
        tx.rollback();
        //em.close();
        System.out.println(
                "[RetrieveAnnotationStatus.retrieveAnnotationStatus] unable to retrieve AnnotationStatus"
                        + " label : " + label + " cause : " + e.getMessage());
        return new AnnotationStatus();
    }
}