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.github.jrh3k5.membership.renewal.mailer.service.jpa.JpaEmailService.java

@Override
public JpaEmailRecord getByAddress(String emailAddress) {
    final EntityManager entityManager = entityManagerFactory.createEntityManager();
    try {// w  w w . j  av  a 2  s . co m
        final Query query = entityManager
                .createQuery("select e from EmailRecord e where e.emailAddress = :emailAddress");
        query.setParameter("emailAddress", emailAddress);
        @SuppressWarnings("unchecked")
        final List<JpaEmailRecord> results = query.getResultList();
        return results.size() == 1 ? results.get(0) : null;
    } finally {
        entityManager.close();
    }
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.VacancySkillRepositoryImplementation.java

@Override
public boolean delete(VacancySkill vacancySkill) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean success = true;
    VacancySkill managedVacancySkill = null;
    try {//from w w  w  . java  2  s. co m
        em.getTransaction().begin();
        managedVacancySkill = em.merge(vacancySkill);
        em.remove(managedVacancySkill);
        em.getTransaction().commit();

        success = true;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
            success = false;
        }
        em.close();
    }

    return success;
}

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

/**
 * Update the {@link com.enioka.jqm.jpamodel.History} with the given progress data.
 * //from   ww w.ja v  a  2 s.  c  o m
 * @param msg
 * @throws JqmKillException
 */
private void sendProgress(Integer msg) {
    EntityManager em = Helpers.getNewEm();
    try {
        em.getTransaction().begin();
        this.ji = em.find(JobInstance.class, this.ji.getId(), LockModeType.PESSIMISTIC_WRITE);
        ji.setProgress(msg);
        em.getTransaction().commit();
    } finally {
        em.close();
    }
}

From source file:in.bookmylab.jpa.JpaDAO.java

public List<StaticData> getStaticData() {
    EntityManager em = emf.createEntityManager();
    List<StaticData> data = null;
    try {//from  ww  w .  j av  a2 s .  c o m
        data = em.createNamedQuery("StaticData.findAll", StaticData.class).getResultList();
    } finally {
        em.clear();
        em.close();
    }
    return data;
}

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

/**
 * Register a new infrastructure.//from  w  ww.  ja  v  a 2  s.c  om
 *
 * @param infra The infrastructure to register
 * @return The registered infrastructure
 */
@POST
@Status(Response.Status.CREATED)
@Consumes({ MediaType.APPLICATION_JSON, Constants.INDIGOMIMETYPE })
@Produces(Constants.INDIGOMIMETYPE)
public final Infrastructure createInfrastructure(final Infrastructure infra) {
    Date now = new Date();
    infra.setDateCreated(now);
    EntityManager em = getEntityManager();
    EntityTransaction et = null;
    try {
        et = em.getTransaction();
        et.begin();
        em.persist(infra);
        et.commit();
        log.debug("New infrastructure registered: " + infra.getId());
    } catch (RuntimeException re) {
        if (et != null && et.isActive()) {
            et.rollback();
        }
        log.error("Impossible to create the infrastructure");
        log.error(re);
    } finally {
        em.close();
    }
    return infra;
}

From source file:de.zib.gndms.infra.tests.FileTransferActionTest.java

