java - 最后一个成功发送到服务器的数据包是在 79,547 毫秒之前。比服务器配置值 'wait_timeout' 长

标签 java mysql

我有一个大问题,我不知道如何解决:

我有一个数据库的单例实例如下:

public Connection getConnection() throws SQLException {
    if (db_con == null)
        db_con = createConnection();

    return db_con;
}

我有一个代码如下:

   shortTextScoringComponent.scoreComponent(    "RS",SelectDataBase.getBlogs(rightSarcastic));
    shortTextScoringComponent.scoreComponent( "RNS",SelectDataBase.getBlogs(rightNonSarcasm));
    shortTextScoringComponent.scoreComponent( "FNS",SelectDataBase.getBlogs(wrongNonSarcasm));
    shortTextScoringComponent.scoreComponent( "FS",SelectDataBase.getBlogs(wrongSarcasm));

正如你所看到的,我调用了数据库 4 次,值得注意的是每次调用之间都有很长的处理时间,所以在他成功执行第二行之后,我的意思是这一行:

SelectDataBase.getBlogs(rightNonSarcasm);

当谈到第三行时,我得到以下错误:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet    successfully received from the server was 79,547,948 milliseconds ago.  The last packet sent successfully to the server was 79,547,964 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.

   com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No     operations allowed after connection closed.

我搜索了很多,但有很多不同的答案让我感到困惑,你知道我的确切问题是什么吗?

最佳答案

首先,如异常所述,请添加

autoReconnect=true

在你的 connectionString 中也添加这个

tcpKeepAlive=true

其次,您可以保留一个轮询线程来持续检查连接 Activity

class PollingThread extends Thread
{
    private java.sql.Connection connection;

    PollingThread(int index, java.sql.Connection connection)
    {
        super();
        this.connection = connection;
    }

    public void run()
    {
        Statement stmt = null;
        while (!interrupted())
        {
            try
            {
                sleep(15 * 60 * 1000L);
                stmt = connection.createStatement();
                stmt.execute("do 1");
            }
            catch (InterruptedException e)
            {
                break;
            }
            catch (Exception sqle)
            {
                /* This thread should run even on DB error. If autoReconnect is true,
                 * the connection will reconnect when the DB comes back up. */
            }
            finally
            {
                if (stmt != null)
                {
                    try
                    {
                        stmt.close();
                    } catch (Exception e)
                    {}
                }
                stmt = null;
            }
        }

关于java - 最后一个成功发送到服务器的数据包是在 79,547 毫秒之前。比服务器配置值 'wait_timeout' 长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34477616/

相关文章:

java - Android Dex文件中的 'String count'是什么意思?

java - 如何在java中制作一个排序列表?

java - 使用 Scala 或 Java 进行 Base 64 编码

java - 多个对象到剪贴板

java - 如何打开 weblogic 的调试和跟踪日志记录?

mysql - UTF-8编码问题

php - 在android中从服务器接收图像

php - Concat逗号使用mysql分隔名字和姓氏

c# - 未创建 MySQL Entity Framework 表

mysql - 为什么在MySQL(邮件服务器)中创建电子邮件后无法访问?