防止 WildFly 过早取消绑定数据源
Prevent WildFly from unbinding a datasource too early
我在一个 WildFly 实例中有两个 webapps。
两者都使用自己的数据源 - 每个都在 standalone.xml 中定义。定义看起来完全一样:
<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>
第一个 webapp A 正在使用带 persistence.xml:
的 Hibernate
<persistence-unit name="database">
<jta-data-source>java:/jdbc/ds-a</jta-data-source>
...
</persistence-unit>
第二个 webapp B 正在使用 JNDI 查找:
DataSource ds = context.lookup("jdbc/ds-b")
一切正常,除了在关闭期间两个 web 应用程序都在执行一些清理任务。虽然 webapp A 在关闭期间仍然能够访问其数据源,但 webapp B 的数据源已经关闭。
这是日志的一个例子:
[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
可以看到,数据源ds-b立即解绑,而WildFly等到A的持久化单元关闭在数据源 ds-a 未绑定之前。
如何防止 WildFly 在取消部署 webapp-b 之前取消绑定数据源?
你应该告诉 WildFly 这种依赖关系,并在你的 Webapp web.xml
中放置一个 resource-ref
B:
<web-app>
...
<resource-ref>
<description>My database reference</description>
<res-ref-name>jdbc/my-db</res-ref-name>
</resource-ref>
</web-app>
并在 jboss-web.xml
中添加 resource-ref
:
<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>
然后您可以使用 jdbc/my-db
或 jdbc/ds-b
查找数据源。
我在一个 WildFly 实例中有两个 webapps。
两者都使用自己的数据源 - 每个都在 standalone.xml 中定义。定义看起来完全一样:
<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>
第一个 webapp A 正在使用带 persistence.xml:
的 Hibernate<persistence-unit name="database">
<jta-data-source>java:/jdbc/ds-a</jta-data-source>
...
</persistence-unit>
第二个 webapp B 正在使用 JNDI 查找:
DataSource ds = context.lookup("jdbc/ds-b")
一切正常,除了在关闭期间两个 web 应用程序都在执行一些清理任务。虽然 webapp A 在关闭期间仍然能够访问其数据源,但 webapp B 的数据源已经关闭。
这是日志的一个例子:
[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
可以看到,数据源ds-b立即解绑,而WildFly等到A的持久化单元关闭在数据源 ds-a 未绑定之前。
如何防止 WildFly 在取消部署 webapp-b 之前取消绑定数据源?
你应该告诉 WildFly 这种依赖关系,并在你的 Webapp web.xml
中放置一个 resource-ref
B:
<web-app>
...
<resource-ref>
<description>My database reference</description>
<res-ref-name>jdbc/my-db</res-ref-name>
</resource-ref>
</web-app>
并在 jboss-web.xml
中添加 resource-ref
:
<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>
然后您可以使用 jdbc/my-db
或 jdbc/ds-b
查找数据源。