@BeforeClass(groups = { "net", "db", "sys", "action", "task" })
public void beforeClass() throws ServerException, IOException, ClientException {

    PropertyConfigurator.configure(logFileConfig);
    runDatabase();/*from  w w  w .j  a  v a  2 s . c  o  m*/
    transferData.initialize();

    // create orq
    FileTransferORQ orq = new FileTransferORQ();
    orq.setSourceURI(transferData.getSourceURI());
    orq.setTargetURI(transferData.getDestinationURI());
    orq.setFileMap(transferData.getFileMap());

    // create orq-calc
    FileTransferORQCalculator calc = new FileTransferORQCalculator();
    calc.setORQArguments(orq);
    // calc.setNetAux( getSys().getNetAux() );

    TransientContract con = calc.createOffer();
    PersistentContract pcon = con.acceptAt(new DateTime());

    // creating offertype
    OfferType ot;
    EntityManager em = null;
    try {
        em = getSys().getEntityManagerFactory().createEntityManager();
        ot = em.find(OfferType.class, "http://gndms.zib.de/ORQTypes/FileTransfer");
        if (ot == null) {
            ot = createFTOfferType();
            em.getTransaction().begin();
            em.persist(ot);
            em.getTransaction().commit();
        }

    } finally {
        if (em != null && em.isOpen())
            em.close();
        ot = createFTOfferType();
    }

    // create task
    task = new Task();
    task.setId(getSys().nextUUID());
    task.setDescription(orq.getDescription());
    task.setTerminationTime(pcon.getCurrentTerminationTime());
    task.setOfferType(ot);
    task.setOrq(orq);
    task.setContract(pcon);
    Calendar tt = pcon.getDeadline();
    tt.add(Calendar.YEAR, 10);
    task.setTerminationTime(tt);
}

From source file:com.github.jrh3k5.membership.renewal.mailer.service.jpa.JpaMembershipService.java

@SuppressWarnings("unchecked")
@Override//from   w  w w  .ja  va  2 s  .co m
public Collection<JpaMembershipRecord> getLapsedMemberships(Date lowerExclusiveBound,
        Date upperInclusiveBound) {
    LOGGER.info("Finding members who have lapsed memberships between ({}, {}]", lowerExclusiveBound,
            upperInclusiveBound);
    final EntityManager entityManager = entityManagerFactory.createEntityManager();
    try {
        final Query query = entityManager.createQuery(
                "select m from MembershipRecord m where m.latestMembershipDate > :lowerBound and m.latestMembershipDate <= :upperBound");
        query.setParameter("lowerBound", lowerExclusiveBound);
        query.setParameter("upperBound", upperInclusiveBound);
        return query.getResultList();
    } finally {
        entityManager.close();
    }
}

From source file:in.bookmylab.jpa.JpaDAO.java

public List<ResourceBooking> getSlotsByDate(Date dt) {
    List<ResourceBooking> list = null;
    EntityManager em = emf.createEntityManager();
    try {//from w  w w  .  ja  v a 2s .  c o m
        Query q = em.createNamedQuery("ResourceBooking.findSlotsByDate");
        list = q.setParameter("bookingDate", dt).getResultList();
    } finally {
        em.clear();
        em.close();
    }
    return list;
}

From source file:eu.optimis.trustedinstance.DBStorage.java

@SuppressWarnings("finally")
public boolean delete(String resourceKey) throws Exception {

    boolean result = true;
    EntityManager em = emf.createEntityManager();

    try {//from  w w  w  .ja  va 2s  .c  o  m
        String jpql = "delete from DBStorageEntry se where se.key=\"" + resourceKey + "\"";
        em.getTransaction().begin();
        Query query = em.createQuery(jpql);
        int row = query.executeUpdate();
        em.getTransaction().commit();
        if (row == 0) {
            result = false;
        }
    } catch (Exception e) {
        result = false;
        throw e;
    } finally {
        em.close();
        return result;
    }
}

From source file:com.boylesoftware.web.impl.auth.AbstractUserRecordHandler.java

@Override
public T getUser(final int id, final int salt, final EntityManagerFactory emf) {

    final boolean debug = this.log.isDebugEnabled();

    if (debug) {//  w w  w. j a v a2  s  .co  m
        this.log.debug("looking up user id " + id + ", salt " + salt + " in the database");
        this.log.debug("creating entity manager");
    }
    final EntityManager em = emf.createEntityManager();
    try {
        final T user = em.find(this.userRecordClass, Integer.valueOf(id));

        if ((user == null) || (this.getUserSalt(user) != salt)) {
            if (debug)
                this.log.debug("user not found or salt does not match");
            return null;
        }

        if (debug)
            this.log.debug("user found");
        return user;

    } finally {
        if (debug)
            this.log.debug("closing entity manager");
        em.close();
    }
}