Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions docs/source/modules/connections.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
Connection Object
=================

Resetting a connection
----------------------

``connection.reset()`` clears the server session using ``COM_RESET_CONNECTION``
without reconnecting or authenticating again. It rolls back outstanding work and
discards temporary tables, user variables, locks and prepared statements. The
selected database and PyMySQL's current charset/collation are retained.

Autocommit and other session settings return to the server's defaults, which may
differ from the options passed to ``connect()``. In particular, reset does not
restore PyMySQL's default ``autocommit=False`` or rerun ``init_command``. Apply any
required session settings explicitly before reuse::

connection.reset()
connection.autocommit(False)
with connection.cursor() as cursor:
cursor.execute("SELECT 1")

``get_autocommit()`` reads the server's protocol status. MySQL 8.4 has a known
limitation when global autocommit is OFF: reset leaves the session variable OFF
but reports ON in protocol status, including subsequent responses. Consequently,
``get_autocommit()`` returns ``True`` and ``autocommit(True)`` may skip the SQL
command because it trusts that status. Reset does not normalize this inconsistent
server state. This limitation was reproduced on MySQL 8.4.11; MariaDB 11.8.6
reports the correct status with either global setting.

Close existing cursors before resetting and create new ones afterwards. Reset
returns ``None`` on success and raises the usual connection/server exceptions on
failure. The command requires MySQL 5.7.3 or MariaDB 10.2.4 or newer; this does not
extend PyMySQL's supported-server policy. Unsupported servers receive no fallback
command. Charset/collation are reapplied after a successful protocol reset; if
that fails, the connection is closed and the error is raised.

API reference
-------------

.. module:: pymysql.connections

.. autoclass:: Connection
Expand Down
37 changes: 37 additions & 0 deletions pymysql/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,43 @@ def rollback(self):
self._execute_command(COMMAND.COM_QUERY, "ROLLBACK")
self._read_ok_packet()

def reset(self):
"""Reset the session on the existing connection.

Roll back any active transaction, release locks and prepared statements,
drop temporary tables, and clear user variables. Session system variables,
including autocommit, return to the server's defaults. Connection options
such as ``autocommit``, ``sql_mode`` and ``init_command`` are not replayed.
The current database is retained.

``get_autocommit()`` follows the server's protocol status. MySQL 8.4
reports autocommit ON after reset even when global autocommit is OFF
and the session variable is OFF. This server inconsistency is not
normalized; see the connection documentation for details.

The current charset and collation are reapplied to keep the server and
the client's encoding consistent. If this fails after the reset, the
connection is closed. Connection result state is cleared; close existing
cursors before resetting and create new cursors afterwards.

Uses COM_RESET_CONNECTION (MySQL >= 5.7.3, MariaDB >= 10.2.4), subject to
PyMySQL's supported-server policy. There is no reconnect or fallback for
servers that do not support it. Returns ``None`` on success.

:raise InterfaceError: If the connection is closed.
:raise Error: If the server rejects the reset or communication fails.
"""
self._execute_command(COMMAND.COM_RESET_CONNECTION, b"")
self._read_ok_packet()
self._affected_rows = 0
self.autocommit_mode = self.get_autocommit()
try:
self.set_character_set(self.charset, self.collation)
except Exception:
# The reset changed the server charset, so the encoder may be stale.
self._force_close()
raise

def show_warnings(self):
"""Send the "SHOW WARNINGS" SQL command."""
self._execute_command(COMMAND.COM_QUERY, "SHOW WARNINGS")
Expand Down
3 changes: 2 additions & 1 deletion pymysql/constants/COMMAND.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
COM_STMT_FETCH = 0x1C
COM_DAEMON = 0x1D
COM_BINLOG_DUMP_GTID = 0x1E
COM_END = 0x1F
COM_RESET_CONNECTION = 0x1F
COM_END = 0x20
266 changes: 266 additions & 0 deletions pymysql/tests/test_reset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
import io
import struct
import unittest
from unittest import mock

import pytest

import pymysql
from pymysql.constants import CLIENT, CR, ER, SERVER_STATUS
from pymysql.tests import base


def packet(payload, sequence=1):
return struct.pack("<I", len(payload))[:3] + bytes([sequence]) + payload


def ok_packet(status=SERVER_STATUS.SERVER_STATUS_AUTOCOMMIT):
return packet(b"\x00\x00\x00" + struct.pack("<HH", status, 0))


def error_packet(code, message):
return packet(b"\xff" + struct.pack("<H", code) + b"#HY000" + message)


