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

* Keep retrying sql queries forever, as long as the returned db error indicates the db is just busy / locked.

This commit is contained in:
Christian Muehlhaeuser 2012-06-07 08:20:55 +02:00
parent 25ef791afc
commit 9443992ff9
2 changed files with 25 additions and 8 deletions

@ -59,8 +59,11 @@ TomahawkSqlQuery::exec()
unsigned int retries = 0; unsigned int retries = 0;
while ( !QSqlQuery::exec() && ++retries < 10 ) while ( !QSqlQuery::exec() && ++retries < 10 )
{ {
tDebug() << "INFO: Retrying failed query:" << this->lastQuery() << this->lastError().text(); if ( isBusyError( lastError() ) )
TomahawkUtils::msleep( 25 ); retries = 0;
tDebug() << "INFO: Retrying failed query:" << lastQuery() << lastError().text();
TomahawkUtils::msleep( 10 );
} }
bool ret = ( retries < 10 ); bool ret = ( retries < 10 );
@ -87,8 +90,11 @@ TomahawkSqlQuery::commitTransaction()
unsigned int retries = 0; unsigned int retries = 0;
while ( !m_db.commit() && ++retries < 10 ) while ( !m_db.commit() && ++retries < 10 )
{ {
tDebug() << "INFO: Retrying failed commit:" << this->lastQuery() << this->lastError().text(); if ( isBusyError( lastError() ) )
TomahawkUtils::msleep( 25 ); retries = 0;
tDebug() << "INFO: Retrying failed commit:" << lastQuery() << lastError().text();
TomahawkUtils::msleep( 10 );
} }
return ( retries < 10 ); return ( retries < 10 );
@ -98,10 +104,19 @@ TomahawkSqlQuery::commitTransaction()
void void
TomahawkSqlQuery::showError() TomahawkSqlQuery::showError()
{ {
tLog() << "\n" << "*** DATABASE ERROR ***" << "\n" tLog() << endl << "*** DATABASE ERROR ***" << endl
<< this->lastQuery() << "\n" << lastQuery() << endl
<< "boundValues:" << this->boundValues() << "\n" << "boundValues:" << boundValues() << endl
<< this->lastError().text() << "\n"; << lastError().text() << endl;
Q_ASSERT( false ); Q_ASSERT( false );
} }
bool
TomahawkSqlQuery::isBusyError( const QSqlError& error )
{
const QString text = error.text().toLower();
return ( text.contains( "locked" ) || text.contains( "busy" ) );
}

@ -38,6 +38,8 @@ public:
bool commitTransaction(); bool commitTransaction();
private: private:
bool isBusyError( const QSqlError& error );
void showError(); void showError();
QSqlDatabase m_db; QSqlDatabase m_db;