Ticket #7836: PurgeIdleConnections_fix.patch

File PurgeIdleConnections_fix.patch, 3.6 KB (added by stichnot@…, 2 years ago)
  • libs/libmythdb/mythdbcon.h

     
    5656    QList<MSqlDatabase*> m_pool; 
    5757    QMutex m_lock; 
    5858    QSemaphore *m_sem; 
    59     int m_connID; 
     59    int m_nextConnID; 
     60    int m_connCount; 
    6061 
    6162    MSqlDatabase *m_schedCon; 
    6263    MSqlDatabase *m_DDCon; 
  • libs/libmythdb/mythdbcon.cpp

     
    185185 
    186186MDBManager::MDBManager() 
    187187{ 
    188     m_connID = 0; 
     188    m_nextConnID = 0; 
     189    m_connCount = 0; 
    189190 
    190191    m_sem = new QSemaphore(20); 
    191192 
     
    216217 
    217218    if (m_pool.isEmpty()) 
    218219    { 
    219         db = new MSqlDatabase("DBManager" + QString::number(m_connID++)); 
    220         VERBOSE(VB_IMPORTANT, QString("New DB connection, total: %1").arg(m_connID)); 
     220        db = new MSqlDatabase("DBManager" + QString::number(m_nextConnID++)); 
     221        ++m_connCount; 
     222        VERBOSE(VB_IMPORTANT, QString("New DB connection, total: %1").arg(m_connCount)); 
    221223    } 
    222224    else 
    223225        db = m_pool.takeLast(); 
     
    242244    m_lock.unlock(); 
    243245    m_sem->release(); 
    244246 
    245     PurgeIdleConnections(); 
     247    // Delay purging of idle connections as long as possible. 
     248    //PurgeIdleConnections(); 
    246249} 
    247250 
    248251void MDBManager::PurgeIdleConnections(void) 
     
    252255    QDateTime now = QDateTime::currentDateTime(); 
    253256    QList<MSqlDatabase*>::iterator it = m_pool.begin(); 
    254257 
     258    unsigned purgedConnections=0, totalConnections=0; 
     259    MSqlDatabase *newDb = NULL; 
    255260    while (it != m_pool.end()) 
    256261    { 
     262        totalConnections ++; 
    257263        if ((*it)->m_lastDBKick.secsTo(now) <= 3600) 
    258264        { 
    259265            ++it; 
     
    264270        // not been used in the past hour. 
    265271        MSqlDatabase *entry = *it; 
    266272        it = m_pool.erase(it); 
    267         --m_connID; 
     273        --m_connCount; 
     274        purgedConnections ++; 
     275        // Qt's MySQL driver apparently keeps track of the number of 
     276        // open DB connections, and when it hits 0, calls 
     277        // my_thread_global_end().  The mysql library then assumes the 
     278        // application is ending and that all threads that created DB 
     279        // connections have already exited.  This is rarely true, and 
     280        // may result in the mysql library pausing 5 seconds and 
     281        // printing a message like "Error in my_thread_global_end(): 1 
     282        // threads didn't exit".  This workaround simply creates an 
     283        // extra DB connection before all pooled connections are 
     284        // purged so that my_thread_global_end() won't be called. 
     285        if (it == m_pool.end() && 
     286            purgedConnections > 0 && 
     287            totalConnections == purgedConnections) 
     288        { 
     289            newDb = new MSqlDatabase("DBManager" + 
     290                                     QString::number(m_nextConnID++)); 
     291            ++m_connCount; 
     292            VERBOSE(VB_IMPORTANT, 
     293                    QString("New DB connection, total: %1").arg(m_connCount)); 
     294            newDb->m_lastDBKick = QDateTime::currentDateTime(); 
     295        } 
     296        VERBOSE(VB_IMPORTANT, "Deleting idle DB connection..."); 
    268297        delete entry; 
     298        VERBOSE(VB_IMPORTANT, "Done deleting idle DB connection."); 
    269299    } 
     300    if (newDb != NULL) 
     301    { 
     302        m_pool.prepend(newDb); 
     303    } 
     304    if (purgedConnections > 0) 
     305    { 
     306        VERBOSE(VB_IMPORTANT, 
     307                QString("Purged %1 idle of %2 total DB connections.") 
     308                .arg(purgedConnections).arg(totalConnections)); 
     309    } 
    270310} 
    271311 
    272312MSqlDatabase *MDBManager::getSchedCon()