protected EntityManagerFactoryImpl createEntityManagerFactoryImpl(PersistenceUnitInfo puInfo, Map properties, boolean requiresConnection){
if (puInfo != null) {
boolean isNew = false;
String uniqueName = null; // the name the uniquely defines the pu
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;
}
// emSetupImpl has been already predeployed, predeploy will just increment factoryCount.
emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(), properties);
}
if (undeployed) {
// after the emSetupImpl has been obtained from emSetupImpls
// 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);
// This code has been added to allow validation to occur without actually calling createEntityManager
if (emSetupImpl.shouldGetSessionOnCreateFactory(properties)) {
factory.getDatabaseSession();
}
return factory;
} catch (RuntimeException ex) {
if (factory != null) {
factory.close();
} else {
emSetupImpl.undeploy();
}
throw ex;
}
}