SQLite:在 WAL 模式下持久化文件句柄
SQLite: Persisting file handles in WAL mode
我有一个C++程序,其中有多个线程写入单个数据库的SQLite表(启用了WAL模式)。每个线程创建一个 SQLite 句柄,执行 sqlite3_open(),写入表(事务中有写入),然后执行 sqlite3_close(),然后删除 SQLite 句柄。然后线程死亡。
即使在所有线程死亡后,SQLite 句柄仍然处于打开状态。为什么 SQLite 句柄没有关闭?我在这里错过了什么?
我的C++程序运行在 CentOS 5.5 上。
[编辑]这是我使用的示例程序pthread
void threadFunction(void* pArg) {
sqlite3 *handle;
sqlite3_open("a.cm", &handle);
printf("Worker thread - Opened n");
sleep(10);
int r = sqlite3_close(handle);
printf("Ret: %dn", r);
printf("Worker thread - Closed n");
}
int main() {
int i(0);
pthread_t thread1, thread2;
printf("Creating a worker threadn");
printf("SQLite libversion: %sn", sqlite3_libversion());
sqlite3 *handle;
sqlite3_open("a.cm", &handle);
sqlite3_exec(handle, "pragma journal_mode = WAL", NULL, NULL, NULL);
printf("Main thread - Opened n");
pthread_create(&thread1, NULL, threadFunction, NULL);
pthread_create(&thread2, NULL, threadFunction, NULL);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
sleep(200);
sqlite3_close(handle);
printf("Main thread - close n");
return 0;
}
联系了SQLite团队:这是他们的回复-
关闭一个连接(在WAL
模式下)时,SQLite 检查进程中是否有任何其他连接保持正在关闭的数据库文件上的 POSIX 锁。如果是这样,它会推迟关闭文件句柄,直到另一个连接丢弃其POSIX 锁(如果还有另一个,文件描述符将被重用打开与同一文件的连接,但除此之外,它只是徘徊,直到可以安全关闭)。
结论是:所以直到从main()函数关闭连接从其他线程打开的句柄将不会关闭!
相关文章:
- 确定文件句柄是否为管道句柄
- 如何从文件句柄获取包含目录的句柄
- 将C文件句柄分配给C 文件流
- 调用posix_spawn时关闭所有文件句柄
- 保持潜在的文件句柄
- 在先前关闭的文件句柄上的 fclose() 上出现双重释放错误
- 正在从内存中获取文件句柄
- HANDLE(IntPtr)已过时,kernel32.dll,安全文件句柄到IntPtr
- JNI 问题:DLL 中的文件句柄出现符号查找错误C++
- 有没有办法使用一个文件句柄将手表放在千个目录上
- 将文件句柄重定向到字符缓冲区
- SQLite:在 WAL 模式下持久化文件句柄
- Windows -- 继承子进程中的控制台文件句柄
- 如何获取进程的文件句柄
- 关闭提前退出的程序的文件句柄时的Linux 3.14策略
- c++ boost asio Windows文件句柄async_read_until无限循环-没有eof.< /
- 查找当前打开的文件句柄数(不是lsof)
- 使用FindFile和文件句柄的c++内存泄漏
- 获取现有的文件句柄
- 如何根据要写入的数据类型动态选择文件句柄