Skip to content

Commit

Permalink
Refactor MySQLConnection to handle closed connections during query ex…
Browse files Browse the repository at this point in the history
…ecution
  • Loading branch information
josephmancuso committed Oct 27, 2024
1 parent 9475b9e commit 09d082b
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions src/masoniteorm/connections/MySQLConnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(
if str(port).isdigit():
self.port = int(self.port)
self.database = database

self.user = user
self.password = password
self.prefix = prefix
Expand All @@ -57,40 +57,35 @@ def make_connection(self):
"You must have the 'pymysql' package installed to make a connection to MySQL. Please install it using 'pip install pymysql'"
)


if self.has_global_connection():
return self.get_global_connection()

# Check if there is an available connection in the pool
self._connection = self.create_connection()

self._connection.close = self.close_connection
self.enable_disable_foreign_keys()

# self._connection._open = 1
self.open = 1

return self

# Add the connection back to the pool when it's closed

def close_connection(self):
if (
self.full_details.get("connection_pooling_enabled")
and len(CONNECTION_POOL) < self.connection_pool_size
):
if self.full_details.get("connection_pooling_enabled") and len(CONNECTION_POOL) < self.connection_pool_size:
CONNECTION_POOL.append(self._connection)

self._connection = None

def create_connection(self, autocommit=True):
import pymysql
import pendulum
import pymysql.converters

pymysql.converters.conversions[pendulum.DateTime] = (
pymysql.converters.escape_datetime
)

pymysql.converters.conversions[
pendulum.DateTime
] = pymysql.converters.escape_datetime

# Initialize the connection pool if the option is set
initialize_size = self.full_details.get("connection_pooling_min_size")
if initialize_size and len(CONNECTION_POOL) < initialize_size:
Expand All @@ -107,11 +102,7 @@ def create_connection(self, autocommit=True):
)
CONNECTION_POOL.append(connection)

if (
self.full_details.get("connection_pooling_enabled")
and CONNECTION_POOL
and len(CONNECTION_POOL) > 0
):
if self.full_details.get("connection_pooling_enabled") and CONNECTION_POOL and len(CONNECTION_POOL) > 0:
connection = CONNECTION_POOL.pop()
else:
connection = pymysql.connect(
Expand All @@ -124,7 +115,7 @@ def create_connection(self, autocommit=True):
database=self.database,
**self.options
)

return connection

def reconnect(self):
Expand Down Expand Up @@ -201,10 +192,11 @@ def query(self, query, bindings=(), results="*"):
if not self.open:
if self._connection is None:
self._connection = self.create_connection()

self._connection.connect()

self._cursor = self._connection.cursor()


try:
with self._cursor as cursor:
Expand Down

0 comments on commit 09d082b

Please sign in to comment.