当前位置 : 主页 > 网络安全 > 测试自动化 >

性能 – 在不同的线程(QT)上同时打开多个SQLite数据库实例

来源:互联网 收集:自由互联 发布时间:2021-06-22
从不同的线程同时使用多个开放连接有什么问题吗? 从我所看到的默认情况下它的线程安全,但是,这可能会伤害性能而不是改善它吗? documentation说: A connection can only be used from within
从不同的线程同时使用多个开放连接有什么问题吗?

从我所看到的默认情况下它的线程安全,但是,这可能会伤害性能而不是改善它吗?

documentation说:

A connection can only be used from within the thread that created it.
Moving connections between threads or creating queries from a
different thread is not supported.

In addition, the third party libraries used by the QSqlDrivers can
impose further restrictions on using the SQL Module in a multithreaded
program. Consult the manual of your database client for more
information.

这意味着您必须创建与数据库的连接,该数据库将与父线程链接.在QSqlDatabase类的docs,您可以看到描述:

The QSqlDatabase class represents a connection to a database.

The QSqlDatabase class provides an interface for accessing a database
through a connection. An instance of QSqlDatabase represents the
connection. The connection provides access to the database via one of
the supported database drivers, which are derived from QSqlDriver.

Create a connection (i.e., an instance of QSqlDatabase) by calling one
of the static addDatabase() functions, where you specify the driver or
type of driver to use (i.e., what kind of database will you access?)
and a connection name.

使用静态addDatabase()功能是创建连接的方法.

但由于Renzo said SQLite不支持多个写入事务同时进行.所以你需要一些机制(包装器)来使用低级互斥体或类似的东西来同步线程,比如任务队列.您可以在docs查看更多信息.

网友评论