Skip to content

fix: sqlite "database is locked" error#1009

Merged
looplj merged 2 commits intolooplj:release/v0.9.xfrom
myml:fix-sqlite
Mar 10, 2026
Merged

fix: sqlite "database is locked" error#1009
looplj merged 2 commits intolooplj:release/v0.9.xfrom
myml:fix-sqlite

Conversation

@myml
Copy link
Copy Markdown
Contributor

@myml myml commented Mar 10, 2026

在sqllite+hdd+trace很容易出现 database is locked 错误, 导致trace记录不完整

通过限制sqlite并发数避免这个问题

已实际测试有效 fix #907

在sqllite+hdd+trace很容易出现 database is locked 错误, 导致trace记录不完整

通过限制sqlite并发数避免这个问题
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此PR旨在解决SQLite数据库在特定操作环境下(如结合HDD和trace时)容易出现的“database is locked”错误,该错误会导致数据记录不完整。通过限制数据库的并发连接数,此项更改显著提升了数据库的稳定性和数据完整性,从而避免了此类问题的再次发生。

Highlights

  • SQLite并发控制: 限制SQLite数据库的最大并发连接数为1,以解决在特定环境下(如sqllite+hdd+trace)出现的“database is locked”错误,确保trace记录的完整性。
Changelog
  • internal/server/db/ent.go
    • 设置了SQLite数据库的最大开放连接数为1。
Activity
  • 暂无人工活动记录。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

该 PR 通过将 SQLite 的最大打开连接数限制为 1 来解决 “database is locked” 错误,这在高并发写入场景下很常见。该方法通过强制序列化数据库访问来避免锁竞争,是一个直接有效的解决方案。代码更改是正确的,我有一个小建议可以进一步提高代码的清晰度和健壮性。

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@looplj looplj merged commit f0e32b2 into looplj:release/v0.9.x Mar 10, 2026
2 checks passed
looplj added a commit that referenced this pull request Mar 13, 2026
looplj added a commit that referenced this pull request Mar 13, 2026
@myml
Copy link
Copy Markdown
Contributor Author

myml commented Mar 13, 2026

我看这个提交回退了,是有什么问题吗?我自己试着好像没问题呀 @looplj

@looplj
Copy link
Copy Markdown
Owner

looplj commented Mar 13, 2026

#1046

system 页面很慢,经常加载不出来,好像也没法备份了。

看起来是在页面使用很慢,可能只使用 API 没问题

@myml
Copy link
Copy Markdown
Contributor Author

myml commented Mar 13, 2026

我试试其他解决方法

@myml
Copy link
Copy Markdown
Contributor Author

myml commented Mar 13, 2026

原来默认就会启用sqlite3的WAL, 只是因为我手动设置了 AXONHUB_DB_DSN=file:/data/axonhub.db 没有加任何参数才导致有这个报错。

图片

@looplj
Copy link
Copy Markdown
Owner

looplj commented Mar 13, 2026

开了 wal 会好点吗。

@myml
Copy link
Copy Markdown
Contributor Author

myml commented Mar 13, 2026

结合AI提供的资料和自己测试,只开启wal还会报错,再给DSN加上busy_timeout参数能解决这个问题。

项目使用的sqlite库不支持journal_mode=WAL这种写法,所以默认选项其实没起效。正确的写法应该是_pragma=journal_mode(WAL)

AXONHUB_DB_DSN=file:/data/axonhub.db?_fk=1&_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)

@myml
Copy link
Copy Markdown
Contributor Author

myml commented Mar 13, 2026

图片

开启wal后,启动服务会生成一个 axonhub.db-wal 文件,这个文件的体积不应该这么大,具体原因不明,可能是驱动的问题。考虑这个文件异常再加上sqlite3非生产环境使用,感觉还是先不要改动现在的默认配置了,有需要或新建实例再酌情配置。

_pragma=journal_mode(WAL) 开启数据库读写分离,提高性能(该选项开启一次后无法再关闭)
_pragma=busy_timeout(5000) 等待锁,避免"database is locked"报错

两个选项可单独使用也可以一起使用

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug/错误]: database is locked

2 participants