From e87a8fe962ec3424b73305117343a167afe79b9a Mon Sep 17 00:00:00 2001 From: Guenter Obiltschnig Date: Thu, 15 Sep 2016 12:01:30 +0200 Subject: [PATCH] handle MySQL connection lost/server gone when starting a transaction --- Data/MySQL/src/SessionHandle.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Data/MySQL/src/SessionHandle.cpp b/Data/MySQL/src/SessionHandle.cpp index 9815e4243..5af93044a 100644 --- a/Data/MySQL/src/SessionHandle.cpp +++ b/Data/MySQL/src/SessionHandle.cpp @@ -150,8 +150,17 @@ void SessionHandle::close() void SessionHandle::startTransaction() { - if (mysql_autocommit(_pHandle, false) != 0) - throw TransactionException("Start transaction failed.", _pHandle); + int rc = mysql_autocommit(_pHandle, false); + if (rc != 0) + { + // retry if connection lost + int err = mysql_errno(_pHandle); + if (err == 2006 /* CR_SERVER_GONE_ERROR */ || err == 2013 /* CR_SERVER_LOST */) + { + rc = mysql_autocommit(_pHandle, false); + } + } + if (rc != 0) throw TransactionException("Start transaction failed.", _pHandle); }