Java Wildfly 和自动重新连接到数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28707650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wildfly and auto reconnect to the database
提问by Robert
I've got a client, a server and a database. The client communicates with the server via a EJB remote interfaces. As the server - I use a Wildfly 8.2.0. As the database - I use a MySQL. The server communicates with the MySQL via a JPA/Hibernate. When I turn off the MySQL server - the Wildfly throws an exception, of course. But when I turn on the MySQL again - the Wildfly still throws the same ERROR. I've to turn off the Wildfly and turn it back that the Wildfly reconnect to the database.
我有一个客户端、一个服务器和一个数据库。客户端通过 EJB 远程接口与服务器通信。作为服务器 - 我使用 Wildfly 8.2.0。作为数据库 - 我使用 MySQL。服务器通过 JPA/Hibernate 与 MySQL 通信。当我关闭 MySQL 服务器时 - 当然,Wildfly 会抛出异常。但是当我再次打开 MySQL 时 - Wildfly 仍然抛出相同的ERROR。我必须关闭 Wildfly 并将其恢复为 Wildfly 重新连接到数据库。
How to set auto reconnect in the Wildfly?
如何在 Wildfly 中设置自动重新连接?
I tried to set auto reconnect in a connection URL: jdbc:mysql://localhost/db?autoReconnect=true&useUnicode=yes&characterEncoding=UTF8
and i tried to add to the standalone-full.xml file which i use, this line: <check-valid-connection-sql>select 1</check-valid-connection-sql>
, but both solutions don't work.
我尝试在连接 URL: 中设置自动重新连接,jdbc:mysql://localhost/db?autoReconnect=true&useUnicode=yes&characterEncoding=UTF8
并尝试将这一行添加到我使用的 standalone-full.xml 文件中<check-valid-connection-sql>select 1</check-valid-connection-sql>
,但这两种解决方案都不起作用。
standalone-full.xml:
独立full.xml:
<!-- ... -->
<datasource jta="true" jndi-name="java:jboss/datasources/MySQLDS" pool-name="MySQLDS" enabled="true" use-ccm="true">
<connection-url>jdbc:mysql://localhost/db?autoReconnect=true&amp;useUnicode=yes&amp;characterEncoding=UTF8</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver>mysqlDriver</driver>
<security>
<user-name>user</user-name>
<password>***</password>
</security>
<validation>
<check-valid-connection-sql>select 1</check-valid-connection-sql>
<validate-on-match>false</validate-on-match>
<background-validation>false</background-validation>
</validation>
<timeout>
<set-tx-query-timeout>false</set-tx-query-timeout>
<blocking-timeout-millis>0</blocking-timeout-millis>
<idle-timeout-minutes>0</idle-timeout-minutes>
<query-timeout>0</query-timeout>
<use-try-lock>0</use-try-lock>
<allocation-retry>0</allocation-retry>
<allocation-retry-wait-millis>0</allocation-retry-wait-millis>
</timeout>
<statement>
<share-prepared-statements>false</share-prepared-statements>
</statement>
</datasource>
<drivers>
<driver name="mysqlDriver" module="com.mysql">
<xa-datasource-class>com.mysql.jdbc.Driver</xa-datasource-class>
</driver>
</drivers>
<!-- ... -->
采纳答案by Robert
I solved this problem by changing the validate-on-match value to true in my standalone-full.xml file:
我通过在我的 standalone-full.xml 文件中将 validate-on-match 值更改为 true 解决了这个问题:
<validate-on-match>true</validate-on-match>
<validate-on-match>true</validate-on-match>
回答by Domenico Briganti
This working on Wildfly 8.1:
这适用于 Wildfly 8.1:
<datasource jta="true" jndi-name="java:jboss/datasources/xxxdb" pool-name="xxxxDB" enabled="true" use-ccm="false">
<connection-url>jdbc:mysql://localhost:3306/xxxdb?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver>mysql-connector-java-5.1.26-bin.jar</driver>
<security>
<user-name>xxxuser</user-name>
<password>xxxpassword</password>
</security>
<validation>
<check-valid-connection-sql>select 1</check-valid-connection-sql>
<validate-on-match>false</validate-on-match>
<background-validation>true</background-validation>
<background-validation-millis>10000</background-validation-millis>
</validation>
<statement>
<share-prepared-statements>false</share-prepared-statements>
</statement>
</datasource>
回答by Alireza Alallah
You can set <background-validation>
to true
, because this option has better performance , and set <validate-on-match>
to false
您可以设置<background-validation>
为true
,因为此选项具有更好的性能,并设置<validate-on-match>
为false