Example usage for javax.persistence EntityManagerFactory isOpen

List of usage examples for javax.persistence EntityManagerFactory isOpen

Introduction

In this page you can find the example usage for javax.persistence EntityManagerFactory isOpen.

Prototype

public boolean isOpen();

Source Link

Document

Indicates whether the factory is open.

Usage

From source file:es.us.isa.ideas.utilities.PopulateDatabase.java

public static void main(String[] args) {

    ApplicationContext ctx;/*from w w  w.  jav  a  2s.  c  o  m*/
    EntityManagerFactory emf;
    EntityManager em;
    EntityTransaction et;

    ctx = new ClassPathXmlApplicationContext("utilities/PopulateDatabase.xml");

    emf = Persistence.createEntityManagerFactory("persistenceUnit");
    em = emf.createEntityManager();
    et = em.getTransaction();

    et.begin();
    try {
        for (Entry<String, Object> entry : ctx.getBeansWithAnnotation(Entity.class).entrySet()) {
            em.persist(entry.getValue());
            System.out.println(String.format("Persisting (%s, %s@%d)", entry.getKey(),
                    entry.getValue().getClass().getName(), entry.getValue().hashCode()));
        }
        et.commit();
    } catch (Exception oops) {
        oops.printStackTrace();
        et.rollback();
        oops.printStackTrace();
    } finally {
        if (em.isOpen())
            em.close();
        if (emf.isOpen())
            emf.close();
        ((ClassPathXmlApplicationContext) ctx).close();
    }
}

From source file:org.drools.semantics.lang.dl.DL_9_CompilationTest.java

private void checkSQLRefresh(Object obj, ClassLoader urlk, String punit) {
    ClassLoader oldKL = Thread.currentThread().getContextClassLoader();
    EntityManagerFactory emf = null;
    EntityManager em = null;/*ww  w . j av a2  s  . c om*/
    try {
        Thread.currentThread().setContextClassLoader(urlk);

        HashMap props = new HashMap();
        props.put("hibernate.hbm2ddl.auto", "create-drop");
        emf = Persistence.createEntityManagerFactory(punit, props);

        em = emf.createEntityManager();

        checkJPARefresh(obj, ((UIdAble) obj).getDyEntryId(), em);

    } finally {
        Thread.currentThread().setContextClassLoader(oldKL);
        if (em != null && em.isOpen()) {
            em.clear();
            em.close();
        }
        if (emf != null && emf.isOpen()) {
            emf.close();
        }
    }

}

From source file:org.eclipse.gyrex.persistence.eclipselink.internal.EclipseLinkRepositoryImpl.java

/**
 * Returns an EntityManagerFactory for the specified content type.
 * <p>//  w w  w.java 2 s. c  o  m
 * If the content type contains a parameter
 * {@link RepositoryContentType#getParameter(String)
 * <code>persistenceUnitName</code>} its value will be used. If no such
 * parameter is specified the
 * {@link RepositoryContentType#getMediaTypeSubType() media type sub type}
 * will be used.
 * </p>
 * 
 * @param contentType
 * @return the entity manager
 */
@Override
public EntityManagerFactory getEntityManagerFactory(final RepositoryContentType contentType) {
    WeakReference<EntityManagerFactory> factoryRef = emfCacheByContentType.get(contentType);
    EntityManagerFactory factory = factoryRef != null ? factoryRef.get() : null;
    if ((null == factory) || !factory.isOpen()) {
        synchronized (emfCacheByContentType) {
            factoryRef = emfCacheByContentType.get(contentType);
            factory = factoryRef != null ? factoryRef.get() : null;
            if ((null == factory) || !factory.isOpen()) {
                factory = createEntityManagerFactory(contentType);
                emfCacheByContentType.put(contentType, new WeakReference<EntityManagerFactory>(factory));
            }
        }
    }
    return factory;
}

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

/**
 * //from   w w  w . j  a  v a 2  s.  com
 * @param factory The factory from which to retrieve the property.
 * @param key The key/name of the property to retrieve.
 * @return the string value of the retrieved property. Returns 
 *         <code>null</code> if the factory does not contain the desired 
 *         property or if the value of the property is <code>null</code>
 *         or if the string representation of the property value is
 *         <code>null</code>.
 */
private static String getFactoryProperty(EntityManagerFactory factory, String key) {

    if (factory != null && factory.isOpen()) {
        return ObjectUtils.toString(factory.getProperties().get(key));
    }

    return null;
}

From source file:org.kie.server.services.jbpm.JbpmKieServerExtension.java

@Override
public void destroy(KieServerImpl kieServer, KieServerRegistry registry) {
    ((AbstractDeploymentService) deploymentService).shutdown();

    if (executorService != null) {
        executorService.destroy();//www  .ja v a2 s. co  m
    }

    EntityManagerFactory emf = EntityManagerFactoryManager.get().remove(persistenceUnitName);
    if (emf != null && emf.isOpen()) {
        emf.close();
    }
}

From source file:org.ualerts.testing.jpa.EntityManagerFactoryUtil.java

/**
 * Creates a new {@code EntityManagerFactory} configured using the 
 * properties specified in the given resource.
 * @param propertiesResource a class path resource containing properties
 * @return factory object/*  w  w  w  . j a  va2  s .  c  o  m*/
 * @throws IOException
 * @throws PersistenceException
 */
public static EntityManagerFactory createEntityManagerFactory(String propertiesResource)
        throws IOException, PersistenceException {

    if (factoryMap.containsKey(propertiesResource)) {
        EntityManagerFactory emf = factoryMap.get(propertiesResource);
        if (emf.isOpen()) {
            return emf;
        }
    }

    Properties properties = loadProperties(propertiesResource);
    String persistenceUnit = properties.getProperty(PERSISTENCE_UNIT_PROPERTY, DEFAULT_PERSISTENCE_UNIT_NAME);

    factoryMap.put(propertiesResource,
            createEntityManagerFactory(properties, persistenceUnit, propertiesResource));

    return factoryMap.get(propertiesResource);
}

From source file:utilities.PopulateDatabase.java

public static void main(String[] args) throws Throwable {
    ApplicationContext applicationContext;
    EntityManagerFactory entityManagerFactory;
    EntityManager entityManager;//from  w  ww.j a  va 2 s .co  m
    EntityTransaction entityTransaction;

    applicationContext = new ClassPathXmlApplicationContext("classpath:PopulateDatabase.xml");

    entityManagerFactory = Persistence.createEntityManagerFactory(PersistenceUnit);
    entityManager = entityManagerFactory.createEntityManager();
    entityTransaction = entityManager.getTransaction();

    initialise(entityManagerFactory, entityManager);

    entityTransaction.begin();
    try {
        for (Entry<String, Object> entry : applicationContext.getBeansWithAnnotation(Entity.class).entrySet()) {
            String beanName;
            DomainEntity entity;

            beanName = entry.getKey();
            entity = (DomainEntity) entry.getValue();
            entityManager.persist(entity);
            System.out.println(String.format("Persisting (%s, %s, %d)", beanName, entity.getClass().getName(),
                    entity.getId()));
        }
        entityTransaction.commit();
    } catch (Exception oops) {
        oops.printStackTrace();
        entityTransaction.rollback();
    } finally {
        if (entityManager.isOpen())
            entityManager.close();
        if (entityManagerFactory.isOpen())
            entityManagerFactory.close();
    }
}