Wildfly 和自动重新连接到数据库

Wildfly and auto reconnect to the database

我有一个客户端、一个服务器和一个数据库。客户端通过 EJB 远程接口与服务器通信。作为服务器 - 我使用 Wildfly 8.2.0。作为数据库——我使用 MySQL。服务器通过 JPA/Hibernate 与 MySQL 通信。当我关闭 MySQL 服务器时,Wildfly 当然会抛出异常。但是当我再次打开 MySQL 时 - Wildfly 仍然抛出相同的 ERROR。我必须关闭 Wildfly 并将其转回 Wildfly 重新连接到数据库。

如何在Wildfly中设置自动重连?

我尝试在连接中设置自动重新连接 URL:jdbc:mysql://localhost/db?autoReconnect=true&amp;useUnicode=yes&amp;characterEncoding=UTF8 并且我尝试添加到我使用的独立 full.xml 文件中,这一行:<check-valid-connection-sql>select 1</check-valid-connection-sql>,但这两种解决方案都不起作用。

独立-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;amp;useUnicode=yes&amp;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>
<!-- ... -->

我通过在我的独立 full.xml 文件中将 validate-on-match 值更改为 true 解决了这个问题:

<validate-on-match>true</validate-on-match>

这适用于 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;amp;useUnicode=true&amp;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>

你可以把<background-validation>设置成true,因为这个选项性能更好,把<validate-on-match>设置成false