diff --git a/orm_lib/src/DbClientImpl.cc b/orm_lib/src/DbClientImpl.cc index 9c40cf6c..52351914 100644 --- a/orm_lib/src/DbClientImpl.cc +++ b/orm_lib/src/DbClientImpl.cc @@ -97,6 +97,10 @@ DbClientImpl::DbClientImpl(const std::string &connInfo, const size_t connNum, Cl DbClientImpl::~DbClientImpl() noexcept { std::lock_guard lock(_connectionsMutex); + for (auto &conn : _connections) + { + conn->disconnect(); + } _connections.clear(); _readyConnections.clear(); _busyConnections.clear(); diff --git a/orm_lib/src/DbConnection.h b/orm_lib/src/DbConnection.h index 389a1871..82f2aad6 100644 --- a/orm_lib/src/DbConnection.h +++ b/orm_lib/src/DbConnection.h @@ -65,6 +65,7 @@ class DbConnection : public trantor::NonCopyable } ConnectStatus status() const { return _status; } trantor::EventLoop *loop() { return _loop; } + virtual void disconnect() = 0; protected: QueryCallback _cb; diff --git a/orm_lib/src/mysql_impl/MysqlConnection.cc b/orm_lib/src/mysql_impl/MysqlConnection.cc index eb840694..e3178cbe 100644 --- a/orm_lib/src/mysql_impl/MysqlConnection.cc +++ b/orm_lib/src/mysql_impl/MysqlConnection.cc @@ -149,6 +149,16 @@ void MysqlConnection::handleClosed() auto thisPtr = shared_from_this(); _closeCb(thisPtr); } +void MysqlConnection::disconnect() +{ + auto thisPtr = shared_from_this(); + _loop->runInLoop([thisPtr]() { + thisPtr->_status = ConnectStatus_Bad; + thisPtr->_channelPtr->disableAll(); + thisPtr->_channelPtr->remove(); + thisPtr->_mysqlPtr.reset(); + }); +} void MysqlConnection::handleTimeout() { LOG_TRACE << "channel index:" << _channelPtr->index(); diff --git a/orm_lib/src/mysql_impl/MysqlConnection.h b/orm_lib/src/mysql_impl/MysqlConnection.h index 60f300cf..fd291d7c 100644 --- a/orm_lib/src/mysql_impl/MysqlConnection.h +++ b/orm_lib/src/mysql_impl/MysqlConnection.h @@ -45,6 +45,7 @@ class MysqlConnection : public DbConnection, public std::enable_shared_from_this ResultCallback &&rcb, std::function &&exceptCallback, std::function &&idleCb) override; + virtual void disconnect() override; private: std::unique_ptr _channelPtr; diff --git a/orm_lib/src/postgresql_impl/PgConnection.cc b/orm_lib/src/postgresql_impl/PgConnection.cc index 01206fb9..91ec3bb5 100644 --- a/orm_lib/src/postgresql_impl/PgConnection.cc +++ b/orm_lib/src/postgresql_impl/PgConnection.cc @@ -88,6 +88,20 @@ void PgConnection::handleClosed() auto thisPtr = shared_from_this(); _closeCb(thisPtr); } +void PgConnection::disconnect() +{ + std::promise pro; + auto f = pro.get_future(); + auto thisPtr = shared_from_this(); + _loop->runInLoop([thisPtr, &pro]() { + thisPtr->_status = ConnectStatus_Bad; + thisPtr->_channel.disableAll(); + thisPtr->_channel.remove(); + thisPtr->_connPtr.reset(); + pro.set_value(1); + }); + f.get(); +} void PgConnection::pgPoll() { _loop->assertInLoopThread(); diff --git a/orm_lib/src/postgresql_impl/PgConnection.h b/orm_lib/src/postgresql_impl/PgConnection.h index 79ef8ec0..489a6140 100644 --- a/orm_lib/src/postgresql_impl/PgConnection.h +++ b/orm_lib/src/postgresql_impl/PgConnection.h @@ -45,6 +45,7 @@ class PgConnection : public DbConnection, public std::enable_shared_from_this &&exceptCallback, std::function &&idleCb) override; + virtual void disconnect() override; private: std::shared_ptr _connPtr; diff --git a/orm_lib/src/sqlite3_impl/Sqlite3Connection.cc b/orm_lib/src/sqlite3_impl/Sqlite3Connection.cc index a47ac869..49a9fc4c 100644 --- a/orm_lib/src/sqlite3_impl/Sqlite3Connection.cc +++ b/orm_lib/src/sqlite3_impl/Sqlite3Connection.cc @@ -253,3 +253,14 @@ int Sqlite3Connection::stmtStep(sqlite3_stmt *stmt, const std::shared_ptr pro; + auto f = pro.get_future(); + auto thisPtr = shared_from_this(); + _loopThread.getLoop()->runInLoop([thisPtr, &pro]() { + thisPtr->_conn.reset(); + pro.set_value(1); + }); + f.get(); +} \ No newline at end of file diff --git a/orm_lib/src/sqlite3_impl/Sqlite3Connection.h b/orm_lib/src/sqlite3_impl/Sqlite3Connection.h index 163e6b6d..c538b32f 100644 --- a/orm_lib/src/sqlite3_impl/Sqlite3Connection.h +++ b/orm_lib/src/sqlite3_impl/Sqlite3Connection.h @@ -48,6 +48,7 @@ class Sqlite3Connection : public DbConnection, public std::enable_shared_from_th ResultCallback &&rcb, std::function &&exceptCallback, std::function &&idleCb) override; + virtual void disconnect() override; private: static std::once_flag _once;