Search in sources :

Example 1 with EntityManagerFactoryImpl

use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.

the class PersistenceProvider method createContainerEntityManagerFactoryImpl.

protected EntityManagerFactory createContainerEntityManagerFactoryImpl(PersistenceUnitInfo info, Map properties, boolean requiresConnection) {
    // Record that we are inside a JEE container to allow weaving for non managed persistence units.
    JavaSECMPInitializer.setIsInContainer(true);
    Map nonNullProperties = (properties == null) ? new HashMap<>() : properties;
    String forceTargetServer = EntityManagerFactoryProvider.getConfigPropertyAsString(SystemProperties.ENFORCE_TARGET_SERVER, null);
    if ("true".equalsIgnoreCase(forceTargetServer)) {
        nonNullProperties.remove(PersistenceUnitProperties.TARGET_SERVER);
    }
    EntityManagerSetupImpl emSetupImpl = null;
    if (EntityManagerSetupImpl.mustBeCompositeMember(info)) {
        // persistence unit cannot be used standalone (only as a composite member).
        // still the factory will be created but attempt to createEntityManager would cause an exception.
        emSetupImpl = new EntityManagerSetupImpl(info.getPersistenceUnitName(), info.getPersistenceUnitName());
        // predeploy assigns puInfo and does not do anything else.
        // the session is not created, no need to add emSetupImpl to the global map.
        emSetupImpl.predeploy(info, nonNullProperties);
    } else {
        boolean isNew = false;
        ClassTransformer transformer = null;
        String uniqueName = PersistenceUnitProcessor.buildPersistenceUnitName(info.getPersistenceUnitRootUrl(), info.getPersistenceUnitName());
        String sessionName = EntityManagerSetupImpl.getOrBuildSessionName(nonNullProperties, info, uniqueName);
        synchronized (EntityManagerFactoryProvider.emSetupImpls) {
            emSetupImpl = EntityManagerFactoryProvider.getEntityManagerSetupImpl(sessionName);
            if (emSetupImpl == null) {
                emSetupImpl = new EntityManagerSetupImpl(uniqueName, sessionName);
                isNew = true;
                emSetupImpl.setIsInContainerMode(true);
                // if predeploy fails then emSetupImpl shouldn't be added to FactoryProvider
                transformer = emSetupImpl.predeploy(info, nonNullProperties);
                EntityManagerFactoryProvider.addEntityManagerSetupImpl(sessionName, emSetupImpl);
            }
        }
        if (!isNew) {
            if (!uniqueName.equals(emSetupImpl.getPersistenceUnitUniqueName())) {
                throw PersistenceUnitLoadingException.sessionNameAlreadyInUse(sessionName, uniqueName, emSetupImpl.getPersistenceUnitUniqueName());
            }
            // synchronized to prevent undeploying by other threads.
            boolean undeployed = false;
            synchronized (emSetupImpl) {
                if (emSetupImpl.isUndeployed()) {
                    undeployed = true;
                } else {
                    // emSetupImpl has been already predeployed, predeploy will just increment factoryCount.
                    transformer = emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(), nonNullProperties);
                }
            }
            if (undeployed) {
                // it has been undeployed by factory.close() in another thread - start all over again.
                return createContainerEntityManagerFactory(info, properties);
            }
        }
        if (transformer != null) {
            info.addTransformer(transformer);
        }
    }
    EntityManagerFactoryImpl factory = null;
    try {
        factory = new EntityManagerFactoryImpl(emSetupImpl, nonNullProperties);
        emSetupImpl.setRequiresConnection(requiresConnection);
        emSetupImpl.preInitializeCanonicalMetamodel(factory);
        // This code has been added to allow validation to occur without actually calling createEntityManager
        if (emSetupImpl.shouldGetSessionOnCreateFactory(nonNullProperties)) {
            factory.getDatabaseSession();
        }
        return factory;
    } catch (RuntimeException ex) {
        if (factory != null) {
            factory.close();
        } else {
            emSetupImpl.undeploy();
        }
        throw ex;
    }
}
Also used : EntityManagerSetupImpl(org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl) ClassTransformer(jakarta.persistence.spi.ClassTransformer) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with EntityManagerFactoryImpl

use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.

