Search in sources :

Example 1 with EntityManagerSetupImpl

use of org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl in project blaze-persistence by Blazebit.

the class AbstractPersistenceTest method clearSchemaUsingJpa.

@Override
protected void clearSchemaUsingJpa() {
    Map<String, EntityManagerSetupImpl> emSetupImpls = EntityManagerFactoryProvider.getEmSetupImpls();
    Map<String, EntityManagerSetupImpl> copy = new HashMap<>(emSetupImpls);
    emSetupImpls.clear();
    SessionManager manager = SessionManager.getManager();
    for (EntityManagerSetupImpl emSetupImpl : copy.values()) {
        manager.getSessions().remove(emSetupImpl.getSessionName());
    }
    try {
        super.clearSchemaUsingJpa();
    } finally {
        emSetupImpls.putAll(copy);
        for (EntityManagerSetupImpl emSetupImpl : copy.values()) {
            manager.addSession(emSetupImpl.getSession());
        }
    }
}
Also used : HashMap(java.util.HashMap) SessionManager(org.eclipse.persistence.sessions.factories.SessionManager) EntityManagerSetupImpl(org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl)

Example 2 with EntityManagerSetupImpl

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

the class StaticWeaveClassTransformer method buildClassTransformers.

/**
 * The method creates classtransformer list corresponding to each persistence unit.
 */
private void buildClassTransformers(URL inputArchiveURL, String persistenceXMLLocation, ClassLoader aclassloader) throws URISyntaxException, IOException {
    if (classTransformers != null) {
        return;
    } else {
        classTransformers = new ArrayList<ClassTransformer>();
    }
    Archive archive = null;
    try {
        archive = (new ArchiveFactoryImpl()).createArchive(inputArchiveURL, persistenceXMLLocation == null ? PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML_DEFAULT : persistenceXMLLocation, null);
        List<SEPersistenceUnitInfo> persistenceUnitsList = PersistenceUnitProcessor.processPersistenceArchive(archive, aclassloader);
        if (persistenceUnitsList == null) {
            throw PersistenceUnitLoadingException.couldNotGetUnitInfoFromUrl(inputArchiveURL);
        }
        Map<Object, Object> emptyMap = new HashMap<>(0);
        Iterator<SEPersistenceUnitInfo> persistenceUnitsIterator = persistenceUnitsList.iterator();
        while (persistenceUnitsIterator.hasNext()) {
            SEPersistenceUnitInfo unitInfo = persistenceUnitsIterator.next();
            // build class transformer.
            String puName = unitInfo.getPersistenceUnitName();
            String sessionName = (String) unitInfo.getProperties().get(PersistenceUnitProperties.SESSION_NAME);
            if (sessionName == null) {
                sessionName = puName;
            }
            EntityManagerSetupImpl emSetupImpl = new EntityManagerSetupImpl(puName, sessionName);
            // indicates that predeploy is used for static weaving, also passes logging parameters
            emSetupImpl.setStaticWeaveInfo(this.info);
            ClassTransformer transformer = emSetupImpl.predeploy(unitInfo, emptyMap);
            if (transformer != null) {
                classTransformers.add(transformer);
            }
        }
    } catch (ZipException e) {
        throw StaticWeaveException.exceptionOpeningArchive(inputArchiveURL, e);
    } finally {
        if (archive != null) {
            archive.close();
        }
    }
}
Also used : Archive(org.eclipse.persistence.jpa.Archive) HashMap(java.util.HashMap) ClassTransformer(jakarta.persistence.spi.ClassTransformer) ZipException(java.util.zip.ZipException) SEPersistenceUnitInfo(org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo) EntityManagerSetupImpl(org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl) ArchiveFactoryImpl(org.eclipse.persistence.internal.jpa.deployment.ArchiveFactoryImpl)

Example 3 with EntityManagerSetupImpl

use of org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl 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 4 with EntityManagerSetupImpl

use of org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl 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 5 with EntityManagerSetupImpl

use of org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl 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)

Aggregations

EntityManagerSetupImpl (org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl)11 HashMap (java.util.HashMap)8 Map (java.util.Map)5 EntityManagerFactoryImpl (org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl)4 SEPersistenceUnitInfo (org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo)4 ClassTransformer (jakarta.persistence.spi.ClassTransformer)3 SessionManager (org.eclipse.persistence.sessions.factories.SessionManager)3 EntityManager (jakarta.persistence.EntityManager)2 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)2 PersistenceUnitLoadingException (org.eclipse.persistence.exceptions.PersistenceUnitLoadingException)2 EntityManagerFactoryDelegate (org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate)2 JPAInitializer (org.eclipse.persistence.internal.jpa.deployment.JPAInitializer)2 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)2 JpaEntityManagerFactory (org.eclipse.persistence.jpa.JpaEntityManagerFactory)2 Address (org.eclipse.persistence.testing.models.jpa.extensibility.Address)2 PersistenceException (jakarta.persistence.PersistenceException)1 PersistenceProvider (jakarta.persistence.spi.PersistenceProvider)1 PersistenceUnitInfo (jakarta.persistence.spi.PersistenceUnitInfo)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1