diff --git a/include/maxscale/session.h b/include/maxscale/session.h index 0b1cd1d97..cba1456a8 100644 --- a/include/maxscale/session.h +++ b/include/maxscale/session.h @@ -181,6 +181,7 @@ typedef struct session int refcount; /*< Reference count on the session */ bool ses_is_child; /*< this is a child session */ session_trx_state_t trx_state; /*< The current transaction state. */ + bool autocommit; /*< Whether autocommit is on. */ skygw_chk_t ses_chk_tail; } SESSION; @@ -289,6 +290,41 @@ static inline bool session_trx_is_read_write(const SESSION* ses) return ses->trx_state == SESSION_TRX_READ_WRITE; } +/** + * Tells whether autocommit is ON or not. + * + * Note that the returned value effectively only tells the last value + * of the statement "set autocommit=...". + * + * That is, if the statement "set autocommit=1" has been executed, then + * even if a transaction has been started, which implicitly will cause + * autocommit to be set to 0 for the duration of the transaction, this + * function will still return true. + * + * Note also that by default autocommit is ON. + * + * @return True if autocommit has been set ON, false otherwise. + */ +static inline bool session_is_autocommit(const SESSION* ses) +{ + return ses->autocommit; +} + +/** + * Sets the autocommit state of the session. + * + * NOTE: Only the protocol object may call this. + * + * @param enable True if autocommit is enabled, false otherwise. + * @return The previous state. + */ +static inline bool session_set_autocommit(SESSION* ses, bool autocommit) +{ + bool prev_autocommit = ses->autocommit; + ses->autocommit = autocommit; + return prev_autocommit; +} + MXS_END_DECLS #endif diff --git a/server/core/session.c b/server/core/session.c index f14def029..5e51cc48a 100644 --- a/server/core/session.c +++ b/server/core/session.c @@ -143,6 +143,7 @@ session_alloc(SERVICE *service, DCB *client_dcb) session->state = SESSION_STATE_READY; session->trx_state = SESSION_TRX_UNKNOWN; + session->autocommit = true; /* * Only create a router session if we are not the listening * DCB or an internal DCB. Creating a router session may create a connection to a diff --git a/server/modules/protocol/MySQL/MySQLClient/mysql_client.c b/server/modules/protocol/MySQL/MySQLClient/mysql_client.c index bb8ab60b7..655b4e53c 100644 --- a/server/modules/protocol/MySQL/MySQLClient/mysql_client.c +++ b/server/modules/protocol/MySQL/MySQLClient/mysql_client.c @@ -916,6 +916,14 @@ gw_read_finish_processing(DCB *dcb, GWBUF *read_buffer, uint64_t capabilities) { session_set_trx_state(ses, SESSION_TRX_INACTIVE); } + else if (type & QUERY_TYPE_ENABLE_AUTOCOMMIT) + { + session_set_autocommit(ses, true); + } + else if (type & QUERY_TYPE_DISABLE_AUTOCOMMIT) + { + session_set_autocommit(ses, false); + } /** * Feed each statement completely and separately