From d8c3006cb4ca516dc98bd4006c2a848f77e1e7ee Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Wed, 6 Jun 2012 16:25:44 +0200 Subject: [PATCH] * Added retry blocks for failed (busy) sql queries & commits. --- src/libtomahawk/database/TomahawkSqlQuery.cpp | 27 ++++++++++++++++++- src/libtomahawk/database/TomahawkSqlQuery.h | 4 +++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/libtomahawk/database/TomahawkSqlQuery.cpp b/src/libtomahawk/database/TomahawkSqlQuery.cpp index 5a5ea628a..3fbc28f2d 100644 --- a/src/libtomahawk/database/TomahawkSqlQuery.cpp +++ b/src/libtomahawk/database/TomahawkSqlQuery.cpp @@ -18,10 +18,12 @@ #include "database/TomahawkSqlQuery.h" +#include "utils/TomahawkUtils.h" #include "utils/Logger.h" #include #include +#include #include #define QUERY_THRESHOLD 60 @@ -35,6 +37,7 @@ TomahawkSqlQuery::TomahawkSqlQuery() TomahawkSqlQuery::TomahawkSqlQuery( const QSqlDatabase& db ) : QSqlQuery( db ) + , m_db( db ) { } @@ -53,12 +56,20 @@ TomahawkSqlQuery::exec() QTime t; t.start(); - bool ret = QSqlQuery::exec(); + unsigned int retries = 0; + while ( !QSqlQuery::exec() && ++retries < 10 ) + { + tDebug() << "INFO: Retrying failed query:" << this->lastQuery() << this->lastError().text(); + TomahawkUtils::msleep( 25 ); + } + + bool ret = ( retries < 10 ); if ( !ret ) showError(); int e = t.elapsed(); bool log = ( e >= QUERY_THRESHOLD ); + #ifdef TOMAHAWK_QUERY_ANALYZE log = true; #endif @@ -70,6 +81,20 @@ TomahawkSqlQuery::exec() } +bool +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 ); + } + + return ( retries < 10 ); +} + + void TomahawkSqlQuery::showError() { diff --git a/src/libtomahawk/database/TomahawkSqlQuery.h b/src/libtomahawk/database/TomahawkSqlQuery.h index 8bf1a1495..adb7153d5 100644 --- a/src/libtomahawk/database/TomahawkSqlQuery.h +++ b/src/libtomahawk/database/TomahawkSqlQuery.h @@ -34,9 +34,13 @@ public: bool exec( const QString& query ); bool exec(); + + bool commitTransaction(); private: void showError(); + + QSqlDatabase m_db; }; #endif // TOMAHAWKSQLQUERY_H