File tree 4 files changed +51
-0
lines changed
4 files changed +51
-0
lines changed Original file line number Diff line number Diff line change @@ -452,6 +452,17 @@ def connect(self, **kwargs):
452
452
self ._open_connection ()
453
453
self ._post_connection ()
454
454
455
+ def shutdown (self ):
456
+ """Shut down connection to MySQL Server.
457
+ """
458
+ if not self ._socket :
459
+ return
460
+
461
+ try :
462
+ self ._socket .shutdown ()
463
+ except (AttributeError , errors .Error ):
464
+ pass # Getting an exception would mean we are disconnected.
465
+
455
466
def disconnect (self ):
456
467
"""Disconnect from the MySQL server
457
468
"""
Original file line number Diff line number Diff line change @@ -98,6 +98,15 @@ def get_address(self):
98
98
"""Get the location of the socket"""
99
99
raise NotImplementedError
100
100
101
+ def shutdown (self ):
102
+ """Shut down the socket before closing it"""
103
+ try :
104
+ self .sock .shutdown (socket .SHUT_RDWR )
105
+ self .sock .close ()
106
+ del self ._packet_queue
107
+ except (socket .error , AttributeError ):
108
+ pass
109
+
101
110
def close_connection (self ):
102
111
"""Close the socket"""
103
112
try :
Original file line number Diff line number Diff line change 31
31
import unittest
32
32
from decimal import Decimal
33
33
import io
34
+ import socket
34
35
35
36
import tests
36
37
from . import PY2
@@ -1820,6 +1821,31 @@ def test_cmd_reset_connection(self):
1820
1821
else :
1821
1822
self .assertNotEqual ((b'2' ,), self .cnx .get_rows ()[0 ][0 ])
1822
1823
1824
+ def test_shutdown (self ):
1825
+ """Shutting down a connection"""
1826
+ config = tests .get_mysql_config ()
1827
+ cnx = connection .MySQLConnection (** config )
1828
+ sql = "SHOW GLOBAL STATUS WHERE variable_name LIKE 'Aborted_clients'"
1829
+ cur = cnx .cursor ()
1830
+ cur .execute (sql )
1831
+ aborted_clients = cur .fetchone ()[1 ]
1832
+
1833
+ test_close_cnx = connection .MySQLConnection (** config )
1834
+ test_shutdown_cnx = connection .MySQLConnection (** config )
1835
+
1836
+ test_close_cnx .close ()
1837
+ cur .execute (sql )
1838
+ self .assertEqual (aborted_clients , cur .fetchone ()[1 ])
1839
+
1840
+ test_shutdown_cnx .shutdown ()
1841
+ self .assertRaises (socket .error ,
1842
+ test_shutdown_cnx ._socket .sock .getsockname )
1843
+ cur .execute (sql )
1844
+ self .assertEqual (str (int (aborted_clients ) + 1 ), cur .fetchone ()[1 ])
1845
+
1846
+ cur .close ()
1847
+ cnx .close ()
1848
+
1823
1849
1824
1850
class WL7937 (tests .MySQLConnectorTests ):
1825
1851
"""Allow 'LOAD DATA LOCAL INFILE' by default
Original file line number Diff line number Diff line change @@ -106,6 +106,11 @@ def test_get_address(self):
106
106
"""Get the address of a connection"""
107
107
self .assertRaises (NotImplementedError , self .cnx .get_address )
108
108
109
+ def test_shutdown (self ):
110
+ """Shutting down a connection"""
111
+ self .cnx .shutdown ()
112
+ self .assertEqual (None , self .cnx .sock )
113
+
109
114
def test_close_connection (self ):
110
115
"""Closing a connection"""
111
116
self .cnx .close_connection ()
You can’t perform that action at this time.
0 commit comments