From 22ecd671276400d30a9aac45eb98b5c8d73e0e77 Mon Sep 17 00:00:00 2001
From: Lawrence Rust <lvr@softsystem.co.uk>
Date: Wed, 11 Jan 2012 15:31:04 +0100
Subject: [PATCH] libmythdb: Only pool mysql connections created my the UI thread.
This works around the 5 second delay on exit caused by Qt checking which
thread opened a mysql connection but did not close one.
The pool is freed by the cleanup guard which executes on the main thread.
Therefore any pooled mysql connection made by a different thread will
trigger the problem. For instance, ImageLoadThread calls
MythUIImage::LoadImage which in turn needs the theme directory name from
the settings database. Multiple images are loaded during statup, each
creating a parallel mysql connection, which are then pooled when the
thread exits. At exit, Qt complains after all mysql connections are
closed that the ImageLoadThreads didn't close their mysql connection.
Signed-off-by: Lawrence Rust <lvr@softsystem.co.uk>
---
mythtv/libs/libmythdb/mythdbcon.cpp | 20 +++++++++++++++++---
mythtv/libs/libmythdb/mythdbcon.h | 2 ++
2 files changed, 19 insertions(+), 3 deletions(-)
diff --git a/mythtv/libs/libmythdb/mythdbcon.cpp b/mythtv/libs/libmythdb/mythdbcon.cpp
index 7f441a2..7cb97f4 100644
a
|
b
|
|
8 | 8 | #include <QSqlError> |
9 | 9 | #include <QSqlField> |
10 | 10 | #include <QSqlRecord> |
| 11 | #include <QThread> |
| 12 | #include <QApplication> |
11 | 13 | |
12 | 14 | // MythTV |
13 | 15 | #include "compat.h" |
… |
… |
|
17 | 19 | |
18 | 20 | static const uint kPurgeTimeout = 60 * 60; |
19 | 21 | |
20 | | MSqlDatabase::MSqlDatabase(const QString &name) |
| 22 | MSqlDatabase::MSqlDatabase(const QString &name) : m_thread(0) |
21 | 23 | { |
22 | 24 | m_name = name; |
23 | 25 | m_db = QSqlDatabase::addDatabase("QMYSQL3", name); |
… |
… |
bool MSqlDatabase::OpenDatabase() |
118 | 120 | VERBOSE(VB_GENERAL, |
119 | 121 | QString("Connected to database '%1' at host: %2") |
120 | 122 | .arg(m_db.databaseName()).arg(m_db.hostName())); |
| 123 | m_thread = QThread::currentThread(); |
121 | 124 | |
122 | 125 | // WriteDelayed depends on SetHaveDBConnection() and SetHaveSchema() |
123 | 126 | // both being called with true, so order is important here. |
… |
… |
void MDBManager::pushConnection(MSqlDatabase *db) |
274 | 277 | |
275 | 278 | if (db) |
276 | 279 | { |
277 | | db->m_lastDBKick = QDateTime::currentDateTime(); |
278 | | m_pool.prepend(db); |
| 280 | // Only re-use the connection if opened by the main thread. |
| 281 | // Otherwise when the app exits and the connections are closed by main, |
| 282 | // Qt complains that some threads have not exited and hangs for 5 seconds |
| 283 | if (!db->m_thread || db->m_thread == qApp->thread()) |
| 284 | { |
| 285 | db->m_lastDBKick = QDateTime::currentDateTime(); |
| 286 | m_pool.prepend(db); |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | --m_connCount; |
| 291 | delete db; |
| 292 | } |
279 | 293 | } |
280 | 294 | |
281 | 295 | m_lock.unlock(); |
diff --git a/mythtv/libs/libmythdb/mythdbcon.h b/mythtv/libs/libmythdb/mythdbcon.h
index 8bc921d..c260812 100644
a
|
b
|
|
12 | 12 | #include "mythexp.h" |
13 | 13 | |
14 | 14 | class QSemaphore; |
| 15 | class QThread; |
15 | 16 | |
16 | 17 | /// \brief QSqlDatabase wrapper, used by MSqlQuery. Do not use directly. |
17 | 18 | class MPUBLIC MSqlDatabase |
… |
… |
class MPUBLIC MSqlDatabase |
34 | 35 | QString m_name; |
35 | 36 | QSqlDatabase m_db; |
36 | 37 | QDateTime m_lastDBKick; |
| 38 | QThread* m_thread; |
37 | 39 | }; |
38 | 40 | |
39 | 41 | /// \brief DB connection pool, used by MSqlQuery. Do not use directly. |