Skip to content

Commit 218eb75

Browse files
authored
Fix errors after database connection interruption (drogonframework#2350)
1 parent 3221c43 commit 218eb75

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

orm_lib/src/mysql_impl/MysqlConnection.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,14 @@ void MysqlConnection::execSqlInLoop(
433433
assert(rcb);
434434
assert(!isWorking_);
435435
assert(!sql.empty());
436-
436+
if (status_ != ConnectStatus::Ok)
437+
{
438+
LOG_ERROR << "Connection is not ready";
439+
auto exceptPtr =
440+
std::make_exception_ptr(drogon::orm::BrokenConnection());
441+
exceptCallback(exceptPtr);
442+
return;
443+
}
437444
callback_ = std::move(rcb);
438445
isWorking_ = true;
439446
exceptionCallback_ = std::move(exceptCallback);

orm_lib/src/postgresql_impl/PgBatchConnection.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,14 @@ void PgConnection::execSqlInLoop(
241241
std::function<void(const std::exception_ptr &)> &&exceptCallback)
242242
{
243243
LOG_TRACE << sql;
244+
if (status_ != ConnectStatus::Ok)
245+
{
246+
LOG_ERROR << "Connection is not ready";
247+
auto exceptPtr =
248+
std::make_exception_ptr(drogon::orm::BrokenConnection());
249+
exceptCallback(exceptPtr);
250+
return;
251+
}
244252
isWorking_ = true;
245253
batchSqlCommands_.emplace_back(
246254
std::make_shared<SqlCmd>(std::move(sql),

orm_lib/src/postgresql_impl/PgConnection.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,14 @@ void PgConnection::execSqlInLoop(
230230
assert(rcb);
231231
assert(!isWorking_);
232232
assert(!sql.empty());
233+
if (status_ != ConnectStatus::Ok)
234+
{
235+
LOG_ERROR << "Connection is not ready";
236+
auto exceptPtr =
237+
std::make_exception_ptr(drogon::orm::BrokenConnection());
238+
exceptCallback(exceptPtr);
239+
return;
240+
}
233241
sql_ = std::move(sql);
234242
callback_ = std::move(rcb);
235243
isWorking_ = true;

orm_lib/src/sqlite3_impl/Sqlite3Connection.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ void Sqlite3Connection::init()
128128
else
129129
{
130130
sqlite3_extended_result_codes(tmp, true);
131+
status_ = ConnectStatus::Ok;
131132
okCallback_(thisPtr);
132133
}
133134
});
@@ -167,6 +168,14 @@ void Sqlite3Connection::execSqlInQueue(
167168
const std::function<void(const std::exception_ptr &)> &exceptCallback)
168169
{
169170
LOG_TRACE << "sql:" << sql;
171+
if (status_ != ConnectStatus::Ok)
172+
{
173+
LOG_ERROR << "Connection is not ready";
174+
auto exceptPtr =
175+
std::make_exception_ptr(drogon::orm::BrokenConnection());
176+
exceptCallback(exceptPtr);
177+
return;
178+
}
170179
std::shared_ptr<sqlite3_stmt> stmtPtr;
171180
bool newStmt = false;
172181
if (paraNum > 0)
@@ -375,6 +384,7 @@ void Sqlite3Connection::disconnect()
375384
auto thisPtr = weakPtr.lock();
376385
if (!thisPtr)
377386
return;
387+
thisPtr->status_ = ConnectStatus::Bad;
378388
thisPtr->connectionPtr_.reset();
379389
}
380390
pro.set_value(1);

0 commit comments

Comments
 (0)