Example usage for javax.persistence EntityManager close

List of usage examples for javax.persistence EntityManager close

Introduction

In this page you can find the example usage for javax.persistence EntityManager close.

Prototype

public void close();

Source Link

Document

Close an application-managed entity manager.

Usage

From source file:com.enioka.jqm.tools.Loader.java

@Override
public Long getRunTimeSeconds() {
    if (this.job.getExecutionDate() == null) {
        EntityManager em2 = Helpers.getNewEm();
        this.job.setExecutionDate(em2.find(JobInstance.class, this.job.getId()).getExecutionDate());
        em2.close();
    }/* w w w  . j  a  va  2 s  .  co  m*/
    if (this.job.getExecutionDate() == null) {
        return 0L;
    }
    return (Calendar.getInstance().getTimeInMillis() - this.job.getExecutionDate().getTimeInMillis()) / 1000;
}

From source file:com.enioka.jqm.tools.JobManagerHandler.java

/**
 * Create a {@link com.enioka.jqm.jpamodel.Message} with the given message. The {@link com.enioka.jqm.jpamodel.History} to link to is
 * deduced from the context.//from w ww .j a v  a2 s  . co  m
 * 
 * @param msg
 * @throws JqmKillException
 */
private void sendMsg(String msg) {
    EntityManager em = Helpers.getNewEm();
    try {
        em.getTransaction().begin();
        Helpers.createMessage(msg, ji, em);
        em.getTransaction().commit();
    } finally {
        em.close();
    }
}

From source file:cz.fi.muni.pa165.dto.PrintedBookDAOTest.java

@Test
public void testFindPrintedBooksByLoan() {
    EntityManager em = emf.createEntityManager();
    PrintedBookDAOImpl pbDAO = new PrintedBookDAOImpl();
    pbDAO.setManager(em);/* ww  w .  j  av a  2s . c  o m*/
    Book b = new Book();
    b.setIdBook(1);

    Loan l = new Loan();
    l.setIdLoan(1);

    List<PrintedBook> li = pbDAO.findPrintedBooksByLoan(b, l);
    em.close();
    assertEquals(1, li.size());
}

From source file:de.zib.gndms.GORFX.context.service.globus.resource.TaskResource.java

/**
 * Loads a task state form the data base and executes the task, useing executeTask.
 *
 * @param id The id of the task to load.
 * @return The task object./* ww w  .j a  va2s  .  c  om*/
 * @throws ResourceException If this resource already go an task object.
 */
@NotNull
public Task loadModelById(@NotNull String id) throws ResourceException {

    logger.debug("task resource for: " + id);

    if (taskAction != null)
        throw new ResourceException("task action already loaded");

    EntityManager em = home.getEntityManagerFactory().createEntityManager();
    Task tsk = (Task) mH.loadModelById(em, id);

    if (tsk.isPostMortem()) {
        em.close();
        throw new NoSuchResourceException();
    }

    byte[] bcred = tsk.getSerializedCredential();
    GlobusCredential gcred = null;
    if (bcred.length > 0)
        gcred = DelegationAux.fromByteArray(bcred);

    try {
        taskAction = getResourceHome().getSystem().getInstanceDir().newTaskAction(em,
                tsk.getOfferType().getOfferTypeKey());
        taskAction.initFromModel(em, tsk);
        taskAction.setClosingEntityManagerOnCleanup(true);
        taskAction.setCredentialProvider(
                new GlobusCredentialProviderImpl(((ORQTaskAction) taskAction).getOrq().getOfferType(), gcred));
    } catch (IllegalAccessException e) {
        throw new ResourceException(e);
    } catch (InstantiationException e) {
        throw new ResourceException(e);
    } catch (ClassNotFoundException e) {
        throw new ResourceException(e);
    }
    executeTask();
    return tsk;
}

From source file:org.spc.ofp.tubs.domain.common.CommonRepository.java