@pytest.mark.parametrize("autocommit", [False, True, None])
def test_reset_autocommit(autocommit):
params = base.PyMySQLTestCase.databases[0].copy()
params["autocommit"] = autocommit
with pymysql.connect(**params) as conn:
with conn.cursor() as cur:
cur.execute("SELECT @@GLOBAL.autocommit")
default = bool(cur.fetchone()[0])
conn.autocommit(not default)
conn.reset()
cached = conn.get_autocommit()
assert conn.autocommit_mode is cached
with conn.cursor() as cur:
cur.execute("SELECT @@SESSION.autocommit")
assert bool(cur.fetchone()[0]) is default
if not default and "MariaDB" not in conn.get_server_info() and cached:
pytest.xfail("MySQL reset reports autocommit ON with global autocommit OFF")
assert cached is default
conn.autocommit(not default)
with conn.cursor() as cur:
cur.execute("SELECT @@SESSION.autocommit")
assert bool(cur.fetchone()[0]) is not default


class TestResetProtocol(unittest.TestCase):
def connection(self, response):
conn = pymysql.connect(
defer_connect=True, charset="latin1", collation="latin1_bin"
)
conn._sock = mock.Mock()
conn._rfile = io.BytesIO(response)
conn._current_timeout = None
conn.server_status = SERVER_STATUS.SERVER_STATUS_IN_TRANS
conn._affected_rows = 7
self.addCleanup(conn._force_close)
return conn

def test_reset_command_and_state(self):
conn = self.connection(ok_packet() + ok_packet())
self.assertIsNone(conn.reset())
self.assertEqual(
conn._sock.sendall.call_args_list,
[
mock.call(b"\x01\x00\x00\x00\x1f"),
mock.call(
packet(b"\x03SET NAMES latin1 COLLATE latin1_bin", sequence=0)
),
],
)
self.assertTrue(conn.get_autocommit())
self.assertTrue(conn.autocommit_mode)
self.assertEqual(conn.server_status, SERVER_STATUS.SERVER_STATUS_AUTOCOMMIT)
self.assertEqual(conn.affected_rows(), 0)
self.assertEqual(conn.insert_id(), 0)
self.assertIsNone(conn._result)
self.assertEqual(conn._rfile.read(), b"")

def test_reset_autocommit_off(self):
conn = self.connection(ok_packet(0) + ok_packet(0))
conn.autocommit_mode = True
conn.reset()
self.assertFalse(conn.get_autocommit())
self.assertFalse(conn.autocommit_mode)

def test_closed(self):
conn = pymysql.connect(defer_connect=True)
with mock.patch.object(conn, "connect") as connect:
with self.assertRaises(pymysql.InterfaceError) as error:
conn.reset()
self.assertEqual(error.exception.args, (0, ""))
connect.assert_not_called()

def test_server_error(self):
conn = self.connection(error_packet(ER.UNKNOWN_COM_ERROR, b"Unknown command"))
with self.assertRaises(pymysql.OperationalError) as error:
conn.reset()
self.assertEqual(
error.exception.args, (ER.UNKNOWN_COM_ERROR, "Unknown command")
)
conn._sock.sendall.assert_called_once_with(b"\x01\x00\x00\x00\x1f")
self.assertEqual(conn.server_status, SERVER_STATUS.SERVER_STATUS_IN_TRANS)
self.assertEqual(conn.affected_rows(), 7)

def test_unexpected_response(self):
conn = self.connection(packet(b"\xfe\x00\x00\x00\x00"))
with self.assertRaises(pymysql.OperationalError) as error:
conn.reset()
self.assertEqual(error.exception.args[0], CR.CR_COMMANDS_OUT_OF_SYNC)

def test_lost_connection(self):
conn = self.connection(b"")
with mock.patch.object(conn, "connect") as connect:
with self.assertRaises(pymysql.OperationalError) as error:
conn.reset()
self.assertEqual(error.exception.args[0], CR.CR_SERVER_LOST)
self.assertFalse(conn.open)
connect.assert_not_called()

def test_charset_failure_closes_connection(self):
conn = self.connection(
ok_packet() + error_packet(ER.UNKNOWN_COLLATION, b"Bad collation")
)
with self.assertRaises(pymysql.OperationalError) as error:
conn.reset()
self.assertEqual(error.exception.args, (ER.UNKNOWN_COLLATION, "Bad collation"))
self.assertFalse(conn.open)

def test_write_failure(self):
conn = self.connection(b"")
conn._sock.sendall.side_effect = OSError("Connection lost")
with mock.patch.object(conn, "connect") as connect:
with self.assertRaises(pymysql.OperationalError) as error:
conn.reset()
self.assertEqual(error.exception.args[0], CR.CR_SERVER_GONE_ERROR)
self.assertFalse(conn.open)
connect.assert_not_called()


