我正在使用Mybatis在后端Web服务器上查询MySQL数据库。
Web服务器仅在有限(但不是固定)的时间段内请求,因此几乎每天我都会收到如下警告日志。
pooled.PooledDataSource: Execution of ping query 'SELECT 1' failed: The last packet successfully received from the server was 51,861,027 milliseconds ago. The last packet sent successfully to the server was 51,861,027 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(Why does autoReconnect=true not seem to work?)看起来很接近我的问题,但这不是我的预期。
我想知道,当它处于空闲状态一段时间(可能是1小时)时,通过ping它可以建立连接永不超时。
由于Mybatis正在使用连接池,因此很难强制ping指定的空闲连接并使其永不超时。
我已经在Google上进行了一些搜索,但看起来并不容易入侵Mybatis。
我的问题是:
或者
答案 0 :(得分:1)
使用C3P0
之类的连接池管理器。您将能够配置永久连接。它的工作方式与您所描述的一样 - “ping”与SELEC 1
等示例查询的连接,以便在连接空闲N秒(可配置)时保持活动状态。
这里有一些指导http://gbif.blogspot.com/2011/08/using-c3p0-with-mybatis.html或此处http://edwin.baculsoft.com/2012/09/connecting-mybatis-orm-to-c3p0-connection-pooling/。可以使用Google搜索C3P0的配置选项。
答案 1 :(得分:0)
定期执行“选择”只是为了保持数据库连接的活动。
答案 2 :(得分:0)
当连接到MySQL服务器时,默认的“ wait_timeout”为8小时(28800秒),这意味着连接空闲超过8小时,Mysql将自动断开连接。可以使用if (isset ($_POST['image'])) {
$cust_id = $_POST['id'];
$images = $_FILES['image']['name'];
$tmp_dir = $_FILES['image']['tmp_name'];
$imageSize = $_FILES['image']['size'];
if (!is_dir('img/logotipa/'.$cust_id)){
mkdir('img/logotipa/'.$cust_id, 0777, true);
}
$upload_dir = 'img/logo/'.$cust_id.'/';
$imgExt = strtolower(pathinfo($images,PATHINFO_EXTENSION));
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
$up_image = $images;
if (file_exists($upload_dir.$images)){
$counter = 0;
while (file_exists($upload_dir.$up_image)){
$up_image = $counter.'_'.$images;
$counter++;
}
move_uploaded_file($tmp_dir, $upload_dir.$images);
$stmt = $conn->prepare('INSERT INTO logos(customer_id, logo) VALUES (:ucid, :upic)');
$stmt->bindParam(':ucid', $cust_id);
$stmt->bindParam(':upic', $up_image);
if ($stmt->execute()){
echo '<script>window.location.href = "customer_logo.php";</script>';
}
}else{
move_uploaded_file($tmp_dir, $upload_dir.$images);
$stmt = $conn->prepare('INSERT INTO logos(customer_id, logo) VALUES (:ucid, :upic)');
$stmt->bindP
aram(':ucid', $cust_id);
$stmt->bindParam(':upic', $up_image);
if ($stmt->execute()){
echo '<script>window.location.href = "customer_logo.php";</script>';
}
}
但是Java连接不知道该连接是否已从数据库端关闭。
要解决此问题,您有多种选择:
set Interactive_timeout = 432000;
设置wait_timeout = 432000;