SQLITE3 CLSQL多线程插入结果报错

SQLITE3 CLSQL multithreaded insert results in error

我想通过多个线程并行使用我的 sqlite3 数据库。我读到使用连接池使访问线程安全,但我在插入数据时仍然出错。

(make-thread
   #'(lambda()
       (dotimes (i 100)
          (with-database (db ("/path/to/db") 
                         :database-type :sqlite3 :pool T)
            (do-stuff-with db)))))

以这种方式使用多个线程时出现此错误

While accessing database # with expression "INSERT INTO ...": Error 5 / database is locked

是否可以使用 sqlite3 数据库进行多线程插入?如果是怎么办?

SQLite不支持并发多写事务。来自 SQlite site:

SQLite supports an unlimited number of simultaneous readers, but it will only allow one writer at any instant in time. For many situations, this is not a problem. Writer queue up. Each application does its database work quickly and moves on, and no lock lasts for more than a few dozen milliseconds. But there are some applications that require more concurrency, and those applications may need to seek a different solution.

Cl-sql 已被写入为典型的客户端-服务器关系 DBMS 提供 "unified" 接口,就像其他 "standardized" 库(例如 JDBC 或 ODBC ),但 SQLite 是一个 "untypical" 数据库管理系统:实际上它是一个库,提供 SQL 作为访问简单 "database-in-a-file" 和其他一些功能的语言的 DBMS。例如,它没有真正的并发控制(它使用操作系统功能来锁定 db 文件),因此不能将其视为 "real" DBMS,而 cl-sql 只能提供底层系统的功能。

所以,如果您需要并发插入数据库,您应该使用其他东西,例如 PostgreSQL