使用 PHP/PDO 避免锁定 SQLite 数据库超时

发布于 2024-11-09 09:08:41 字数 458 浏览 0 评论 0原文

我目前正在尝试修复 Drupal 核心问题队列中的问题,该问题与有人访问时发生的超时有关执行搜索索引时的站点,这是一种相当写入密集的操作,通常在调用 cron 命令时发生(因此它很可能发生在活动站点上,同时仍然可供公众访问)。它源于 SQLite 对写入的锁定,这通常不是问题,但在本例中是这样,因为执行此索引会大大增加连接在等待锁释放时超时的可能性。

答案似乎是增加驱动程序等待锁释放的时间。在 PHP 中,这应该可以通过将 PDO::ATTR_TIMEOUT 选项设置为等待锁定的秒数来实现。然而,这似乎并没有真正起作用;无论传递什么值,超时都会发生。不过,我在互联网上的其他地方找不到任何提及它不起作用的信息,那么只有我们这样吗?还有其他人在尝试增加 SQLite 的超时时间时遇到过麻烦吗?我们是否有其他方法可以避免这个问题的发生?

I'm currently trying to fix an issue in Drupal core's issue queue having to do with timeouts happening when someone visits a site while it is performing search indexing, a rather write-intensive operation that usually happens at the invocation of a cron command (so it's quite likely to happen on a live site while still accessible to the public). It stems from SQLite's locking on writes, which usually isn't an issue, but is in this case because doing this indexing is greatly increasing the likelihood that a connection will time out while waiting for the lock to release.

The answer seems to be to increase the amount of time that the driver waits for the lock to release. In PHP, this is supposed to be possible by setting the PDO::ATTR_TIMEOUT option to the number of seconds to wait for the lock. However, this doesn't seem to actually work; the timeouts happen no matter what value pass along this. I can't find any mention of it not working anywhere else on the internet, though, so is it just us? Has anyone else encountered trouble trying to increase SQLite's timeout time? Is there perhaps some other way we can avoid this issue happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猛虎独行 2024-11-16 09:08:41

只有一种可能的解决方案 -

$dbConnection->setAttribute(PDO::ATTR_TIMEOUT, (int)$time_in_seconds);

不要忘记,时间以秒为单位:-)

There is only one possible solution -

$dbConnection->setAttribute(PDO::ATTR_TIMEOUT, (int)$time_in_seconds);

Do not forget, that the time is in SECONDS :-)

一笑百媚生 2024-11-16 09:08:41

我通过

PRAGMA temp_store=MEMORY; PRAGMA journal_mode=MEMORY; 

在写作时使用事务来设法缓解这个问题。它不会完全解决这个问题,但它会让你的写入速度更快,特别是如果它是一堆同时完成的更新/插入。

I've managed to alleviate this by using

PRAGMA temp_store=MEMORY; PRAGMA journal_mode=MEMORY; 

and using transactions when writing. It won't resolve it fully but it will make your writes much much faster, especially if it's a bunch of updates/inserts that are all done at the same time.

懷念過去 2024-11-16 09:08:41

执行搜索索引时是否可以进入只读状态?
(只读比超时好......)

如果是这样,我建议这样做:

  • 在应用程序级别(例如drupal的核心代码),以友好的方式阻止所有写入查询
  • 复制数据库文件
  • 执行搜索索引复制的文件,主文件未锁定
  • 用复制的文件覆盖主文件(删除不需要的文件)
  • 启用写入查询并返回正常状态

Does it possible to enter read-only state while performing search indexing?
(read-only is better than timeouts...)

If so I would suggest doing it like that:

  • in the application level (e.g. drupal's core code), block all write queries in a friendly way
  • copy the database file
  • perform the search indexing on the copied file, the main file isn't locked
  • overwrite the main file with the copied file (deleting the un-needed file)
  • enable write queries and back to normal state
真心难拥有 2024-11-16 09:08:41

当一起使用以下两个 PRAGMA 命令时,PDO::ATTR_TIMEOUT 似乎被忽略。单独使用其中之一,超时就会按预期工作。 SQLite 版本:3.7.7.1

PRAGMA journal_mode=PERSIST
PRAGMA journal_mode=WAL

我不明白为什么你想要包含这两个,但我确实遇到了它们都被使用的情况,并且我看到 SQLite 数据库正忙消息,因为超时没有被执行受到尊重。

PDO::ATTR_TIMEOUT seems to be ignored when using the following two PRAGMA commands together. Use one or the other by itself and the timeout works as expected. SQLite Version: 3.7.7.1

PRAGMA journal_mode=PERSIST
PRAGMA journal_mode=WAL

I don't see why you would want to include both of these, but I did run into a situation where they were both used, and I was seeing the SQLite database is busy message because the timeout was not being respected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文