跳到正文
DEV Community· LORQAI·· 47 分鐘前AI 評分44

LORQAI Studio 3 的間歇性 SQLite 鎖定錯誤帶來哪些本地優先軟件開發教訓

What an Intermittent SQLite Lock Taught Us About Building Local-First Software

AI 導讀

LORQAI Studio 3 開發期間,團隊重現了讀取交易與寫入操作競爭造成的 SQLite 鎖定問題,並為可寫入的資料庫連線啟用 WAL,使讀取交易仍在進行時,寫入者也能提交修訂。團隊使用 SQLite Online Backup API 建立一致的備份快照,並驗證資料完整性及還原內容。受控測試顯示問題在測試條件下得到解決,但不能證明所有資料庫競爭都已消除。

正文

Building a local-first application for a single user sounds simple enough. No distributed infrastructure, no thousands of concurrent users, no complicated database servers.

Yet even an ordinary editorial workflow can reveal an unexpected concurrency problem.

While developing LORQAI Studio 3, a creative content management application built with Go, SQLite, React, and TypeScript, we encountered an intermittent error:

database is locked

It appeared when saving an article after generating a website preview.

That small error led us to reconsider not only database concurrency, but also how we create and verify backups.

Reproducing the Problem

The application generates previews from existing content while allowing users to continue editing.

We suspected that an active read transaction might overlap with a subsequent save operation.

Instead of relying on additional retries, we isolated the suspected mechanism in a disposable test database.

The test followed a simple sequence:

  1. Create an article revision.
  2. Open a read-only transaction and read the revision.
  3. Keep the read transaction open.
  4. Attempt to save an updated revision through a separate writable connection.

With rollback-journal mode, the writer reached the configured three-second busy timeout and returned database is locked.

Even though each connection pool was limited to one open connection, the separate reader and writer could still compete for access to the same database.

We had reproduced a plausible failure mechanism, although the precise sequence behind the original intermittent UI error remained unconfirmed.

Moving to WAL

The architectural adjustment was to enable SQLite's Write-Ahead Logging mode for writable database connections.

The relevant setting is:

PRAGMA journal_mode = WAL;

Unlike rollback-journal mode, WAL allows readers to retain their existing snapshots while a writer commits new changes.

The journal mode also persists across database connections and restarts.

Our configuration uses:

  • Go driver: mattn/go-sqlite3
  • Busy timeout: 3000 milliseconds
  • Synchronous mode: FULL
  • Foreign keys: enabled
  • Maximum open connections: one per connection pool

After the change, the writer successfully committed a new article revision while the existing read transaction continued to see the original title.

That was the behavior we needed.

However, WAL does not turn SQLite into a multi-writer database. Write transactions remain serialized, and long-running readers can interfere with checkpoint progress.

WAL addresses an important concurrency problem, but it does not eliminate every possible source of contention.

The Backup Question

Changing the journaling mode introduced another consideration: backup integrity.

When WAL is active, committed changes may still reside in the associated WAL file. Simply copying the main database file is therefore not a reliable general-purpose backup strategy.

Our implementation uses SQLite's Online Backup API to create a separate, consistent database snapshot.

The backup workflow then:

  1. Creates an isolated database copy.
  2. Converts the destination database to DELETE journal mode.
  3. Confirms that the journal mode has changed.
  4. Closes the database connections before cleaning up the destination's remaining sidecar files.
  5. Verifies the resulting backup.

The live application continues using WAL.

Backup verification includes SQLite's PRAGMA integrity_check, SHA-256 payload verification, schema consistency checks, and validation of restored content and media.

Creating a backup is one thing. Demonstrating that it can be restored successfully is another.

What the Tests Confirmed

The recorded development results included successful targeted concurrency, backup, recovery, and restart tests.

The subsequent end-to-end editorial workflow also completed successfully without requiring a save retry.

These were controlled tests using disposable data, not a long-term production benchmark.

The results demonstrate that the reproduced reader/writer conflict was addressed under the tested conditions. They do not establish that all possible database contention has been eliminated.

What We Learned

Local-first does not mean concurrency-free.

A single-user application can still have background jobs, preview generation, database writes, and backup operations competing for shared resources.

Our experience reinforced four practical engineering principles:

  • Reproduce a failure before attempting to fix it.
  • Understand the database's concurrency model.
  • Consider the consequences of configuration changes beyond the immediate problem.
  • Verify recovery and the actual user workflow, not just individual components.

The most valuable outcome was not enabling WAL itself.

It was recognizing how a seemingly small database configuration change could affect the reliability of the wider application.

Sometimes the smallest error messages lead to the most useful architectural lessons.

References


Based on documented LORQAI Studio 3 development experience and recorded regression tests. AI-generated drafting and editorial review were used in preparing this article.

來源:DEV Community · dev.to