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.sixsq.slipstream.persistence.Run.java

public static Run updateRunState(Run run, States newState, boolean retry) {
    EntityManager em = PersistenceUtil.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();//w  w  w .ja  va2  s.  com
    try {
        run = Run.loadFromUuid(run.getUuid(), em);
        run.setState(newState);
        transaction.commit();
        em.close();
    } catch (Exception e) {
        String error = "error setting run state: " + newState;
        if (retry) {
            Logger.getLogger("restlet").warning(error + " retrying...");
        } else {
            Logger.getLogger("restlet").severe(error);
        }
        // retry once
        if (retry) {
            updateRunState(run, newState, false);
        }
    }
    return run;
}

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

public List<DataSource> getAllDataSources() {
    EntityManager em = getEntityManager();
    try {/* www .  j  av a2s .c o  m*/
        return em.createQuery("SELECT ds FROM DataSource ds", DataSource.class).getResultList();
    } finally {
        em.close();
    }
}

From source file:com.sixsq.slipstream.persistence.Run.java

@SuppressWarnings("unchecked")
public static List<Run> listOldTransient(User user, int timeout)
        throws ConfigurationException, ValidationException {
    if (timeout <= 0) {
        timeout = DEFAULT_TIMEOUT;
    }/*from w  ww .  j  a va  2 s. c  o m*/
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MINUTE, -timeout);
    Date back = calendar.getTime();

    EntityManager em = PersistenceUtil.createEntityManager();
    Query q = createNamedQuery(em, "oldInStatesRuns");
    q.setParameter("user", user.getName());
    q.setParameter("before", back);
    q.setParameter("states", States.transition());
    List<Run> runs = q.getResultList();
    em.close();
    return runs;
}

From source file:edu.kit.dama.mdm.dataorganization.impl.jpa.DataOrganizerImpl.java

@Override
public NodeId getRootNodeId(DigitalObjectId digitalObjectId, String viewName) {
    PersistenceFacade pf = PersistenceFacade.getInstance();
    EntityManager em = pf.getEntityManagerFactory().createEntityManager();
    DataOrganizationNode root = pf.getRootNode(digitalObjectId, em, viewName);
    em.close();
    return root.getTransientNodeId();
}

From source file:fr.xebia.demo.wicket.blog.service.GenericService.java

protected void closeEntityManager() throws PersistenceException {
    EntityManager entityManager = entityManagerCache.get();
    entityManagerCache.set(null);//from  ww  w  . ja  v  a 2s  .  co  m
    if (entityManager != null) {
        entityManager.close();
    }
}

From source file:com.soen.smbank.dao.ObjectDao.java

public ArrayList getAllObjects(Class<T> ClassName, String tableName) {
    EntityManager em = this.getEMF().createEntityManager();
    ArrayList entities = null;//from  ww  w.  j a  v a2  s. c  o  m
    try {
        entities = (ArrayList) em.createQuery("SELECT tb FROM " + tableName + " tb ", ClassName)
                .getResultList();

        return entities;
    } finally {
        em.close();
    }
}

From source file:io.symcpe.hendrix.api.ApplicationManager.java

public void init(AppConfig appConfiguration) {
    config = new Properties(System.getProperties());
    if (System.getenv(PROP_CONFIG_FILE) != null) {
        try {/* w  w w  . j av a2 s.c o  m*/
            config.load(new FileInputStream(System.getenv(PROP_CONFIG_FILE)));
        } catch (IOException e) {
            throw new RuntimeException("Configuration file not loaded", e);
        }
    } else if (System.getProperty(PROP_CONFIG_FILE) != null) {
        try {
            config.load(new FileInputStream(System.getProperty(PROP_CONFIG_FILE)));
        } catch (IOException e) {
            throw new RuntimeException("Configuration file not loaded", e);
        }
    } else {
        try {
            config.load(
                    ApplicationManager.class.getClassLoader().getResourceAsStream("default-config.properties"));
        } catch (IOException e) {
            throw new RuntimeException("Default configuration file not loaded", e);
        }
    }
    try {
        Utils.createDatabase(config.getProperty(JAVAX_PERSISTENCE_JDBC_URL),
                config.getProperty(JAVAX_PERSISTENCE_JDBC_DB, "hendrix"),
                config.getProperty(JAVAX_PERSISTENCE_JDBC_USER),
                config.getProperty(JAVAX_PERSISTENCE_JDBC_PASSWORD),
                config.getProperty(JAVAX_PERSISTENCE_JDBC_DRIVER));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    config.setProperty(JAVAX_PERSISTENCE_JDBC_URL, config.getProperty(JAVAX_PERSISTENCE_JDBC_URL)
            + config.getProperty(JAVAX_PERSISTENCE_JDBC_DB, "hendrix"));
    factory = Persistence.createEntityManagerFactory("hendrix", config);
    EntityManager em = factory.createEntityManager();
    System.out.println("Rules stats" + em.createNamedQuery("Rules.stats").getResultList());
    em.close();
    if (!LOCAL) {
        initKafkaConnection();
    }
}

From source file:org.spc.ofp.tubs.domain.purseseine.TripRepository.java

public void update(final PurseSeineTrip trip) {
    final EntityManager mgr = emf.createEntityManager();
    final EntityTransaction xa = mgr.getTransaction();
    try {//www . j av a  2  s. c om
        xa.begin();
        mgr.merge(trip);
        xa.commit();
    } catch (Exception ex) {
        if (xa.isActive()) {
            xa.rollback();
        }
    } finally {
        mgr.close();
    }
}

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

@Override
public List<Troop> findTroopByName(String name) throws IllegalArgumentException {
    List<Troop> troop;//  w  ww .  ja v  a  2 s. c o m
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    troop = em.createQuery("SELECT t FROM Troop t WHERE t.name = :name").setParameter("name", name)
            .getResultList();
    //query.setParameter("name", name);
    em.getTransaction().commit();
    em.close();
    return troop;
}

From source file:ejb.bean.UsuarioDAOJPAImplBean.java

/**Mtodo para realizar a busca de usurio pelo ID.
 * @author Richel Sensineli/*w  w w.j a  v a 2s. c o  m*/
 * @param id int - ID do usurio
 * @return Usuario usuario - Objeto Usuario
 */
@Override
public Usuario buscaUsuarioPorId(final int id) throws UsuarioNaoEncontradoException {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("UsuarioPU");
    EntityManager em = emf.createEntityManager();
    Usuario u = em.find(UsuarioImpl.class, id);
    if (u == null) {
        throw new UsuarioNaoEncontradoException("usuario no encontrado");
    }
    em.clear();
    em.close();
    emf.close();
    return u;
}