the class PersistenceProvider method createEntityManagerFactoryImpl.

/**
 * Internal method to return the entity manager factory.
 */
protected EntityManagerFactoryImpl createEntityManagerFactoryImpl(PersistenceUnitInfo puInfo, Map properties, boolean requiresConnection) {
    if (puInfo != null) {
        boolean isNew = false;
        // the name the uniquely defines the pu
        String uniqueName = null;
        String sessionName = null;
        EntityManagerSetupImpl emSetupImpl = null;
        String puName = puInfo.getPersistenceUnitName();
        JPAInitializer initializer = getInitializer(puInfo.getPersistenceUnitName(), properties);
        try {
            if (EntityManagerSetupImpl.mustBeCompositeMember(puInfo)) {
                // Persistence unit cannot be used standalone (only as a composite member).
                // Still the factory will be created but attempt to createEntityManager would cause an exception.
                emSetupImpl = new EntityManagerSetupImpl(puName, puName);
                // Predeploy assigns puInfo and does not do anything else.
                // The session is not created, no need to add emSetupImpl to the global map.
                emSetupImpl.predeploy(puInfo, properties);
                isNew = true;
            } else {
                if (initializer.isPersistenceUnitUniquelyDefinedByName()) {
                    uniqueName = puName;
                } else {
                    uniqueName = initializer.createUniquePersistenceUnitName(puInfo);
                }
                sessionName = EntityManagerSetupImpl.getOrBuildSessionName(properties, puInfo, uniqueName);
                synchronized (EntityManagerFactoryProvider.emSetupImpls) {
                    emSetupImpl = EntityManagerFactoryProvider.getEntityManagerSetupImpl(sessionName);
                    if (emSetupImpl == null) {
                        // there may be initial emSetupImpl cached in Initializer - remove it and use.
                        emSetupImpl = initializer.extractInitialEmSetupImpl(puName);
                        if (emSetupImpl != null) {
                            // change the name
                            emSetupImpl.changeSessionName(sessionName);
                        } else {
                            // create and predeploy a new emSetupImpl
                            emSetupImpl = initializer.callPredeploy((SEPersistenceUnitInfo) puInfo, properties, uniqueName, sessionName);
                        }
                        // emSetupImpl has been already predeployed, predeploy will just increment factoryCount.
                        emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(), properties);
                        EntityManagerFactoryProvider.addEntityManagerSetupImpl(sessionName, emSetupImpl);
                        isNew = true;
                    }
                }
            }
        } catch (Exception e) {
            throw PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(initializer.getInitializationClassLoader(), e);
        }
        if (!isNew) {
            if (!uniqueName.equals(emSetupImpl.getPersistenceUnitUniqueName())) {
                throw PersistenceUnitLoadingException.sessionNameAlreadyInUse(sessionName, uniqueName, emSetupImpl.getPersistenceUnitUniqueName());
            }
            // synchronized to prevent undeploying by other threads.
            boolean undeployed = false;
            synchronized (emSetupImpl) {
                if (emSetupImpl.isUndeployed()) {
                    undeployed = true;
                } else {
                    // emSetupImpl has been already predeployed, predeploy will just increment factoryCount.
                    emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(), properties);
                }
            }
            if (undeployed) {
                // it has been undeployed by factory.close() in another thread - start all over again.
                return (EntityManagerFactoryImpl) createEntityManagerFactory(puName, properties);
            }
        }
        EntityManagerFactoryImpl factory = null;
        try {
            factory = new EntityManagerFactoryImpl(emSetupImpl, properties);
            emSetupImpl.setRequiresConnection(requiresConnection);
            emSetupImpl.preInitializeCanonicalMetamodel(factory);
            if (emSetupImpl.shouldGetSessionOnCreateFactory(properties)) {
                factory.getDatabaseSession();
            }
            return factory;
        } catch (RuntimeException ex) {
            if (factory != null) {
                factory.close();
            } else {
                emSetupImpl.undeploy();
            }
            throw ex;
        }
    }
    return null;
}
Also used : JPAInitializer(org.eclipse.persistence.internal.jpa.deployment.JPAInitializer) EntityManagerSetupImpl(org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) SEPersistenceUnitInfo(org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo) PersistenceUnitLoadingException(org.eclipse.persistence.exceptions.PersistenceUnitLoadingException) PersistenceException(jakarta.persistence.PersistenceException)

