Search code examples
jdbcwildflydatasource

Prevent WildFly from unbinding a datasource too early


I've got two webapps in one WildFly instance.

Both are using their own data source - each defined in the standalone.xml. The definition looks exactly the same:

<datasource jndi-name="java:/jdbc/ds-a" pool-name="pool-a" enabled="true" use-java-context="true">
    ...
</datasource>

<datasource jndi-name="java:/jdbc/ds-b" pool-name="pool-b" enabled="true" use-java-context="true">
    ...
</datasource>

The first webapp A is using Hibernate with a persistence.xml:

<persistence-unit name="database">
    <jta-data-source>java:/jdbc/ds-a</jta-data-source>
    ...
</persistence-unit>

The second webapp B is using JNDI lookup:

DataSource ds = context.lookup("jdbc/ds-b")

Everything is working fine, except during shutdown when both webapps are doing some clean up tasks. While webapp A is still able to access its datasource during shutdown, the datasource of webapp B is already closed.

This is an example of the log:

[org.jboss.as.server] (Management Triggered Shutdown) WFLYSRV0241: Shutting down in response to management operation 'shutdown'
[org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-2) WFLYJCA0010: Unbound data source [java:/jdbc/ds-b]
[org.jboss.jca.core.connectionmanager.pool.strategy.OnePool] (ServerService Thread Pool -- 135) IJ000615: Destroying active connection in pool: pool-b (org.jboss.jca.adapters.jdbc.local.LocalManagedConnection@...)
[org.hibernate.util.JDBCExceptionReporter] (webappb/scheduler_dispatch-3) javax.resource.ResourceException: IJ000470: You are trying to use a connection factory that has been shut down: java:/jdbc/ds-b

...

[org.jboss.as.jpa] (ServerService Thread Pool -- 139) WFLYJPA0011: Stopping Persistence Unit (phase 1 of 2) Service 'webappa.war#database'
[org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-6) WFLYJCA0010: Unbound data source [java:/jdbc/ds-a]
[org.jboss.as.server.deployment] (MSC service thread 1-4) WFLYSRV0028: Stopped deployment webappa.war (runtime-name: webappa.war) in 822ms

... many "javax.resource.ResourceException: IJ000470: You are trying to use a connection factory that has been shut down: java:/jdbc/ds-b" ....

[org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-1) WFLYJCA0010: Unbound data source [java:jboss/datasources/ExampleDS]
[org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-7) WFLYJCA0019: Stopped Driver service with driver-name = h2
[org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 161) WFLYCLINF0003: Stopped http-remoting-connector cache from ejb container
[org.infinispan.manager.DefaultCacheManager] (ServerService Thread Pool -- 161) Stopping cache manager null on null
[org.wildfly.extension.undertow] (MSC service thread 1-6) WFLYUT0019: Host default-host stopping
[org.wildfly.extension.undertow] (MSC service thread 1-6) WFLYUT0008: Undertow HTTP listener default suspending
[org.wildfly.extension.undertow] (MSC service thread 1-6) WFLYUT0007: Undertow HTTP listener default stopped, was bound to 127.0.0.1:8080
[org.wildfly.extension.undertow] (MSC service thread 1-4) WFLYUT0008: Undertow HTTPS listener https suspending
[org.wildfly.extension.undertow] (MSC service thread 1-4) WFLYUT0007: Undertow HTTPS listener https stopped, was bound to 127.0.0.1:8443
[org.wildfly.extension.undertow] (MSC service thread 1-7) WFLYUT0004: Undertow 2.2.3.Final stopping
[org.jboss.as.server.deployment] (MSC service thread 1-2) WFLYSRV0028: Stopped deployment webappb.war (runtime-name: webappb.war) in 72112ms

As you can see, data source ds-b is unbound immediately, while WildFly waits until the persistence unit of A is closed before the data source ds-a is unbound.

How can I prevent WildFly from unbinding the data source until webapp-b is undeployed?


Solution

  • You should tell WildFly about this dependency and put a resource-ref in your web.xml of Webapp B:

    <web-app>
        ...
        <resource-ref>
            <description>My database reference</description>
            <res-ref-name>jdbc/my-db</res-ref-name>
        </resource-ref>
    </web-app>
    

    And add that resource-ref in your jboss-web.xml:

    <jboss-web>
        ...
        <resource-ref>
            <res-ref-name>jdbc/my-db</res-ref-name>
            <jndi-name>java:/jdbc/ds-b</jndi-name>
        </resource-ref>
    </jboss-web>
    

    Then you can either lookup the datasource with jdbc/my-db or jdbc/ds-b.