From 9443992ff9b7d42de7c58582e604eb2f368670df Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Thu, 7 Jun 2012 08:20:55 +0200 Subject: [PATCH] * Keep retrying sql queries forever, as long as the returned db error indicates the db is just busy / locked. --- src/libtomahawk/database/TomahawkSqlQuery.cpp | 31 ++++++++++++++----- src/libtomahawk/database/TomahawkSqlQuery.h | 2 ++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/libtomahawk/database/TomahawkSqlQuery.cpp b/src/libtomahawk/database/TomahawkSqlQuery.cpp index 3fbc28f2d..d62c02351 100644 --- a/src/libtomahawk/database/TomahawkSqlQuery.cpp +++ b/src/libtomahawk/database/TomahawkSqlQuery.cpp @@ -59,8 +59,11 @@ TomahawkSqlQuery::exec() unsigned int retries = 0; while ( !QSqlQuery::exec() && ++retries < 10 ) { - tDebug() << "INFO: Retrying failed query:" << this->lastQuery() << this->lastError().text(); - TomahawkUtils::msleep( 25 ); + if ( isBusyError( lastError() ) ) + retries = 0; + + tDebug() << "INFO: Retrying failed query:" << lastQuery() << lastError().text(); + TomahawkUtils::msleep( 10 ); } bool ret = ( retries < 10 ); @@ -87,8 +90,11 @@ TomahawkSqlQuery::commitTransaction() unsigned int retries = 0; while ( !m_db.commit() && ++retries < 10 ) { - tDebug() << "INFO: Retrying failed commit:" << this->lastQuery() << this->lastError().text(); - TomahawkUtils::msleep( 25 ); + if ( isBusyError( lastError() ) ) + retries = 0; + + tDebug() << "INFO: Retrying failed commit:" << lastQuery() << lastError().text(); + TomahawkUtils::msleep( 10 ); } return ( retries < 10 ); @@ -98,10 +104,19 @@ TomahawkSqlQuery::commitTransaction() void TomahawkSqlQuery::showError() { - tLog() << "\n" << "*** DATABASE ERROR ***" << "\n" - << this->lastQuery() << "\n" - << "boundValues:" << this->boundValues() << "\n" - << this->lastError().text() << "\n"; + tLog() << endl << "*** DATABASE ERROR ***" << endl + << lastQuery() << endl + << "boundValues:" << boundValues() << endl + << lastError().text() << endl; Q_ASSERT( false ); } + + +bool +TomahawkSqlQuery::isBusyError( const QSqlError& error ) +{ + const QString text = error.text().toLower(); + + return ( text.contains( "locked" ) || text.contains( "busy" ) ); +} diff --git a/src/libtomahawk/database/TomahawkSqlQuery.h b/src/libtomahawk/database/TomahawkSqlQuery.h index adb7153d5..ad00caa11 100644 --- a/src/libtomahawk/database/TomahawkSqlQuery.h +++ b/src/libtomahawk/database/TomahawkSqlQuery.h @@ -38,6 +38,8 @@ public: bool commitTransaction(); private: + bool isBusyError( const QSqlError& error ); + void showError(); QSqlDatabase m_db;