Is SQLite thread-safe?

  • Short answer: Yes
  • Medium length answer:
    1.Be sure to recompile with -DTHREADSAFE=1
2.Do not use the same database connection at the same time in more than one thread.

3.On some operating systems, a database connection should always be used in the same thread in which it was originally created.
4.There are a few features of SQLite that are not threadsafe. Avoid those features.

  • Longer answer:

By "threadsafe" we mean that you can use different SQLite database connections in different threads at the same time.

线程安全:并发情况下,在不同的线程中操作不同的数据库句柄是安全的。

It has never been safe to use the same database connection simultaneously in multiple threads.

If you use the sqlite3_prepare() API to create prepared statements, each prepared statement is considered to be a part of the database connection from which it was derived. So you cannot run two prepared statements originating from the same database connection in different threads at the same time.

Conclusion

  • Make sure you're compiling SQLite with -DTHREADSAFE=1.
  • Make sure that each thread opens the database file and keeps its own sqlite structure.
  • Make sure you handle the likely possibility that one or more threads collide when they access the db file at the same time: handle SQLITE_BUSY appropriately.
  • Make sure you enclose within transactions the commands that modify the database file, like INSERT, UPDATE, DELETE, and others.

转载地址:
http://www.sqlite.org/cvstrac/wiki?p=MultiThreading

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容