public boolean saveImportStatus(final ImportStatus status) {
    boolean success = false;
    final EntityManager mgr = emf.createEntityManager();
    final EntityTransaction xa = mgr.getTransaction();
    xa.begin();//from w w  w.j a  v a 2  s . com
    try {
        if (status.getId() > 0L) {
            mgr.merge(status);
        } else {
            mgr.persist(status);
        }
        xa.commit();
        success = true;
    } catch (Exception ignoreMe) {
        rollbackQuietly(xa);
    } finally {
        mgr.close();
    }
    return success;
}

From source file:com.yahoo.sql4d.indexeragent.meta.DBHandler.java

public DataSource getDataSource(String tableName) {
    EntityManager em = getEntityManager();
    try {/*  www . jav a2s. co  m*/
        List<DataSource> resultList = em
                .createQuery("SELECT ds FROM DataSource ds WHERE ds.name = :name", DataSource.class)
                .setParameter("name", tableName).getResultList();
        return resultList.isEmpty() ? null : resultList.get(0);
    } finally {
        em.close();
    }
}

From source file:cz.fi.muni.pa165.daoImpl.TroopDAOImpl.java

@Override
public void removeTroop(Troop troop) throws IllegalArgumentException {
    if (troop == null) {
        throw new IllegalArgumentException("Troop can't be null.");
    }//from   w ww .  j  a  va  2 s. co m
    if (troop.getId() == null) {
        throw new IllegalArgumentException("Troop is not present in DB.");
    }
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    Troop present = em.find(Troop.class, troop.getId());
    em.getTransaction().commit();

    if (present == null) {
        throw new IllegalArgumentException("Troop is not present in DB.");
    } else {
        em.getTransaction().begin();
        em.remove(em.contains(troop) ? troop : em.merge(troop));
        em.getTransaction().commit();
    }
    em.close();
}

From source file:com.thruzero.domain.jpa.transaction.JpaDatabaseTransactionMgr.java

protected EntityManager doGetCurrentPersistenceManager(boolean autoCreateTransaction) {
    JpaTransactionState txState = localTransactionState.get();
    EntityManager currentEntityManager = txState.getEntityManager();

    if (autoCreateTransaction && !isTransactionActive()) {
        if (currentEntityManager != null && currentEntityManager.isOpen()) {
            transactionMgrLogHelper.logEntityManagerNotClosedWarning();
            currentEntityManager.close();
        }/* w w w.  j av  a  2 s  .com*/

        // begins a transaction if not currently active
        currentEntityManager = createEntityManager();
        if (!isTransactionActive()) {
            currentEntityManager.getTransaction().begin();
        }

        txState.setEntityManager(currentEntityManager);
    }

    return currentEntityManager;
}

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();//  w ww  . j  a va 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:entity.files.SYSFilesTools.java

public static boolean deleteFile(SYSFiles sysfile) {
    boolean success = false;
    EntityManager em = OPDE.createEM();

    try {/*from  ww w. j ava  2  s. co  m*/
        em.getTransaction().begin();
        SYSFiles mySYSFile = em.merge(sysfile);
        em.remove(mySYSFile);
        em.getTransaction().commit();
        success = true;
    } catch (OptimisticLockException ole) {
        OPDE.warn(ole);
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (ole.getMessage().indexOf("Class> entity.info.Resident") > -1) {
            OPDE.getMainframe().emptyFrame();
            OPDE.getMainframe().afterLogin();
        }

        OPDE.getDisplayManager().addSubMessage(DisplayManager.getLockMessage());
        success = false;
    } catch (Exception ex) {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        success = false;
    } finally {
        em.close();
    }

    if (success) {
        try {
            FileTransferClient ftp = getFTPClient();
            ftp.deleteFile(sysfile.getRemoteFilename());
            ftp.disconnect();
            OPDE.info("DELETING FILE FROM FTPSERVER: " + sysfile.getFilename() + " (" + sysfile.getMd5() + ")");
        } catch (Exception e) {
            OPDE.error(e);
            success = false;
        }
    }

    return success;
}