Program: 66146
Project: 66146
Buggy source:
/** 
 * Called by Persistence class when an EntityManagerFactory is to be created.
 * @param emName The name of the persistence unit
 * @param map A Map of properties for use by thepersistence provider. These properties may be used to override the values of the corresponding elements in the persistence.xml file or specify values for properties not specified in the persistence.xml.
 * @param classLoader The classloader to search for persistenceunits on
 * @return EntityManagerFactory for the persistence unit,or null if the provider is not the right provider
 */
protected EntityManagerFactory createEntityManagerFactory(String emName,Map properties,ClassLoader classLoader){
  Map nonNullProperties=(properties == null) ? new HashMap() : properties;
  String name=emName;
  if (name == null) {
    name="String_Node_Str";
  }
  JPAInitializer initializer=initializationHelper.getInitializer(classLoader,nonNullProperties);
  EntityManagerSetupImpl emSetupImpl=null;
  boolean isNew=false;
  String uniqueName;
  String sessionName;
  try {
    SEPersistenceUnitInfo puInfo;
    puInfo=initializer.findPersistenceUnitInfo(name,nonNullProperties,initializationHelper);
    if (puInfo == null) {
      return null;
    }
    if (initializer.isPersistenceUnitUniquelyDefinedByName()) {
      uniqueName=name;
    }
 else {
      uniqueName=initializer.createUniquePersistenceUnitName(puInfo);
    }
    sessionName=EntityManagerSetupImpl.getOrBuildSessionName(nonNullProperties,puInfo,uniqueName);
synchronized (EntityManagerFactoryProvider.emSetupImpls) {
      emSetupImpl=EntityManagerFactoryProvider.getEntityManagerSetupImpl(sessionName);
      if (emSetupImpl == null) {
        if (EntityManagerFactoryProvider.initialEmSetupImpls != null) {
          emSetupImpl=EntityManagerFactoryProvider.initialEmSetupImpls.remove(uniqueName);
          if (emSetupImpl != null) {
            emSetupImpl.changeSessionName(sessionName);
            puInfo.setClassLoader(classLoader);
          }
        }
        if (emSetupImpl == null) {
          emSetupImpl=initializer.callPredeploy(puInfo,nonNullProperties,initializationHelper,uniqueName,sessionName);
        }
        emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(),nonNullProperties);
        EntityManagerFactoryProvider.addEntityManagerSetupImpl(sessionName,emSetupImpl);
        isNew=true;
      }
    }
  }
 catch (  Exception e) {
    throw PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(classLoader,e);
  }
  if (!isNew) {
    if (!uniqueName.equals(emSetupImpl.getPersistenceUnitUniqueName())) {
      throw PersistenceUnitLoadingException.sessionNameAlreadyInUse(sessionName,uniqueName,emSetupImpl.getPersistenceUnitUniqueName());
    }
    boolean undeployed=false;
synchronized (emSetupImpl) {
      if (emSetupImpl.isUndeployed()) {
        undeployed=true;
      }
      emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(),nonNullProperties);
    }
    if (undeployed) {
      return createEntityManagerFactory(emName,properties,classLoader);
    }
  }
  EntityManagerFactoryImpl factory=null;
  try {
    factory=new EntityManagerFactoryImpl(emSetupImpl,nonNullProperties);
    if (emSetupImpl.shouldGetSessionOnCreateFactory(nonNullProperties)) {
      factory.getServerSession();
    }
    return factory;
  }
 catch (  RuntimeException ex) {
    if (factory != null) {
      factory.close();
    }
 else {
      emSetupImpl.undeploy();
    }
    throw ex;
  }
}

Fixed version (ground truth):
/** 
 * Called by Persistence class when an EntityManagerFactory is to be created.
 * @param emName The name of the persistence unit
 * @param map A Map of properties for use by thepersistence provider. These properties may be used to override the values of the corresponding elements in the persistence.xml file or specify values for properties not specified in the persistence.xml.
 * @param classLoader The classloader to search for persistenceunits on
 * @return EntityManagerFactory for the persistence unit,or null if the provider is not the right provider
 */
protected EntityManagerFactory createEntityManagerFactory(String emName,Map properties,ClassLoader classLoader){
  Map nonNullProperties=(properties == null) ? new HashMap() : properties;
  String name=emName;
  if (name == null) {
    name="String_Node_Str";
  }
  JPAInitializer initializer=initializationHelper.getInitializer(classLoader,nonNullProperties);
  EntityManagerSetupImpl emSetupImpl=null;
  boolean isNew=false;
  String uniqueName;
  String sessionName;
  try {
    SEPersistenceUnitInfo puInfo;
    puInfo=initializer.findPersistenceUnitInfo(name,nonNullProperties,initializationHelper);
    if (puInfo == null) {
      return null;
    }
    if (initializer.isPersistenceUnitUniquelyDefinedByName()) {
      uniqueName=name;
    }
 else {
      uniqueName=initializer.createUniquePersistenceUnitName(puInfo);
    }
    sessionName=EntityManagerSetupImpl.getOrBuildSessionName(nonNullProperties,puInfo,uniqueName);
synchronized (EntityManagerFactoryProvider.emSetupImpls) {
      emSetupImpl=EntityManagerFactoryProvider.getEntityManagerSetupImpl(sessionName);
      if (emSetupImpl == null) {
        emSetupImpl=EntityManagerFactoryProvider.initialEmSetupImpls.remove(uniqueName);
        if (emSetupImpl != null) {
          emSetupImpl.changeSessionName(sessionName);
          puInfo.setClassLoader(classLoader);
        }
        if (emSetupImpl == null) {
          emSetupImpl=initializer.callPredeploy(puInfo,nonNullProperties,initializationHelper,uniqueName,sessionName);
        }
        emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(),nonNullProperties);
        EntityManagerFactoryProvider.addEntityManagerSetupImpl(sessionName,emSetupImpl);
        isNew=true;
      }
    }
  }
 catch (  Exception e) {
    throw PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(classLoader,e);
  }
  if (!isNew) {
    if (!uniqueName.equals(emSetupImpl.getPersistenceUnitUniqueName())) {
      throw PersistenceUnitLoadingException.sessionNameAlreadyInUse(sessionName,uniqueName,emSetupImpl.getPersistenceUnitUniqueName());
    }
    boolean undeployed=false;
synchronized (emSetupImpl) {
      if (emSetupImpl.isUndeployed()) {
        undeployed=true;
      }
      emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(),nonNullProperties);
    }
    if (undeployed) {
      return createEntityManagerFactory(emName,properties,classLoader);
    }
  }
  EntityManagerFactoryImpl factory=null;
  try {
    factory=new EntityManagerFactoryImpl(emSetupImpl,nonNullProperties);
    if (emSetupImpl.shouldGetSessionOnCreateFactory(nonNullProperties)) {
      factory.getServerSession();
    }
    return factory;
  }
 catch (  RuntimeException ex) {
    if (factory != null) {
      factory.close();
    }
 else {
      emSetupImpl.undeploy();
    }
    throw ex;
  }
}