diff --git a/ibis/backends/mysql/__init__.py b/ibis/backends/mysql/__init__.py index a22447b76b02c..fb9ce68453aa4 100644 --- a/ibis/backends/mysql/__init__.py +++ b/ibis/backends/mysql/__init__.py @@ -255,13 +255,19 @@ def drop_database(self, name: str, force: bool = False) -> None: def begin(self): con = self.con cur = con.cursor() + + if not con.autocommit_mode: + con.begin() + try: yield cur except Exception: - con.rollback() + if not con.autocommit_mode: + con.rollback() raise else: - con.commit() + if not con.autocommit_mode: + con.commit() finally: cur.close() @@ -279,14 +285,19 @@ def raw_sql(self, query: str | sg.Expression, **kwargs: Any) -> Any: con = self.con cursor = con.cursor() + if not con.autocommit_mode: + con.begin() + try: cursor.execute(query, **kwargs) except Exception: - con.rollback() + if not con.autocommit_mode: + con.rollback() cursor.close() raise else: - con.commit() + if not con.autocommit_mode: + con.commit() return cursor # TODO: disable positional arguments