2013-04-11 61 views
0

這似乎是一個常見問題,我搜索了很多,但沒有成功找到解決方案。試過mysql_auto_reconnect和connect_cached但沒有幫助。這基本上是我在這裏的代碼。 在閒置30秒後,mysql服務器將關閉連接。 mysql_auto_reconnect只有當我第一個$ dbh->從服務器斷開(在代碼中註釋)時才起作用。 connect_cached方法在這裏工作,但我仍然在while循環中使用connect_cached方法得到「lost connection」消息。perl + mysql在查詢期間丟失與MySQL服務器的連接

my $dbh = DBI->connect_cached($mysql_data_source, $mysql_username, $mysql_passwd, {'RaiseError' => 1}); 
    $dbh->{AutoInactiveDestroy} = 1; 
    $dbh->{mysql_auto_reconnect} = 1; 
    print strftime('%F %T', localtime()); 
    print "\n"; 
    #$dbh->disconnect(); 
    sleep 30; 
    $dbh = DBI->connect_cached($mysql_data_source, $mysql_username, $mysql_passwd, {'RaiseError' => 1}); 
    print strftime('%F %T', localtime()); 
    my $sql = "some sql"; 
    my $hashref = $dbh->selectall_hashref($sql, "id"); 
    my $info_hashref = $dbh->{mysql_dbd_stats}; 
    print Dumper($info_hashref); 
    print Dumper($hashref); 

你有什麼想法嗎?

回答

0

您可以嘗試操作超時,或許是增加了此信息連接:mysql_connect_timeout = 60

當然,如果查詢時間那麼長,它可能是最好查看SQL。

+1

mysql_auto_reconnect和$ dbh-> ping()的作品。 DBD :: Safe是一個更好的選擇。 – dezhi 2013-04-18 06:51:04

相關問題