Skip to content

Commit

Permalink
Refactor MySQLConnection to use connection pooling and update connect…
Browse files Browse the repository at this point in the history
…ion pool size parameter
  • Loading branch information
josephmancuso committed Oct 27, 2024
1 parent cf9bc84 commit 3010894
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
34 changes: 20 additions & 14 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,13 +57,12 @@ 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()

Expand All @@ -73,21 +72,25 @@ def make_connection(self):
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 @@ -104,7 +107,11 @@ 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 @@ -117,7 +124,7 @@ def create_connection(self, autocommit=True):
database=self.database,
**self.options
)

return connection

def reconnect(self):
Expand Down Expand Up @@ -194,11 +201,10 @@ 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
2 changes: 1 addition & 1 deletion tests/integrations/config/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"propagate": False,
"connection_pooling_enabled": True,
"connection_pooling_max_size": 10,
"connection_pool_min_size": None,
"connection_pooling_min_size": None,
},
"t": {"driver": "sqlite", "database": "orm.sqlite3", "log_queries": True, "foreign_keys": True},
"devprod": {
Expand Down

0 comments on commit 3010894

Please sign in to comment.