Ticket #10254: 0001-libmythdb-Only-pool-mysql-connections-created-my-the.patch

File 0001-libmythdb-Only-pool-mysql-connections-created-my-the.patch, 3.4 KB (added by Lawrence Rust <lvr@…>, 13 years ago)
  • mythtv/libs/libmythdb/mythdbcon.cpp

    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  
    88#include <QSqlError>
    99#include <QSqlField>
    1010#include <QSqlRecord>
     11#include <QThread>
     12#include <QApplication>
    1113
    1214// MythTV
    1315#include "compat.h"
     
    1719
    1820static const uint kPurgeTimeout = 60 * 60;
    1921
    20 MSqlDatabase::MSqlDatabase(const QString &name)
     22MSqlDatabase::MSqlDatabase(const QString &name) : m_thread(0)
    2123{
    2224    m_name = name;
    2325    m_db = QSqlDatabase::addDatabase("QMYSQL3", name);
    bool MSqlDatabase::OpenDatabase() 
    118120            VERBOSE(VB_GENERAL,
    119121                    QString("Connected to database '%1' at host: %2")
    120122                            .arg(m_db.databaseName()).arg(m_db.hostName()));
     123            m_thread = QThread::currentThread();
    121124
    122125            // WriteDelayed depends on SetHaveDBConnection() and SetHaveSchema()
    123126            // both being called with true, so order is important here.
    void MDBManager::pushConnection(MSqlDatabase *db) 
    274277
    275278    if (db)
    276279    {
    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        }
    279293    }
    280294
    281295    m_lock.unlock();
  • mythtv/libs/libmythdb/mythdbcon.h

    diff --git a/mythtv/libs/libmythdb/mythdbcon.h b/mythtv/libs/libmythdb/mythdbcon.h
    index 8bc921d..c260812 100644
    a b  
    1212#include "mythexp.h"
    1313
    1414class QSemaphore;
     15class QThread;
    1516
    1617/// \brief QSqlDatabase wrapper, used by MSqlQuery. Do not use directly.
    1718class MPUBLIC MSqlDatabase
    class MPUBLIC MSqlDatabase 
    3435    QString m_name;
    3536    QSqlDatabase m_db;
    3637    QDateTime m_lastDBKick;
     38    QThread* m_thread;
    3739};
    3840
    3941/// \brief DB connection pool, used by MSqlQuery. Do not use directly.