title: How to resolve the error "Could not unbind factory from JNDI: org.hibernate.engine.jndi.JndiException?
body: <p>I have a Java EE app running on a Wildfly 10 server where I see this error after I finish making the number of requests mentioned as the max pool size(in other words if I have 5 set as max pool size I get this error on the 6th try). </p>
  
  <p>Here is my datasource configuration: </p>
  
  <pre><code> &lt;datasource jta="true" jndi-name="java:/jdbc/ITS" pool-name="ITS" use-ccm="true"&gt;
                      &lt;connection-url&gt;jdbc:oracle:thin:@prodrac-s..(conn URL here)/connection-url&gt;
                      &lt;driver&gt;oracle&lt;/driver&gt;
                      &lt;pool&gt;
                          &lt;min-pool-size&gt;2&lt;/min-pool-size&gt;
                          &lt;max-pool-size&gt;5&lt;/max-pool-size&gt;
                          &lt;prefill&gt;true&lt;/prefill&gt;
                      &lt;/pool&gt;
                      &lt;security&gt;
                          &lt;security-domain&gt;encrypted-password-its&lt;/security-domain&gt;
                      &lt;/security&gt;
                      &lt;validation&gt;
                          &lt;valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker"/&gt;
                          &lt;stale-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleStaleConnectionChecker"/&gt;
                          &lt;exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleExceptionSorter"/&gt;
                      &lt;/validation&gt;
                      &lt;timeout&gt;
                          &lt;set-tx-query-timeout&gt;false&lt;/set-tx-query-timeout&gt;
                          &lt;blocking-timeout-millis&gt;0&lt;/blocking-timeout-millis&gt;
                          &lt;idle-timeout-minutes&gt;30&lt;/idle-timeout-minutes&gt;
                          &lt;query-timeout&gt;0&lt;/query-timeout&gt;
                          &lt;use-try-lock&gt;0&lt;/use-try-lock&gt;
                          &lt;allocation-retry&gt;0&lt;/allocation-retry&gt;
                          &lt;allocation-retry-wait-millis&gt;0&lt;/allocation-retry-wait-millis&gt;
                      &lt;/timeout&gt;
                      &lt;statement&gt;
                          &lt;share-prepared-statements&gt;false&lt;/share-prepared-statements&gt;
                      &lt;/statement&gt;
                  &lt;/datasource&gt;
  </code></pre>
  
  <p>In my datasource for the Oracle DB I am trying to connect to the max-pool-size is set as 5 and the min-pool-size is 2. I have tried increasing the number to even 20, but I get the error on the 21st attempt. </p>
  
  <p>Here is the full stacktrace: </p>
  
  <pre><code>2017-07-26 18:13:18,446 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-6) - WFLYJCA0018: Started Driver service with driver-name = OnCore-FHIR.war_oracle.jdbc.OracleDriver_12_1
  2017-07-26 18:13:19,027 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 64) - WFLYJPA0010: Starting Persistence Unit (phase 2 of 2) Service 'OnCore-FHIR.war#its'
  2017-07-26 18:13:19,497 INFO  [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 64) - HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
  2017-07-26 18:13:19,672 INFO  [org.hibernate.envers.boot.internal.EnversServiceImpl] (ServerService Thread Pool -- 64) - Envers integration enabled? : true
  2017-07-26 18:13:21,070 INFO  [org.hibernate.internal.SessionFactoryRegistry] (ServerService Thread Pool -- 64) - HHH000094: Bound factory to JNDI name: hib/oncore/sessions/ITS_FACTORY
  2017-07-26 18:13:22,370 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 61) - RESTEASY002225: Deploying javax.ws.rs.core.Application: class org.company.OnCoreInterface.rest.resources.FhirRestExampleApplicationActivator$Proxy$_$$_WeldClientProxy
  2017-07-26 18:13:22,417 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 61) - WFLYUT0021: Registered web context: /OnCore-FHIR
  2017-07-26 18:13:22,463 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 33) - WFLYSRV0010: Deployed "OnCore-FHIR.war" (runtime-name : "OnCore-FHIR.war")
  2017-07-26 18:13:22,682 INFO  [org.jboss.as] (Controller Boot Thread) - WFLYSRV0060: Http management interface listening on http://127.0.0.1:10993/management
  2017-07-26 18:13:22,682 INFO  [org.jboss.as] (Controller Boot Thread) - WFLYSRV0051: Admin console listening on http://127.0.0.1:10993
  2017-07-26 18:13:22,682 INFO  [org.jboss.as] (Controller Boot Thread) - WFLYSRV0025: WildFly Full 10.1.0.Final (WildFly Core 2.2.0.Final) started in 13623ms - Started 482 of 740 services (435 services are lazy, passive or on-demand)
  2017-07-26 18:15:34,813 INFO  [org.hibernate.hql.internal.QueryTranslatorFactoryInitiator] (default task-5) - HHH000397: Using ASTQueryTranslatorFactory
  2017-07-26 18:20:53,378 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-21) - SQL Error: 0, SQLState: null
  2017-07-26 18:20:53,378 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-21) - javax.resource.ResourceException: IJ000453: Unable to get managed connection for java:/jdbc/ITS
  2017-07-26 18:22:16,472 INFO  [org.jboss.as.server] (management-handler-thread - 4) - WFLYSRV0236: Suspending server with no timeout.
  2017-07-26 18:22:16,488 INFO  [org.jboss.as.server] (Management Triggered Shutdown) - WFLYSRV0241: Shutting down in response to management operation 'shutdown'
  2017-07-26 18:22:16,503 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-4) - WFLYJCA0019: Stopped Driver service with driver-name = jtds
  2017-07-26 18:22:16,503 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-1) - WFLYJCA0019: Stopped Driver service with driver-name = OnCore-FHIR.war_oracle.jdbc.OracleDriver_12_1
  2017-07-26 18:22:16,503 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-5) - WFLYJCA0019: Stopped Driver service with driver-name = oracleXA
  2017-07-26 18:22:16,503 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-2) - WFLYJCA0019: Stopped Driver service with driver-name = h2
  2017-07-26 18:22:16,503 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-8) - WFLYJCA0010: Unbound data source [java:/jdbc/XA_SECURITY_CORE]
  2017-07-26 18:22:16,503 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-2) - WFLYJCA0019: Stopped Driver service with driver-name = sqlserver
  2017-07-26 18:22:16,503 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-5) - WFLYJCA0010: Unbound data source [java:/jdbc/XA_ADMINISTRATION_CORE]
  2017-07-26 18:22:16,519 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-5) - WFLYJCA0010: Unbound data source [java:/jdbc/XA_ADMIN_SELECT]
  2017-07-26 18:22:16,519 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-2) - WFLYJCA0010: Unbound data source [java:/jdbc/XA_SECURITY_SELECT]
  2017-07-26 18:22:16,519 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 69) - WFLYUT0022: Unregistered web context: /OnCore-FHIR
  2017-07-26 18:22:16,534 INFO  [org.infinispan.eviction.impl.PassivationManagerImpl] (ServerService Thread Pool -- 74) - ISPN000029: Passivating all entries to disk
  2017-07-26 18:22:16,534 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-7) - WFLYJCA0019: Stopped Driver service with driver-name = sqlserverXA
  2017-07-26 18:22:16,534 INFO  [org.infinispan.eviction.impl.PassivationManagerImpl] (ServerService Thread Pool -- 74) - ISPN000030: Passivated 0 entries in 2 milliseconds
  2017-07-26 18:22:16,534 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-3) - WFLYUT0019: Host default-host stopping
  2017-07-26 18:22:16,535 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 74) - WFLYCLINF0003: Stopped default-host cache from web container
  2017-07-26 18:22:16,568 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 74) - WFLYJPA0011: Stopping Persistence Unit (phase 2 of 2) Service 'OnCore-FHIR.war#its'
  2017-07-26 18:22:16,583 WARN  [org.hibernate.internal.SessionFactoryRegistry] (ServerService Thread Pool -- 74) - HHH000374: Could not unbind factory from JNDI: org.hibernate.engine.jndi.JndiException: Error performing unbind [hib/oncore/sessions/ITS_FACTORY]
      at org.hibernate.engine.jndi.internal.JndiServiceImpl.unbind(JndiServiceImpl.java:213)
      at org.hibernate.internal.SessionFactoryRegistry.removeSessionFactory(SessionFactoryRegistry.java:124)
      at org.hibernate.internal.SessionFactoryImpl.close(SessionFactoryImpl.java:1082)
      at org.hibernate.jpa.internal.EntityManagerFactoryImpl.close(EntityManagerFactoryImpl.java:347)
      at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$2$1.run(PersistenceUnitServiceImpl.java:237)
      at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$2$1.run(PersistenceUnitServiceImpl.java:217)
      at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:667)
      at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$2.run(PersistenceUnitServiceImpl.java:262)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
      at org.jboss.threads.JBossThread.run(JBossThread.java:320)
  Caused by: javax.naming.NamingException: WFLYNAM0020: Could not resolve service service jboss.naming.context.java.hib.oncore.sessions.ITS_FACTORY
      at org.jboss.as.naming.WritableServiceBasedNamingStore.unbind(WritableServiceBasedNamingStore.java:136)
      at org.jboss.as.naming.NamingContext.unbind(NamingContext.java:318)
      at org.jboss.as.naming.InitialContext$DefaultInitialContext.unbind(InitialContext.java:280)
      at javax.naming.InitialContext.unbind(InitialContext.java:445)
      at javax.naming.InitialContext.unbind(InitialContext.java:445)
      at org.hibernate.engine.jndi.internal.JndiServiceImpl.unbind(JndiServiceImpl.java:210)
  </code></pre>
  
  <p>I am trying to explicitly close the connection like so:</p>
  
  <pre><code>} catch (NamingException ne) {
              System.out.println(ne.getMessage());
  
          } catch (SQLException ne) {
              System.out.println(ne.getMessage());
  
          } catch (Exception e) {
              System.out.println(e.getMessage());
          }
          finally{
              try {
                   HibernateSession.closeITSSession();
  
              } catch (Exception ex) {
                  log.error("Exception trying to close the Hibernate ITS session: "+ ex);
                  System.out.println(ex.getMessage());
              }
          }
  </code></pre>
  
  <p>Can someone please help here? </p>