From 034f254d5c31d794b72436a32de434bef315ac0f Mon Sep 17 00:00:00 2001 From: Arin Alexander Date: Fri, 10 Feb 2017 23:08:49 +0300 Subject: [PATCH] Connection will be restored if server is gone away or drop limereport connection --- limereport/lrdatasourcemanager.cpp | 14 +++++++++++--- limereport/lrdatasourcemanager.h | 1 + 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/limereport/lrdatasourcemanager.cpp b/limereport/lrdatasourcemanager.cpp index 42ec024..f97c84f 100644 --- a/limereport/lrdatasourcemanager.cpp +++ b/limereport/lrdatasourcemanager.cpp @@ -702,12 +702,17 @@ bool DataSourceManager::initAndOpenDB(QSqlDatabase& db, ConnectionDesc& connecti return connected; } +bool DataSourceManager::checkConnection(QSqlDatabase& db){ + QSqlQuery query("Select 1",db); + return query.first(); +} + bool DataSourceManager::connectConnection(ConnectionDesc *connectionDesc) { bool connected = false; clearErrors(); - QString lastError =""; + QString lastError = ""; foreach(QString datasourceName, dataSourceNames()){ dataSourceHolder(datasourceName)->clearErrors(); @@ -715,7 +720,7 @@ bool DataSourceManager::connectConnection(ConnectionDesc *connectionDesc) if (!QSqlDatabase::contains(connectionDesc->name())){ QSqlDatabase db = QSqlDatabase::addDatabase(connectionDesc->driver(),connectionDesc->name()); - connected=initAndOpenDB(db, *connectionDesc); + connected = initAndOpenDB(db, *connectionDesc); if (!connected){ setLastError(db.lastError().text()); return false; @@ -726,7 +731,10 @@ bool DataSourceManager::connectConnection(ConnectionDesc *connectionDesc) db.close(); connected = initAndOpenDB(db, *connectionDesc); } else { - connected = db.isOpen(); +// connected = db.isOpen(); + connected = checkConnection(db); + if (!connected) + connected = initAndOpenDB(db, *connectionDesc); } } diff --git a/limereport/lrdatasourcemanager.h b/limereport/lrdatasourcemanager.h index fcf0f99..5b43cce 100644 --- a/limereport/lrdatasourcemanager.h +++ b/limereport/lrdatasourcemanager.h @@ -215,6 +215,7 @@ protected: void setLastError(const QString& value); void invalidateLinkedDatasources(QString datasourceName); + bool checkConnection(QSqlDatabase &db); private slots: void slotConnectionRenamed(const QString& oldName,const QString& newName); void slotQueryTextChanged(const QString& queryName, const QString& queryText);