From bbb810949e86e49dcc307dd84c33985824fc6877 Mon Sep 17 00:00:00 2001 From: antao Date: Tue, 15 Jan 2019 17:55:09 +0800 Subject: [PATCH 1/2] Modify the DbClientImpl class --- orm_lib/src/DbClientImpl.cc | 4 ++++ orm_lib/src/DbConnection.h | 1 + orm_lib/src/mysql_impl/MysqlConnection.cc | 10 ++++++++++ orm_lib/src/mysql_impl/MysqlConnection.h | 1 + orm_lib/src/postgresql_impl/PgConnection.cc | 14 ++++++++++++++ orm_lib/src/postgresql_impl/PgConnection.h | 1 + orm_lib/src/sqlite3_impl/Sqlite3Connection.cc | 11 +++++++++++ orm_lib/src/sqlite3_impl/Sqlite3Connection.h | 1 + 8 files changed, 43 insertions(+) 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; From f20b9199e187737d959d2120766ee2ac03d6165c Mon Sep 17 00:00:00 2001 From: an-tao <20741618@qq.com> Date: Tue, 15 Jan 2019 18:09:21 +0800 Subject: [PATCH 2/2] Increase the size of the sql command bufferr --- orm_lib/src/DbClientImpl.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/orm_lib/src/DbClientImpl.cc b/orm_lib/src/DbClientImpl.cc index 52351914..696c0958 100644 --- a/orm_lib/src/DbClientImpl.cc +++ b/orm_lib/src/DbClientImpl.cc @@ -186,7 +186,7 @@ void DbClientImpl::execSql(std::string &&sql, bool busy = false; { std::lock_guard guard(_bufferMutex); - if (_sqlCmdBuffer.size() > 10000) + if (_sqlCmdBuffer.size() > 200000) { //too many queries in buffer; busy = true;