繁体   English   中英

mybatis设置tomcat服务器上的mysql超时

[英]mybatis setting for mysql time-out on tomcat server

使用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.

我试图将“ autoReconnect = true”添加到连接URL的末尾,但这不能解决问题

Java应用程序处理可能重复的连接被丢弃

在这里报告BalusC的答案:

此异常表明您在应用程序启动期间仅打开一次连接,并在应用程序的生命周期中始终保持打开状态。 这不好。 DB迟早会取消连接,因为它已打开太长时间了。 您应该在打开尝试并对其执行查询的同一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块中关闭连接。 它将使它们可供重用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM