gpt4 book ai didi

multithreading - SQLITE3 CLSQL 多线程插入导致错误

转载 作者:行者123 更新时间:2023-12-03 12:52:06 24 4
gpt4 key购买 nike

我想将我的 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 提供“统一”接口(interface),就像其他“标准化”库(例如 JDBC 或 ODBC)一样,但 SQLite 是一个“非典型”数据库管理系统:实际上它是一个库,它提供 SQL 作为语言来访问简单的“文件中的数据库”,以及 DBMS 的一些其他功能。例如,它没有真正的并发控制(它使用操作系统函数来锁定 db 文件),因此不能将其视为“真正的”DBMS,而 cl-sql 只能提供底层系统的功能。

所以,如果你需要同时插入数据库,你应该使用别的东西,例如 PostgreSQL .

关于multithreading - SQLITE3 CLSQL 多线程插入导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31325248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com