[英]Prevent WildFly from unbinding a datasource too early
我在一個 WildFly 實例中有兩個 webapp。
兩者都使用自己的數據源 - 每個都在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正在使用具有持久性的 Hibernate。xml:
<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")
一切正常,除了在關閉期間兩個 webapps 都在做一些清理任務。 雖然 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才解除綁定。
在取消部署webapp-b之前,如何防止 WildFly 取消綁定數據源?
您應該告訴 WildFly 這種依賴關系,並在 Webapp B的web.xml
中放置一個resource-ref
:
<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
查找數據源。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.