本文介绍了Sqlite3, SQLSTATE[HY000]: 一般错误:5 数据库被锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个小测试脚本:

session_start();
session_write_close();
error_reporting(-1);
register_shutdown_function(function() {
    //echo 'shutdown';
});

$MAX = 120;
set_time_limit($MAX);
echo date('Y-m-d H:i:s').'<br>';
$m = microtime(true);
$file_db = new PDO('sqlite:'.dirname(__FILE__).'/test.sqlite3');
$file_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$file_db->exec("CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, message TEXT, time INTEGER)");

$d = date('U');
do
{
    $file_db->exec ('INSERT INTO messages VALUES (null, "titleee'.rand(1,9).'", "MESSAGEEEE'.rand(1,99).'", "'.rand(1,999).'")');
    if (date('U') - $d > $MAX/2)
    {
        break;
    }
} while (true);
$file_db = null;
echo 'ok: '.(microtime(true)-$m);

如果这在浏览器中的多个实例中运行,它迟早会丢弃SQLSTATE[HY000]: General error: 5 database is locked"异常.怎么躲?

if this is run in browser in multiple instance, sooner or later it drops "SQLSTATE[HY000]: General error: 5 database is locked" exception. How to dodge it?

推荐答案

Add: sleep(2) after $file_db->e​​xec Too many processes are试图插入到锁定表的数据库中的速度过快.欢迎您在实例化 $file_db 后立即尝试:$file_db->query("SET LOCK MODE TO WAIT 120").这应该使脚本等待最多两分钟才能解锁表...

Add: sleep(2) after $file_db->exec Too many processes are trying to insert too rapidly into the database which is locking the table. You are welcome to try: $file_db->query("SET LOCK MODE TO WAIT 120") immediately after you instantiate $file_db. That should make the script wait up to two minutes for the table to unlock...

这篇关于Sqlite3, SQLSTATE[HY000]: 一般错误:5 数据库被锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 14:49
查看更多