sqlite数据库连接/锁定问题
乡亲们 我正在使用 sqlite 实现基于文件的队列(请参阅我之前的问题)。我有以下线程在后台运行:
- thread-1 将内存结构清空到“队列”表中(插入到“队列”表中)。
- 线程 1 读取并“处理”“队列”表,每 5 到 10 秒运行一次
- 线程 3 - 运行频率非常低,并从“队列”表中清除不再需要的旧数据,并且还运行真空,因此大小数据库文件仍然很小。
现在我想要的行为是每个线程获得它需要的任何锁(如果可能的话超时等待),然后完成事务。如果线程不同时运行也没关系 - 重要的是事务一旦开始就不会由于“锁定”错误(例如“数据库被锁定”)而失败。
我查看了交易文档,但似乎没有“超时”设施(我我正在使用 JDBC)。连接中的超时时间可以设置得很大吗?
我能想到的一种解决方案(未尝试过)是拥有一个最多 1 个连接的连接池。因此,一次只有一个线程可以连接,因此我们不应该看到任何锁定错误。还有更好的方法吗?
谢谢!
Folks
I am implementing a file based queue (see my earlier question) using sqlite. I have the following threads running in background:
- thread-1 to empty out a memory structure into the "queue" table (an insert into "queue" table).
- thread-1 to read and "process" the "queue" table runs every 5 to 10 seconds
- thread-3 - runs very infrequently and purges old data that is no longer needed from the "queue" table and also runs vacuum so the size of the database file remains small.
Now the behavior that I would like is for each thread to get whatever lock it needs (waiting with a timeout if possible) and then completing the transaction. It is ok if threads do not run concurrently - what is important is that the transaction once begins does not fail due to "locking" errors such as "database is locked".
I looked at the transaction documentation but there does not seem to be a "timeout" facility (I am using JDBC). Can the timeout be set to a large quantity in the connection?
One solution (untried) I can think of is to have a connection pool of max 1 connection. Thus only one thread can connect at a time and so we should not see any locking errors. Are there better ways?
Thanx!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果是我,我会使用单个数据库连接句柄。如果一个线程需要它,它可以在关键部分(或互斥体或类似的)内分配它 - 这基本上是一个穷人的连接池,池中只有一个连接:)它可以与数据库一起处理事务。完成后,它退出关键部分(或释放互斥体或?)。如果您仔细使用单个数据库连接,则不会出现锁定错误。
-大学教师
If it were me, I'd use a single database connection handle. If a thread needs it, it can allocate it within a critical section (or mutex, or similar) - this is basically a poor man's connection pool with only one connection in the pool:) It can do its business with the databse. When done, it exits the critical section (or frees the mutex or ?). You won't get locking errors if you carefully use the single db connection.
-Don