How QSqlDatabase can support many thread to selected or update.
-
when a new socket comes, i create a thread ,every thread create a database object to execute selected or update ,but sometimes program can breakdown or no right result.
the example ,here
http://idas643.blog.163.com/blog/static/167104838201331635813904/
3Q
-
Hi,
From a quick overlook, you are creating a new database connection for every thread using the same name so basically reusing the same connection. Based on "this":http://qt-project.org/doc/qt-5/threads-modules.html#threads-and-the-sql-module it's not possible.
-
[quote author="SGaist" date="1396984407"]Hi,
From a quick overlook, you are creating a new database connection for every thread using the same name so basically reusing the same connection. Based on "this":http://qt-project.org/doc/qt-5/threads-modules.html#threads-and-the-sql-module it's not possible.[/quote]
can you tell me a method to support many thread to query database ,
the result is right. -
Maybe "this":https://gitorious.org/multi-threaded-access-to-a-database might help. But i haven't use it myself.
-
anything else??
-
Each thread needs to create its own, unique named database object, connect it to the database, and open it. The resulting connections are independent of each other.
You can use any scheme you like to get a unique name for the connection. You might use a counter, QUuid or the address of the worker object in hex to get a unique name.
-
I haven't tried this myself, but I believe you can create multiple connections to the same SQLite database.
Try this:
- Create a QSqlDatabase in each thread. Make sure they all have the same database name but different connection names
- When you create a QSqlQuery, make sure you associate it with the QSqlDatabase of that thread.