Search in sources :

Example 41 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project enhydrator by AdamBien.

the class CoffeeTestFixture method perform.

public static Object perform(String unitName, Function<EntityManager, ?> statement) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(unitName);
    EntityManager em = emf.createEntityManager();
    EntityTransaction et = em.getTransaction();
    et.begin();
    Object result = statement.apply(em);
    et.commit();
    return result;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory)

Example 42 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project aries by apache.

the class JPAEntityManagerProviderFactoryImpl method getProviderFor.

@Override
public AbstractJPAEntityManagerProvider getProviderFor(EntityManagerFactoryBuilder emfb, Map<String, Object> jpaProperties, Map<String, Object> resourceProviderProperties) {
    checkEnlistment(resourceProviderProperties);
    EntityManagerFactory emf = emfb.createEntityManagerFactory(jpaProperties);
    validateEMF(emf);
    return new JPAEntityManagerProviderImpl(emf, () -> emf.close());
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory)

Example 43 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project wildfly by wildfly.

the class TestPersistenceProvider method createContainerEntityManagerFactory.

@Override
public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) {
    persistenceUnitInfo.put(info.getPersistenceUnitName(), info);
    TestClassTransformer testClassTransformer = new TestClassTransformer();
    info.addTransformer(testClassTransformer);
    TestEntityManagerFactory testEntityManagerFactory = new TestEntityManagerFactory();
    Class[] targetInterfaces = javax.persistence.EntityManagerFactory.class.getInterfaces();
    // include extra element for extensionClass
    Class[] proxyInterfaces = new Class[targetInterfaces.length + 1];
    boolean alreadyHasInterfaceClass = false;
    for (int interfaceIndex = 0; interfaceIndex < targetInterfaces.length; interfaceIndex++) {
        Class interfaceClass = targetInterfaces[interfaceIndex];
        if (interfaceClass.equals(javax.persistence.EntityManagerFactory.class)) {
            // targetInterfaces already has all interfaces
            proxyInterfaces = targetInterfaces;
            alreadyHasInterfaceClass = true;
            break;
        }
        proxyInterfaces[1 + interfaceIndex] = interfaceClass;
    }
    if (!alreadyHasInterfaceClass) {
        proxyInterfaces[0] = javax.persistence.EntityManagerFactory.class;
    }
    EntityManagerFactory proxyEntityManagerFactory = (EntityManagerFactory) Proxy.newProxyInstance(//use the target classloader so the proxy has the same scope
    testEntityManagerFactory.getClass().getClassLoader(), proxyInterfaces, testEntityManagerFactory);
    return proxyEntityManagerFactory;
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory)

Example 44 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project wildfly by wildfly.

the class TestPersistenceProvider method createContainerEntityManagerFactory.

@Override
public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) {
    persistenceUnitInfo.put(info.getPersistenceUnitName(), info);
    TestEntityManagerFactory testEntityManagerFactory = new TestEntityManagerFactory();
    Class[] targetInterfaces = EntityManagerFactory.class.getInterfaces();
    // include extra element for extensionClass
    Class[] proxyInterfaces = new Class[targetInterfaces.length + 1];
    boolean alreadyHasInterfaceClass = false;
    for (int interfaceIndex = 0; interfaceIndex < targetInterfaces.length; interfaceIndex++) {
        Class interfaceClass = targetInterfaces[interfaceIndex];
        if (interfaceClass.equals(EntityManagerFactory.class)) {
            // targetInterfaces already has all interfaces
            proxyInterfaces = targetInterfaces;
            alreadyHasInterfaceClass = true;
            break;
        }
        proxyInterfaces[1 + interfaceIndex] = interfaceClass;
    }
    if (!alreadyHasInterfaceClass) {
        proxyInterfaces[0] = EntityManagerFactory.class;
    }
    EntityManagerFactory proxyEntityManagerFactory = (EntityManagerFactory) Proxy.newProxyInstance(//use the target classloader so the proxy has the same scope
    testEntityManagerFactory.getClass().getClassLoader(), proxyInterfaces, testEntityManagerFactory);
    return proxyEntityManagerFactory;
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory)

Example 45 with EntityManagerFactory

use of javax.persistence.EntityManagerFactory in project wildfly by wildfly.

the class SingletonBean method sleepAndDestroy.

@PreDestroy
private void sleepAndDestroy() throws Exception {
    logger.trace("Sleeping for 3 seconds while destroying singleton bean " + this);
    // sleep for a while just to reproduce a race condition with EntityManagerFactory being closed before
    // the singleton bean can finish its pre-destroy
    Thread.sleep(3000);
    logger.trace("Woke up after 3 seconds while destroying singleton bean " + this);
    final EntityManagerFactory entityManagerFactory = this.entityManager.getEntityManagerFactory();
    boolean emFactoryOpen = entityManagerFactory.isOpen();
    if (!emFactoryOpen) {
        throw new RuntimeException("Entitymanager factory: " + entityManagerFactory + " has been closed " + "even before singleton bean " + this + " could complete its @PreDestroy");
    }
}
Also used : EntityManagerFactory(javax.persistence.EntityManagerFactory) PreDestroy(javax.annotation.PreDestroy)

Aggregations

EntityManagerFactory (javax.persistence.EntityManagerFactory)165 Test (org.junit.Test)72 EntityManager (javax.persistence.EntityManager)55 HashMap (java.util.HashMap)21 EJBException (javax.ejb.EJBException)19 AssertionFailedError (junit.framework.AssertionFailedError)17 TestFailureException (org.apache.openejb.test.TestFailureException)17 JMSException (javax.jms.JMSException)15 Map (java.util.Map)14 RemoteException (java.rmi.RemoteException)11 InitialContext (javax.naming.InitialContext)11 EntityTransaction (javax.persistence.EntityTransaction)10 PersistenceUnitDescriptorAdapter (org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter)10 NamingException (javax.naming.NamingException)8 PersistenceProvider (javax.persistence.spi.PersistenceProvider)7 BeanContext (org.apache.openejb.BeanContext)7 Item (org.hibernate.jpa.test.Item)7 Method (java.lang.reflect.Method)6 URL (java.net.URL)6 ArrayList (java.util.ArrayList)6