X秒不活动后,与DataBase的连接丢失

时间:2015-02-15 20:52:13

标签: java mysql

我在20-30分钟不活动后出现此错误

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 
The last packet successfully received from the server was 2.383.932 milliseconds ago.
The last packet sent successfully to the server was 21.351 milliseconds ago.

我已检查数据库选项,但它们没问题 MySQL的>显示会话变量,如'%timeout%';

+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| connect_timeout            | 10    |
| delayed_insert_timeout     | 300   |
| innodb_lock_wait_timeout   | 50    |
| innodb_rollback_on_timeout | OFF   |
| interactive_timeout        | 28800 |
| net_read_timeout           | 30    |
| net_write_timeout          | 60    |
| slave_net_timeout          | 3600  |
| table_lock_wait_timeout    | 50    |
| wait_timeout               | 28800 |
+----------------------------+-------+
10 rows in set (0.00 sec)

的MySQL>显示全局变量,例如'%timeoout%';

+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| connect_timeout            | 10    |
| delayed_insert_timeout     | 300   |
| innodb_lock_wait_timeout   | 50    |
| innodb_rollback_on_timeout | OFF   |
| interactive_timeout        | 28800 |
| net_read_timeout           | 30    |
| net_write_timeout          | 60    |
| slave_net_timeout          | 3600  |
| table_lock_wait_timeout    | 50    |
| wait_timeout               | 28800 |
+----------------------------+-------+
10 rows in set (0.00 sec)

连接类:

public class ConnectionDB {
    Connection con;
    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://XXXXXXXXXX:3306/";
    String nomeutente = "XXXXXXX";
    String password = "XXXXXXXX";
    String nomedatabase = "XXXXXX";
    public Boolean ok = false;
    public Connection connect(){            
            try {
                Class.forName(driver);
                con = DriverManager.getConnection(url+nomedatabase,nomeutente,password);
                ok = true;
                return con;
            } catch (ClassNotFoundException | SQLException   e) {
                new ErrorLog();
                e.printStackTrace();
                ok = false;
                return null;
            }       
    }
}

0 个答案:

没有答案