Example 3 with EntityManagerFactoryImpl

use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.

the class PersistenceFactoryBase method get.

/**
 * Gets existing persistence context or create new based on given parameters if it doesn't exist.
 */
@Override
public PersistenceContext get(String persistenceUnitName, URI defaultURI, String version, Map<String, Object> initializationProperties) {
    PersistenceContext persistenceContext = getDynamicPersistenceContext(persistenceUnitName, version);
    if (persistenceContext == null) {
        try {
            DynamicClassLoader dcl = new DynamicRestClassLoader(Thread.currentThread().getContextClassLoader());
            Map<String, Object> properties = new HashMap<>();
            properties.put(PersistenceUnitProperties.CLASSLOADER, dcl);
            if (initializationProperties != null) {
                properties.putAll(initializationProperties);
            }
            properties.putIfAbsent(PersistenceUnitProperties.WEAVING_REST, "true");
            EntityManagerFactoryImpl factory = (EntityManagerFactoryImpl) Persistence.createEntityManagerFactory(persistenceUnitName, properties);
            ClassLoader sessionLoader = factory.getServerSession().getLoader();
            if (!DynamicClassLoader.class.isAssignableFrom(sessionLoader.getClass())) {
                properties = new HashMap<>();
                dcl = new DynamicRestClassLoader(sessionLoader);
                properties.put(PersistenceUnitProperties.CLASSLOADER, dcl);
                if (initializationProperties != null) {
                    properties.putAll(initializationProperties);
                }
                properties.putIfAbsent(PersistenceUnitProperties.WEAVING_REST, "true");
                factory.refreshMetadata(properties);
            }
            persistenceContext = bootstrapPersistenceContext(persistenceUnitName, factory, defaultURI, version, true);
        } catch (Exception e) {
            JPARSLogger.exception("exception_creating_persistence_context", new Object[] { persistenceUnitName, e.toString() }, e);
        }
    }
    if ((persistenceContext != null) && (!persistenceContext.isWeavingEnabled())) {
        JPARSLogger.error(persistenceContext.getServerSession().getSessionLog(), "weaving_required_for_relationships", new Object[] {});
        throw JPARSException.invalidConfiguration();
    }
    return persistenceContext;
}
Also used : DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) HashMap(java.util.HashMap) DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException)

Example 4 with EntityManagerFactoryImpl

use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.

the class RuntimeFactory method createEntityManagerFactory.

