Ticket #5482: purge_connections.patch

File purge_connections.patch, 1.8 KB (added by tomimo@…, 16 years ago)

Purge un-used db-connections after 3600s of idle time

  • libs/libmyth/mythdbcon.h

     
    6161
    6262    MSqlDatabase *m_schedCon;
    6363    MSqlDatabase *m_DDCon;
     64
     65    void purgeConnections(void);
    6466};
    6567
    6668/// \brief MSqlDatabase Info, used by MSqlQuery. Do not use directly.
  • libs/libmyth/mythdbcon.cpp

     
    203203
    204204    MSqlDatabase *db;
    205205
     206    // If there exists too many unused/old DB-connections, close them.
     207    purgeConnections();
     208
    206209    if (m_pool.isEmpty())
    207210    {
    208211        db = new MSqlDatabase("DBManager" + QString::number(m_connID++));
    209212        VERBOSE(VB_IMPORTANT, QString("New DB connection, total: %1").arg(m_connID));
    210213    }
    211214    else
     215    {
     216        // Use the list as FIFO in order to rotate the used connections.
    212217        db = m_pool.takeLast();
     218    }
    213219
    214220    m_lock.unlock();
    215221
     
    224230
    225231    if (db)
    226232    {
    227         m_pool.append(db);
     233        db->m_lastDBKick = QDateTime::currentDateTime();
     234        m_pool.prepend(db);
    228235    }
    229236
    230237    m_lock.unlock();
    231238    m_sem->release();
    232239}
    233240
     241void MDBManager::purgeConnections()
     242{
     243    //
     244    // Must be called with a valid MDBManager-lock!
     245    //
     246    int i;
     247    QDateTime now;
     248
     249    now=QDateTime::currentDateTime();
     250    for(i = 0; i < m_pool.size(); ++i)
     251    {
     252        if (m_pool[i]->m_lastDBKick.secsTo(now) > 3600)
     253        {
     254            // This connection has
     255            // not been used in the past hour.
     256            --m_connID;
     257            delete m_pool[i];
     258            continue;
     259        }
     260    }
     261    return;
     262}
     263
     264
    234265MSqlDatabase *MDBManager::getSchedCon()
    235266{
    236267    if (!m_schedCon)