spikey
spikey

Reputation: 444

Best strategy to reduce DB connections in a multithreading application using the Qt framework

I have a server that communicates to a lot of devices (>1000). Each connection has its own thread. Now, I realized that I would have to set my mysql config to allow >1000 open concurrent connections what seems to be a very bad idea in my opinion.

Qt docs say that every thread needs its own connection: http://qt-project.org/doc/qt-4.8/threads-modules.html#threads-and-the-sql-module

So, I have to call

 QSqlDatabase::addDatabase("QMYSQL", "thread specific string");

in every thread.

What is the best practice here?

Upvotes: 2

Views: 1802

Answers (2)

Mian Zeshan Farooqi
Mian Zeshan Farooqi

Reputation: 311

  1. Honestly speaking I don't know much about QT but if I take your problem in general then I would advise you to create a "Connection Pool"
  2. If you don't want or can't implement a Connection Pool then its fine to increase Max_Connections in MySQL configuration and leave the pooling on MySQL, it has its own Connection Pooling mechanism.

Upvotes: 0

r_ahlskog
r_ahlskog

Reputation: 1966

I would think some sort of resource pooling would be appropriate here.

Depending on the database workload from the >1000 device threads a single database thread could maybe manage it or then you will need several database threads.

Then setup a queuing system from the device threads to the database thread(s), where the devices push the work and the database thread(s) pulls work units off and perform the query.

I just realised that I was thinking of writing to database only like some sort of logging, and this idea may not work without modification if what you are doing is reading from the database and writing to devices.

Upvotes: 1

Related Questions