createEntityManagerFactory is failing randomly
<p>Now and then it is observed that the createEntityManagerFactory is failing. The stack trace is little confusing for me. Can anybody throw some light on it?</p>

<pre><code>java.lang.StackOverflowError
 at java.io.File.list(File.java:1133)
 at java.io.File.listFiles(File.java:1297)
 at org.eclipse.persistence.internal.jpa.deployment.DirectoryArchive.init(DirectoryArchive.java:90)
 at org.eclipse.persistence.internal.jpa.deployment.DirectoryArchive.init(DirectoryArchive.java:96)
 at org.eclipse.persistence.internal.jpa.deployment.DirectoryArchive.init(DirectoryArchive.java:96)
 at org.eclipse.persistence.internal.jpa.deployment.DirectoryArchive.init(DirectoryArchive.java:96)
 at org.eclipse.persistence.internal.jpa.deployment.DirectoryArchive.init(DirectoryArchive.java:96)
 at org.eclipse.persistence.internal.jpa.deployment.DirectoryArchive.init(DirectoryArchive.java:96)
 at org.eclipse.persistence.internal.jpa.deployment.DirectoryArchive.&lt;init&gt;(DirectoryArchive.java:74)
 at org.eclipse.persistence.internal.jpa.deployment.DirectoryArchive.&lt;init&gt;(DirectoryArchive.java:54)
 at org.eclipse.persistence.internal.jpa.deployment.ArchiveFactoryImpl.createArchive(ArchiveFactoryImpl.java:89)
 at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.findPersistenceArchives(PersistenceUnitProcessor.java:302)
 at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.findPersistenceArchives(PersistenceUnitProcessor.java:276)
 at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.findPersistenceUnitInfoInArchives(JPAInitializer.java:150)
 at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.findPersistenceUnitInfo(JPAInitializer.java:135)
 at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:177)
 at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:129)
 at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:177)
 at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:129)
 at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:177)
 at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:129)
 at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:177)
 at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:129)
 at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:177)
 ....
 ...
 ..
  at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:129)
 at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:177)
 at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
</code></pre>

<p>On further tracing i see that this is happening when a new EntityManagerFactory is being created after the close of existing one is failing with the following exceptions:</p>

<p>code:</p>

<pre><code>synchronized (factoryLock) {
    if (null != emFactory) {
        if (true == emFactory.isOpen()) {
            try {
                emFactory.close();
            } catch (IllegalStateException e) {
                System.out.println(e.getMessage());
            }
            catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        emFactory = null;
    }
    Map&lt;String, String&gt; properties = new HashMap&lt;String, String&gt;();

    properties.put(PersistenceUnitProperties.JDBC_DRIVER, jdbcDriver);
    properties.put(PersistenceUnitProperties.SESSION_CUSTOMIZER,
            "com.ca.waae.dbaccess.custom.JPASessionCustomizer");
    properties.put(PersistenceUnitProperties.JDBC_URL, jdbcUrl);
            properties.put(PersistenceUnitProperties.JDBC_PROPERTY+"REQUEST_KERBEROS_SESSION", "true");
            properties.put(PersistenceUnitProperties.JDBC_PROPERTY+"SERVICE_PRINCIPAL_NAME", dbPrincipalName);

    emFactory = Persistence.createEntityManagerFactory(
            "JPA", properties);
}
</code></pre>

<p>Exception:</p>

<pre><code>java.util.ConcurrentModificationException
        at java.util.HashMap$HashIterator.nextNode(HashMap.java:1440)
        at java.util.HashMap$ValueIterator.next(HashMap.java:1469)
        at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.clearStatementCache(DatabaseAccessor.java:342)
        at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.disconnect(DatabaseAccessor.java:505)
        at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.disconnect(DatabaseSessionImpl.java:415)
        at org.eclipse.persistence.sessions.server.ServerSession.disconnect(ServerSession.java:504)
        at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.logout(DatabaseSessionImpl.java:931)
        at org.eclipse.persistence.sessions.server.ServerSession.logout(ServerSession.java:776)
        at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.removeSessionFromGlobalSessionManager(EntityManagerSetupImpl.java:511)
        at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.undeploy(EntityManagerSetupImpl.java:2850)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.close(EntityManagerFactoryDelegate.java:267)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.close(EntityManagerFactoryImpl.java:287)
        at com.ca.waae.dbaccess.dao.AEConnection.createJpaEntityFactory(AEConnection.java:144)
</code></pre>

<p>persistent.xml contents are like</p>

<pre><code>?xml version="1.0" encoding="UTF-8"?&gt;
&lt;persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.
com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
        &lt;persistence-unit name="JPA"&gt;
                &lt;class&gt;com.xx.yy.dbaccess.model.table1&lt;/class&gt;
                &lt;class&gt;com.xx.yy.dbaccess.model.table2&lt;/class&gt;
                &lt;class&gt;com.xx.yy.dbaccess.model.table3&lt;/class&gt;
                &lt;class&gt;com.xx.yy.dbaccess.model.table4&lt;/class&gt;
                &lt;class&gt;com.xx.yy.dbaccess.model.table5&lt;/class&gt;
&lt;properties&gt;
                &lt;property name="eclipselink.logging.level" value="ALL"/&gt;
                &lt;property name="eclipselink.jdbc.cache-statements" value="true"/&gt;
                &lt;property name="eclipselink.jpa.uppercase-column-names " value="false"/&gt;
                &lt;property name="eclipselink.logging.parameters" value="true"/&gt;
                &lt;property name="eclipselink.weaving" value="static"/&gt;
        &lt;/properties&gt;

        &lt;/persistence-unit&gt;
&lt;/persistence&gt;
</code></pre>