Search in sources :

Example 1 with MetadataSource

use of org.eclipse.persistence.internal.jpa.config.persistenceunit.MetadataSource 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

HashMap (java.util.HashMap)1 PersistenceUnitLoadingException (org.eclipse.persistence.exceptions.PersistenceUnitLoadingException)1 EntityManagerFactoryImpl (org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl)1 EntityManagerSetupImpl (org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl)1 MetadataSource (org.eclipse.persistence.internal.jpa.config.persistenceunit.MetadataSource)1 JPAInitializer (org.eclipse.persistence.internal.jpa.deployment.JPAInitializer)1 SEPersistenceUnitInfo (org.eclipse.persistence.internal.jpa.deployment.SEPersistenceUnitInfo)1 PersistenceProvider (org.eclipse.persistence.jpa.PersistenceProvider)1 SessionManager (org.eclipse.persistence.sessions.factories.SessionManager)1