簡體   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