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

时间:2015-12-27 04:38:31

标签: java mysql

我遇到了一个大问题,我不知道如何修复它:

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

$url = $this->get_json_values( 'https://api.facebook.com/method/links.getStats?urls=http://google.com&format=json' );
$json = json_decode( $url, true );
$facebook_count = isset( $json[0]['share_count'] ) ? intval( $json[0]['share_count'] ) : 0;

我的代码如下:

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

    return db_con;
}

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

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

当涉及到第三行时,我收到以下错误:

SelectDataBase.getBlogs(rightNonSarcasm);

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

1 个答案:

答案 0 :(得分:1)

首先,例外情况是添加

  

autoReconnect的= TRUE

你的connectionString中的

也添加了这个

  

TCPKEEPALIVE =真

其次,您可以保留一个轮询线程以继续检查连接活动性

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;
            }
        }