class TestReset(base.PyMySQLTestCase):
def test_reuse_and_transaction_rollback(self):
conn = self.connect(autocommit=False)
self.safe_create_table(
conn,
"reset_transaction",
"CREATE TABLE reset_transaction (id INT PRIMARY KEY AUTO_INCREMENT) ENGINE=InnoDB",
)
thread_id = conn.thread_id()
with conn.cursor() as cur:
cur.execute("INSERT INTO reset_transaction VALUES ()")
self.assertNotEqual(conn.insert_id(), 0)
self.assertEqual(conn.affected_rows(), 1)
self.assertIsNone(conn.reset())
self.assertEqual(conn.thread_id(), thread_id)
self.assertIsNone(conn._result)
self.assertEqual(conn.insert_id(), 0)
self.assertEqual(conn.affected_rows(), 0)
with conn.cursor() as cur:
cur.execute("SELECT COUNT(*) FROM reset_transaction")
self.assertEqual(cur.fetchone(), (0,))
cur.execute("SELECT CONNECTION_ID(), LAST_INSERT_ID()")
self.assertEqual(cur.fetchone(), (thread_id, 0))
cur.execute("INSERT INTO reset_transaction VALUES ()")
conn.commit()
conn.reset()
with conn.cursor() as cur:
cur.execute("SELECT COUNT(*) FROM reset_transaction")
self.assertEqual(cur.fetchone(), (1,))

def test_session_defaults(self):
conn = self.connect(
autocommit=False, sql_mode="ANSI", init_command="SET @reset_init=1"
)
with conn.cursor() as cur:
cur.execute("SELECT @@GLOBAL.autocommit, @@GLOBAL.sql_mode")
autocommit, sql_mode = cur.fetchone()
conn.autocommit(not autocommit)
cur.execute("CREATE TEMPORARY TABLE reset_temporary (id INT)")
cur.execute("SET @reset_variable=42")
cur.execute("SET SESSION sql_mode='NO_BACKSLASH_ESCAPES'")
self.assertEqual(conn.escape("a'b"), "'a''b'")
conn.reset()
with conn.cursor() as cur:
cur.execute(
"SELECT @@autocommit, @@sql_mode, @reset_variable, @reset_init, DATABASE()"
)
self.assertEqual(
cur.fetchone(),
(autocommit, sql_mode, None, None, self.databases[0]["database"]),
)
value = "a'b\\c"
cur.execute("SELECT %s", (value,))
self.assertEqual(cur.fetchone(), (value,))
with self.assertRaises(pymysql.ProgrammingError) as error:
cur.execute("SELECT * FROM reset_temporary")
self.assertEqual(error.exception.args[0], ER.NO_SUCH_TABLE)

def test_current_database_is_retained(self):
conn = self.connect()
database = self.databases[1]["database"]
conn.select_db(database)
conn.reset()
with conn.cursor() as cur:
cur.execute("SELECT DATABASE()")
self.assertEqual(cur.fetchone(), (database,))

def test_charset_and_collation(self):
for charset, collation, value in [
("latin1", "latin1_bin", "café"),
("utf8mb4", "utf8mb4_bin", "雪🌍"),
("utf8mb4", None, "雪🌍"),
]:
with self.subTest(charset=charset, collation=collation):
conn = self.connect(charset="utf8mb4")
conn.set_character_set(charset, collation)
encoding = conn.encoding
with conn.cursor() as cur:
cur.execute(
"SELECT @@character_set_client, @@character_set_connection, @@character_set_results, @@collation_connection"
)
expected = cur.fetchone()
conn.reset()
self.assertEqual(conn.character_set_name(), charset)
self.assertEqual(conn.encoding, encoding)
self.assertEqual(conn.collation, collation)
with conn.cursor() as cur:
cur.execute(
"SELECT @@character_set_client, @@character_set_connection, @@character_set_results, @@collation_connection"
)
self.assertEqual(cur.fetchone(), expected)
cur.execute("SELECT %s", (value,))
self.assertEqual(cur.fetchone(), (value,))

def test_pending_results(self):
conn = self.connect(client_flag=CLIENT.MULTI_STATEMENTS)
# Exercise the same draining path as other connection commands.
cur = conn.cursor()
cur.execute("SELECT 1; SELECT 2")
conn.reset()
cur.close()
with conn.cursor() as cur:
cur.execute("SELECT 3")
self.assertEqual(cur.fetchone(), (3,))

def test_unbuffered_result(self):
conn = self.connect()
cur = conn.cursor(pymysql.cursors.SSCursor)
cur.execute("SELECT 1 UNION ALL SELECT 2")
with pytest.warns(
UserWarning, match="Previous unbuffered result was left incomplete"
):
conn.reset()
cur.close()
with conn.cursor() as cur:
cur.execute("SELECT 3")
self.assertEqual(cur.fetchone(), (3,))

def test_closed_connection(self):
conn = self.connect()
conn.close()
with self.assertRaises(pymysql.InterfaceError):
conn.reset()
self.assertFalse(conn.open)
Loading