【问题标题】:mybatis setting for mysql time-out on tomcat serverTomcat服务器上mysql超时的mybatis设置
【发布时间】:2015-03-20 17:39:28
【问题描述】:

使用java7 tomcat7和mybatis作为ORM

config.xml 是这样的

<transactionManager type="JDBC" />
    <dataSource type="POOLED">
        <property name="driver" value="com.mysql.jdbc.Driver" />
        <property name="url"    value="jdbc:mysql://localhost:3306/xxxxdb" />
        <property name="username" value="xxxxxxx" />
        <property name="password" value="xxxxxxx" />
        <property name="poolPingEnabled" value="true" />
        <property name="poolPingQuery" value="SELECT 1 " />
    </dataSource>
</environment>

mysql 设置都是默认设置。 因此 interactive_timeout 为 28800。
当我登录我的服务时,它第一次失败,然后第二次成功。
即使在 28800 秒内重新登录,有时也会发生上述错误。 我将错误消息粘贴到服务器中

2015 10:03:49 org.apache.ibatis.datasource.pooled.PooledDataSource warn
WARN: Execution of ping query 'SELECT 1' failed: Communications link failure

The last packet successfully received from the server was 30,572,290 milliseconds ago.  The     last packet sent successfully to the server was 1 milliseconds ago.
org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:     The last packet successfully received from the server was 36,001,604 milliseconds ago.  The     last packet sent successfully to the server was 36,001,633 milliseconds ago. is longer than     the server configured value of 'wait_timeout'. You should consider either expiring and/or     testing connection validity before use in your application, increasing the server configured     values for client timeouts, or using the Connector/J connection property 'autoReconnect=true'     to avoid this problem.
### The error may exist in sql.xml
### The error may involve com.xxx.isRegistered-Inline
### The error occurred while setting parameters
### SQL: [query for login];
### Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 36,001,604 milliseconds ago.  The last packet sent successfully to the server was 36,001,633 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.

我尝试在连接 url 末尾添加“autoReconnect=true”,但没有解决问题

【问题讨论】:

    标签: java mysql tomcat mybatis


    【解决方案1】:

    Java app handling for connections getting dropped 可能重复

    在这里举报BalusC的回答:

    此异常表明您在应用程序启动期间仅打开一次连接,并在应用程序的生命周期内保持永久打开状态。这是不好的。数据库迟早会收回连接,因为它已打开太久。当您打开它并在其上执行查询时,您应该在同一个 try 块的 finally 块中正确关闭连接。

    例如

    public Entity find(Long id) throws SQLException {
        Connection connection = null;
        // ...
    
        try {
            connection = database.getConnection();
            // ...
        } finally {
            // ...
            if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
        }
    
        return entity;
    }
    

    如果您对此有性能问题(这是非常合理的,因为连接是最昂贵的任务),那么您应该使用连接池。它还透明地处理这种“连接断开”问题。例如,BoneCP。请注意,在连接池的情况下,您仍应按照上述 JDBC 代码习语关闭 finally 块中的连接。这将使它们可以重复使用。

    【讨论】:

      猜你喜欢
      • 2011-06-26
      • 1970-01-01
      • 2023-04-03
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 2010-11-23
      相关资源
      最近更新 更多