Performance – Open multiple SQLite database instances on different threads (QT)

Is there any problem with using multiple open connections from different threads at the same time?

From what I have seen it is thread-safe by default, but, could this hurt performance rather than improve it?

The documentation says:

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.

This means that you must create a connection to the database, which will be linked to the parent thread. In the docs of the QSqlDatabase class, you can see the description:

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 (ie, an instance of QSqlDatabase) by calling one
of the static addDatabase() functions, where you specify the driver or
type of driver to use (ie, what kind of database will you access?)
and a connection name.

Using the static addDatabase() function is the way to create a connection.

But Since Renzo said SQLite does not support multiple write transactions at the same time. So you need some mechanism (wrapper) to use low-level mutexes or similar things to synchronize threads, such as task queues. You can see more information in the docs.

Is there any problem with using multiple open connections from different threads at the same time?

From what I have seen it is thread-safe by default, but, could this hurt performance rather than improve it?

The documentation says:

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.

This means you must create a connection to the database, The database will be linked to the parent thread. In the docs of the QSqlDatabase class, you can see the description:

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 (ie, an instance of QSqlDatabase) by calling one
of the static addDatabase() functions, where you specify the driver or
type of driver to use (ie, what kind of database will you access?)
and a connection name.

Using the static addDatabase() function is the way to create a connection.

But because Renzo said SQLite does not support multiple write transactions at the same time. So you need some mechanism (wrapper) to synchronize threads using low-level mutexes or similar things, such as task queues. You can see more information in the docs.

Leave a Comment

Your email address will not be published.