public EntityManagerFactory createEntityManagerFactory(PersistenceUnit pu) {
    EntityManagerSetupImpl emSetupImpl = null;
    boolean isNew = false;
    // the name that uniquely defines persistence unit
    String name = pu.getName();
    pu.setProperty(PersistenceUnitProperties.METADATA_SOURCE, new MetadataSource(pu));
    SEPersistenceUnitInfo puInfo = (SEPersistenceUnitInfo) pu.getPersistenceUnitInfo();
    JPAInitializer initializer = new PersistenceProvider().getInitializer(name, null);
    Map<String, Object> props = new HashMap<String, Object>();
    String uniqueName = initializer.createUniquePersistenceUnitName(puInfo);
    String sessionName = EntityManagerSetupImpl.getOrBuildSessionName(props, puInfo, uniqueName);
    try {
        synchronized (EntityManagerFactoryProvider.emSetupImpls) {
            emSetupImpl = EntityManagerFactoryProvider.getEntityManagerSetupImpl(sessionName);
            if (emSetupImpl != null) {
                if (puInfo.getClassLoader() != emSetupImpl.getPersistenceUnitInfo().getClassLoader()) {
                    emSetupImpl.undeploy();
                    EntityManagerFactoryProvider.emSetupImpls.remove(sessionName);
                    SessionManager manager = SessionManager.getManager();
                    if (manager.getSessions().containsKey(sessionName)) {
                        manager.destroySession(sessionName);
                    }
                    manager.destroy();
                    emSetupImpl = null;
                }
            }
            if (emSetupImpl == null) {
                // there may be initial emSetupImpl cached in Initializer - remove it and use.
                emSetupImpl = initializer.extractInitialEmSetupImpl(name);
                if (emSetupImpl == null) {
                    // create and predeploy a new emSetupImpl
                    emSetupImpl = initializer.callPredeploy(puInfo, props, uniqueName, sessionName);
                } else {
                    // change the name
                    emSetupImpl.changeSessionName(sessionName);
                }
                // emSetupImpl has been already predeployed, predeploy will just increment factoryCount.
                emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(), props);
                EntityManagerFactoryProvider.addEntityManagerSetupImpl(sessionName, emSetupImpl);
                isNew = true;
            }
        }
    } catch (Exception e) {
        throw PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(initializer.getInitializationClassLoader(), e);
    }
    if (!isNew) {
        if (!uniqueName.equals(emSetupImpl.getPersistenceUnitUniqueName())) {
            throw PersistenceUnitLoadingException.sessionNameAlreadyInUse(sessionName, uniqueName, emSetupImpl.getPersistenceUnitUniqueName());
        }
        // synchronized to prevent undeploying by other threads.
        boolean undeployed = false;
        synchronized (emSetupImpl) {
            if (emSetupImpl.isUndeployed()) {
                undeployed = true;
            }
            // emSetupImpl has been already predeployed, predeploy will just increment factoryCount.
            emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(), props);
        }
        if (undeployed) {
            // start all over again.
            return createEntityManagerFactory(pu);
        }
    }
    EntityManagerFactoryImpl factory = null;
    try {
        factory = new EntityManagerFactoryImpl(emSetupImpl, props);
        // actually calling createEntityManager
        if (emSetupImpl.shouldGetSessionOnCreateFactory(props)) {
            factory.getDatabaseSession();
        }
    } catch (RuntimeException ex) {
        if (factory != null) {
            factory.close();
        } else {
            emSetupImpl.undeploy();
        }
        throw ex;
    }
    return factory;
}
Also used : HashMap(java.util.HashMap) SessionManager(org.eclipse.persistence.sessions.factories.SessionManager) JPAInitializer(org.eclipse.persistence.internal.jpa.deployment.JPAInitializer) PersistenceProvider(org.eclipse.persistence.jpa.PersistenceProvider) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) MetadataSource(org.eclipse.persistence.internal.jpa.config.persistenceunit.MetadataSource) SEPersistenceUnitInfo(org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo) PersistenceUnitLoadingException(org.eclipse.persistence.exceptions.PersistenceUnitLoadingException) EntityManagerSetupImpl(org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl)

Example 5 with EntityManagerFactoryImpl

use of org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl in project eclipselink by eclipse-ee4j.

the class TestJNDIConnector method testLibertTargetServerLookupType.

@Test
public void testLibertTargetServerLookupType() {
    ServerSession session = ((EntityManagerFactoryImpl) libertyEmf).getServerSession();
    _connector.connect(new Properties(), session);
    Assert.assertEquals(String.class, _handler.getParamType());
}
Also used : ServerSession(org.eclipse.persistence.sessions.server.ServerSession) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

EntityManagerFactoryImpl (org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl)22 ServerSession (org.eclipse.persistence.sessions.server.ServerSession)10 HashMap (java.util.HashMap)9 Test (org.junit.Test)8 EntityManager (jakarta.persistence.EntityManager)5 Properties (java.util.Properties)5 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)4 EntityManagerSetupImpl (org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl)4 EntityTransaction (jakarta.persistence.EntityTransaction)3 PersistenceException (jakarta.persistence.PersistenceException)3 Map (java.util.Map)3 PersistenceUnitLoadingException (org.eclipse.persistence.exceptions.PersistenceUnitLoadingException)3 SEPersistenceUnitInfo (org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo)3 JpaEntityManagerFactory (org.eclipse.persistence.jpa.JpaEntityManagerFactory)3 Dog (org.eclipse.persistence.jpa.test.basic.model.Dog)3 Person (org.eclipse.persistence.jpa.test.basic.model.Person)3 XmlFish (org.eclipse.persistence.jpa.test.basic.model.XmlFish)3 ConnectionPolicy (org.eclipse.persistence.sessions.server.ConnectionPolicy)3 RollbackException (jakarta.persistence.RollbackException)2 MultitenantPolicy (org.eclipse.persistence.descriptors.MultitenantPolicy)2