数据源中的MySQL连接有效性测试:SELECT 1还是更好的东西?

Jea*_*evy 11 mysql orm database-connection datasource jdbc

我正在MySQL上建立一个主/从体系结构的故障转移集群.我也正在配置我的JBoss数据源,我正在寻找更好的方法来测试我的连接,因为它知道它适用于Alfresco(使用Ibatis).

即使我多次尝试MySQL,我也不太清楚MySQL服务器内部的执行机制.

到目前为止,我正在使用此查询来测试我的SQL连接(就像在这个线程中:Jboss数据源中的数据库故障转移)

SELECT 1;
Run Code Online (Sandbox Code Playgroud)

这里是完整的数据源.

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
    <jndi-name>alfresco-datasource</jndi-name>
    <connection-url>
        jdbc:mysql://10.1.2.13,10.1.2.14:3306/alfresco
    </connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>alfresco</user-name>
    <password>alfresco</password>
    <exception-sorter-class-name>
        org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
    </exception-sorter-class-name>


    <connection-property name="readOnly">false</connection-property>
    <failOverReadOnly>false</failOverReadOnly>

    <!-- Automatic reconnecion - desactivated to preserve transactions  -->
    <!-- http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html :
    Failover happens when the driver determines that the connection has failed (checked before every query), and falls back to the first host when it determines that the host has become available again (after queriesBeforeRetryMaster queries have been issued). -->
    <!--<autoReconnect>true</autoReconnect>-->

    <check-valid-connection-sql>SELECT 1</check-valid-connection-sql>
    <valid-connection-checker-class-name>
        org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker
    </valid-connection-checker-class-name>


    <!-- If you're using Connector/J 3.1.8 or newer, you can use our implementation 
        of these to increase the robustness "mysql-ds.xml" 64L, 3683C of the connection 
        pool. -->
    <exception-sorter-class-name>
        com.mysql.jdbc.integration.jboss.ExtendedMysqlExceptionSorter
    </exception-sorter-class-name>
    <valid-connection-checker-class-name>
        com.mysql.jdbc.integration.jboss.MysqlValidConnectionChecker
    </valid-connection-checker-class-name>

    <metadata>
        <type-mapping>mySQL</type-mapping>
    </metadata>
</local-tx-datasource>

</datasources>
Run Code Online (Sandbox Code Playgroud)

我有多个问题,我无法自己回答:

  • 这个简单的查询不会被Ibatis(或任何类型或ORM)缓存吗?这意味着它可以归还我错误的结果.
  • 这个查询不会太简单吗?它是否真的试图执行代表服务器健康状况的内部机制?或者它只测试连接?
  • 这真的是一个可靠的测试吗?
  • 是否还有其他类型的测试(例如已与Connector/J集成)?
  • 性能对我来说也很重要,因此SELECT 1是健康检查和性能之间的良好折衷

不要犹豫,指出一些链接(在Stackoverflow内部或不是).如果这个问题已经回答了bean(似乎没有,就我搜索而言),我显然会删除这个帖子.

我非常感谢mysql开发人员或管理员的经验回报.我正在寻找最好的方法.

谢谢你的帮助.

Seb*_*bas 19

引用此链接:使用JDBC Ping MySQL服务器

你必须:

MySQL JDBC驱动程序(Connector/J)提供了ping机制.

如果您使用/*ping*/进行SQL查询,例如:

"/*ping*/SELECT 1"这实际上会导致驱动程序向服务器发送ping并返回假的,轻量级的结果集.

(您可以在Connector/J文档中找到相当深入的内容;在该页面上搜索"ping".请仔细阅读:此机制对所使用的语法非常敏感.与大多数SQL不同,"解析""ping" "标记发生在客户端JDBC驱动程序本身.).