1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-20 07:49:42 +01:00

* Added retry blocks for failed (busy) sql queries & commits.

This commit is contained in:
Christian Muehlhaeuser 2012-06-06 16:25:44 +02:00
parent 872d8a3da5
commit d8c3006cb4
2 changed files with 30 additions and 1 deletions

View File

@ -18,10 +18,12 @@
#include "database/TomahawkSqlQuery.h"
#include "utils/TomahawkUtils.h"
#include "utils/Logger.h"
#include <QSqlError>
#include <QTime>
#include <QThread>
#include <QVariant>
#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()
{

View File

@ -34,9 +34,13 @@ public:
bool exec( const QString& query );
bool exec();
bool commitTransaction();
private:
void showError();
QSqlDatabase m_db;
};
#endif // TOMAHAWKSQLQUERY_H