Java 和 MySQL 连接池:避免超时



我一直在尝试使用 Tomcat 的本机连接池功能以避免我的 Java Web 项目中的连接超时,但似乎我仍然不走运。

mysql-connector-java-5.1.23-bin.jar放在WEB-INF/lib文件夹中,创建了一个这样的META-INF/context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Resource name="jdbc/TheDatabase" auth="Container"
            type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
            maxActive="100" maxIdle="30" maxWait="1000"
            poolPreparedStatements="true" maxOpenPreparedStatements="100"
            username="user" password="pass"
            url="jdbc:mysql://localhost:3306/my_database"/>
</Context>

这是我的工作:

public static init() {
    ...
    sqlDataSource = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/TheDatabase");
    ...
}
public static ArrayList<ResultSet> dbRead() {
    Connection conn = sqlDataSource.getConnection();
    Statement stmt = null;
    try {
        stmt = conn.createStatement();
        ResultSet res = stmt.executeQuery("SELECT * FROM the_table");
        ...
        res.close();
        return out;
    } catch (SQLException e) {
        // Logging stuff
        return null;
    } finally {
        if (stmt != null)
            try {if (!stmt.isClosed()) stmt.close();}
            catch (SQLException e) {/* Logging stuff */}
        if (conn != null)
            try {if (!conn.isClosed()) conn.close();}
            catch (SQLException e) {/* Logging stuff */}
    }
}

每当我在wait_timeout通过后dbRead调用函数时,我都会得到:

Communications link failure
The last packet successfully received from the server was 1.818.697 milliseconds
ago. The last packet sent successfully to the server was 0 milliseconds ago.

具有 SQL 状态08S01

我以为是因为一些不正确的关闭,但事实似乎并非如此。

尝试添加验证查询,现在就让我。我希望这将解决问题。
如果没有,则将本地主机更改为您机器的 IP(您也可以测试 127.0.0.1(

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Resource name="jdbc/TheDatabase" auth="Container"
            type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
            maxActive="100" maxIdle="30" maxWait="1000"
            poolPreparedStatements="true" maxOpenPreparedStatements="100"
            username="user" password="pass" validationQuery="select now()" 
            url="jdbc:mysql://192.168.1.1:3306/my_database"/>
</Context>

尝试使用此内容:

<Resource name="jdbc/myDataSource" auth="Container" type="javax.sql.DataSource"
            maxActive="100" maxIdle="30" maxWait="10000"
            username="user" password="pass" 
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost/my_database?autoReconnect=true"
            logAbandoned="true" removeAbandoned="true" 
            removeAbandonedTimeout="60" />

将自动重新连接设置为 true,默认值为 false。以获取更多信息或帮助

自动重新连接 驱动程序是否应尝试重新建立过时和/或失效的连接?如果启用,驱动程序将为在属于当前事务的过时或失效连接上发出的查询引发异常,但会在新事务中的连接上发出下一个查询之前尝试重新连接。不建议使用此功能,因为当应用程序无法正确处理 SQLExceptions 时,此功能会产生与会话状态和数据一致性相关的副作用,并且仅设计为在无法将应用程序配置为正确处理由死连接和过时连接导致的 SQLExceptions 时使用。或者,作为最后一个选项,调查将MySQL服务器变量"wait_timeout"设置为高值,而不是默认值8小时。 错误 1.1

自动重新连接池

使用适用于连接池的重新连接策略(默认为"false"( false 3.1.3

查看 MySQL 文档

似乎我需要添加到<Resource>定义中的只是validationQuery属性。没有autoReconnect,没有autoReconnectForPools,什么都没有。这只是有效的:

<Resource name="jdbc/TheDatabase" auth="Container"
        type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
        maxActive="100" maxIdle="30" maxWait="1000"
        poolPreparedStatements="true" maxOpenPreparedStatements="100"
        username="user" password="pass" validationQuery="SELECT 1"
        url="jdbc:mysql://localhost:3306/my_database"/>

该属性甚至不在MySQL文档中,因此我不知道它的存在。现在我怀疑其他一些属性也有任何影响......

最新更新