From 7cc13d11f9149bdec237fdaca99b4d5bb7612e81 Mon Sep 17 00:00:00 2001 From: Dario Date: Mon, 20 Jul 2020 16:35:00 +0200 Subject: [PATCH] add ping and update isConnected now pinging mysql server, instead returning bool value --- src/cpp/MySQL/Poco/SessionHandle.h | 4 ++++ src/cpp/MySQL/Poco/SessionImpl.h | 9 ++++----- src/cpp/MySQL/SessionHandle.cpp | 4 ++++ src/cpp/MySQL/SessionImpl.cpp | 26 ++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/cpp/MySQL/Poco/SessionHandle.h b/src/cpp/MySQL/Poco/SessionHandle.h index 7af836d40..878999b90 100644 --- a/src/cpp/MySQL/Poco/SessionHandle.h +++ b/src/cpp/MySQL/Poco/SessionHandle.h @@ -65,6 +65,10 @@ public: void rollback(); /// Rollback transaction + int ping(); + /// ping to check if connection is currently active + + operator MYSQL* (); private: diff --git a/src/cpp/MySQL/Poco/SessionImpl.h b/src/cpp/MySQL/Poco/SessionImpl.h index 19bd594f2..c9662bb01 100644 --- a/src/cpp/MySQL/Poco/SessionImpl.h +++ b/src/cpp/MySQL/Poco/SessionImpl.h @@ -24,6 +24,7 @@ #include "StatementExecutor.h" #include "ResultMetadata.h" #include "Poco/Mutex.h" +#include "Poco/Timestamp.h" namespace Poco { @@ -122,6 +123,8 @@ public: const std::string& connectorName() const; /// Returns the name of the connector. + inline void updateTimestamp() { _timestampLastActivity = Poco::Timestamp(); } + private: template @@ -157,6 +160,7 @@ private: bool _inTransaction; std::size_t _timeout; Poco::FastMutex _mutex; + Poco::Timestamp _timestampLastActivity; }; @@ -203,11 +207,6 @@ inline bool SessionImpl::isTransactionIsolation(Poco::UInt32 ti) return getTransactionIsolation() == ti; } - -inline bool SessionImpl::isConnected() -{ - return _connected; -} inline std::size_t SessionImpl::getConnectionTimeout() diff --git a/src/cpp/MySQL/SessionHandle.cpp b/src/cpp/MySQL/SessionHandle.cpp index ea502a468..8915ab941 100644 --- a/src/cpp/MySQL/SessionHandle.cpp +++ b/src/cpp/MySQL/SessionHandle.cpp @@ -175,5 +175,9 @@ void SessionHandle::rollback() throw TransactionException("Rollback failed.", _pHandle); } +int SessionHandle::ping() +{ + return mysql_ping(_pHandle); +} }}} // Poco::Data::MySQL diff --git a/src/cpp/MySQL/SessionImpl.cpp b/src/cpp/MySQL/SessionImpl.cpp index 011c14d6c..e695685f0 100644 --- a/src/cpp/MySQL/SessionImpl.cpp +++ b/src/cpp/MySQL/SessionImpl.cpp @@ -150,6 +150,7 @@ void SessionImpl::open(const std::string& connect) &SessionImpl::isAutoCommit); _connected = true; + _timestampLastActivity = Poco::Timestamp(); } @@ -174,6 +175,7 @@ void SessionImpl::begin() _handle.startTransaction(); _inTransaction = true; + _timestampLastActivity = Poco::Timestamp(); } @@ -181,6 +183,7 @@ void SessionImpl::commit() { _handle.commit(); _inTransaction = false; + _timestampLastActivity = Poco::Timestamp(); } @@ -188,6 +191,7 @@ void SessionImpl::rollback() { _handle.rollback(); _inTransaction = false; + _timestampLastActivity = Poco::Timestamp(); } @@ -196,6 +200,7 @@ void SessionImpl::autoCommit(const std::string&, bool val) StatementExecutor ex(_handle); ex.prepare(Poco::format("SET autocommit=%d", val ? 1 : 0)); ex.execute(); + _timestampLastActivity = Poco::Timestamp(); } @@ -206,6 +211,27 @@ bool SessionImpl::isAutoCommit(const std::string&) } + +bool SessionImpl::isConnected() +{ + return !_handle.ping(); + /* + if (Poco::Timestamp() > _timestampLastActivity + Poco::Timespan(_timeout, 0)) { + return _connected; + } + else { + _timestampLastActivity = Poco::Timestamp(); + if (!_handle.ping()) { + return true; + } + else { + return false; + } + } + */ +} + + void SessionImpl::setTransactionIsolation(Poco::UInt32 ti) { std::string isolation;