Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
Apply @recoverable decorator to MySQLConnection.begin_transaction()
Browse files Browse the repository at this point in the history
In the event that the connection has been lost, causing an
exception when the connection attempts to set the autoconnect mode,
the operation should be retried.
  • Loading branch information
onlywade committed May 20, 2019
1 parent b2c7928 commit 5bab544
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion orator/connections/mysql_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from ..utils import decode
from ..utils import PY2
from .connection import Connection
from .connection import Connection, recoverable
from ..query.grammars.mysql_grammar import MySQLQueryGrammar
from ..query.processors.mysql_processor import MySQLQueryProcessor
from ..schema.grammars import MySQLSchemaGrammar
Expand Down Expand Up @@ -37,6 +37,7 @@ def get_default_schema_grammar(self):
def get_schema_manager(self):
return MySQLSchemaManager(self)

@recoverable
def begin_transaction(self):
self._connection.autocommit(False)

Expand Down
13 changes: 13 additions & 0 deletions tests/connections/test_mysql_connection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# -*- coding: utf-8 -*-

from flexmock import flexmock

from .. import OratorTestCase
from .. import mock

from orator.connections.mysql_connection import MySQLConnection

Expand All @@ -20,3 +23,13 @@ def test_marker_use_qmark_false(self):
connection = MySQLConnection(None, "database", "", {"use_qmark": False})

self.assertIsNone(connection.get_marker())

def test_recover_if_caused_by_lost_connection_is_called(self):
connection = flexmock(MySQLConnection(None, "database"))
connection._connection = mock.Mock()
connection._connection.autocommit.side_effect = Exception("lost connection")

connection.should_receive("_recover_if_caused_by_lost_connection").once()
connection.should_receive("reconnect")

connection.begin_transaction()

0 comments on commit 5bab544

Please sign in to comment.