Ticket #7836: PurgeIdleConnections_fix.patch
| File PurgeIdleConnections_fix.patch, 3.6 KB (added by stichnot@…, 2 years ago) |
|---|
-
libs/libmythdb/mythdbcon.h
56 56 QList<MSqlDatabase*> m_pool; 57 57 QMutex m_lock; 58 58 QSemaphore *m_sem; 59 int m_connID; 59 int m_nextConnID; 60 int m_connCount; 60 61 61 62 MSqlDatabase *m_schedCon; 62 63 MSqlDatabase *m_DDCon; -
libs/libmythdb/mythdbcon.cpp
185 185 186 186 MDBManager::MDBManager() 187 187 { 188 m_connID = 0; 188 m_nextConnID = 0; 189 m_connCount = 0; 189 190 190 191 m_sem = new QSemaphore(20); 191 192 … … 216 217 217 218 if (m_pool.isEmpty()) 218 219 { 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)); 221 223 } 222 224 else 223 225 db = m_pool.takeLast(); … … 242 244 m_lock.unlock(); 243 245 m_sem->release(); 244 246 245 PurgeIdleConnections(); 247 // Delay purging of idle connections as long as possible. 248 //PurgeIdleConnections(); 246 249 } 247 250 248 251 void MDBManager::PurgeIdleConnections(void) … … 252 255 QDateTime now = QDateTime::currentDateTime(); 253 256 QList<MSqlDatabase*>::iterator it = m_pool.begin(); 254 257 258 unsigned purgedConnections=0, totalConnections=0; 259 MSqlDatabase *newDb = NULL; 255 260 while (it != m_pool.end()) 256 261 { 262 totalConnections ++; 257 263 if ((*it)->m_lastDBKick.secsTo(now) <= 3600) 258 264 { 259 265 ++it; … … 264 270 // not been used in the past hour. 265 271 MSqlDatabase *entry = *it; 266 272 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..."); 268 297 delete entry; 298 VERBOSE(VB_IMPORTANT, "Done deleting idle DB connection."); 269 299 } 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 } 270 310 } 271 311 272 312 MSqlDatabase *MDBManager::getSchedCon()
