Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _initialize_pool(self):
self.pool = Queue(maxsize=self.max_pool_size)
for i in range(self.max_pool_size):
conn = Connection(host=self.host, port=self.port, user=self.user, password=self.password, db=self.db,
charset=self.charset, connect_timeout=self.timeout)
self.pool.put_nowait(conn)
def __init__(self, *args, **kwargs):
super(Connection, self).__init__()
self._pymysql_conn = pymysql.connections.Connection(*args, **kwargs)
def _create_connection(self):
"""Create a pymysql connection object
"""
return Connection(host=self._host,
user=self._user,
password=self._password,
database=self._database,
port=self._port,
charset=self._charset,
cursorclass=self._cursor_class,
**self._other_kwargs)
def close(self):
"""
Overwrite the close() method of pymysql.connections.Connection
With pool, put connection back to pool;
Without pool, send the quit message and close the socket
"""
if self._pool:
self._pool.put_connection(self)
else:
pymysql.connections.Connection.close(self)
def _create_connection(self):
"""Create a pymysql connection object
"""
return Connection(host=self._host,
user=self._user,
password=self._password,
database=self._database,
port=self._port,
charset=self._charset,
cursorclass=self._cursor_class,
**self._other_kwargs)
con_params = urlparse.urlparse(f'mysql://{mysql_path}')
assert con_params.hostname is not None
if con_params.scheme != 'mysql':
raise ValueError(f'unsupported URL scheme "{con_params.scheme}"')
self._db_args = MySQLCredentials(
host=con_params.hostname,
user=con_params.username,
password=con_params.password,
port=con_params.port or 3306,
db=self._parse_db_name(con_params)
)
self._connection: Connection
self._cursor: DictCursor
self._connected = False
self._version_checked: bool = False
self._db_keys: Optional[OrderedDict] = None
qualified_path = self._build_qualified_path(self._db_args)
super().__init__(qualified_path)
# -*- coding:utf-8 -*-
from __future__ import absolute_import
import sys
import socket
from pymysql.connections import Connection
from gtornado import AsyncSocket, green, utils
__all__ = ("AsyncConnection", "MySQLConnectionPool", "patch_pymysql")
class AsyncConnection(Connection):
def __init__(self, *args, **kwargs):
super(AsyncConnection, self).__init__(*args, **kwargs)
def connect(self, sock=None):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket = AsyncSocket(sock)
self.socket.connect((self.host, self.port))
self.socket.set_nodelay(True)
self._rfile = self.socket
self._get_server_information()
self._request_authentication()
if self.sql_mode is not None:
c = self.cursor()
c.execute("SET sql_mode=%s", (self.sql_mode,))
def __exit__(self, exc, value, traceback):
"""
Overwrite the __exit__() method of pymysql.connections.Connection
Base action: on successful exit, commit. On exception, rollback
With pool additional action: put connection back to pool
"""
pymysql.connections.Connection.__exit__(self, exc, value, traceback)
if self._pool:
if not exc or exc in self._reusable_exception:
'''reusable connection'''
self._pool.put_connection(self)
else:
'''no reusable connection, close it and create a new one then put it to the pool'''
self._pool.put_connection(self._recreate(*self.args, **self.kwargs))
self._pool = None
try:
self.close()
logger.warning("Close not reusable connection from pool(%s) caused by %s", self._pool.name, value)
except Exception:
pass