Skip to content

Instantly share code, notes, and snippets.

@nedludd0
Last active May 19, 2025 15:27
Show Gist options
  • Select an option

  • Save nedludd0/67b6559bae8e21a9ce1072bd3015bada to your computer and use it in GitHub Desktop.

Select an option

Save nedludd0/67b6559bae8e21a9ce1072bd3015bada to your computer and use it in GitHub Desktop.
Sql Raw on SqlAlchemy Engine with pool connections (Python Pattern Borg Singleton)
"""
SINGLETON CLASS ENCAPSULATING A SQLALCHEMY ENGINE and SESSION.
Credit: https://gist.github.com/skeeved/79dfe5652b20182586bb55c3cbc94250
"""
""" Config """
from os import environ
DB_TYPE = environ.get('DB_TYPE') # Example: mysql
DB_CONNECTOR = environ.get('DB_CONNECTOR') # Example: pymysql
DB_HOST = environ.get('DB_HOST')
DB_PORT = environ.get('DB_PORT')
DB_SCHEMA = environ.get('DB_SCHEMA')
DB_USER = environ.get('DB_USER')
DB_PASSWORD = environ.get('DB_PASSWORD')
SQLALCHEMY_DATABASE_URI = f"{DB_TYPE}+{DB_CONNECTOR}://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_SCHEMA}"
""" Import """
import sqlalchemy
class DB:
__sqlalchemy_url = SQLALCHEMY_DATABASE_URI
__option_engine_pool_size = 10
__option_engine_pool_recycle = 7200 # 120 minutes
"""Borg pattern singleton"""
__state = {}
def __init__(self):
self.__dict__ = self.__state
if not hasattr(self, 'engine'):
self.engine = sqlalchemy.create_engine( self.__sqlalchemy_url,
pool_size = self.__option_engine_pool_size,
pool_recycle = self.__option_engine_pool_recycle)
if __name__ == '__main__':
# get engine
db = DB()
# get a connection from pool under the engine
conn = db.engine.connect()
# use it
result = conn.execute('SELECT field1, field2 FROM table;')
for row in result:
print(row)
# "close" the connection --> returns it to the pool.
conn.close()
"""
https://docs.sqlalchemy.org/en/13/core/connections.html
The Engine.connect() method returns a Connection object. This Connection object is a proxy object for an actual
DBAPI connection. The DBAPI connection is retrieved from the connection pool at the point at which Connection is created.
The object returned is known as ResultProxy, which references a DBAPI cursor and provides methods for fetching rows
similar to that of the DBAPI cursor. The DBAPI cursor will be closed by the ResultProxy when all of its result rows
(if any) are exhausted. A ResultProxy that returns no rows, such as that of an UPDATE statement (without any returned rows),
releases cursor resources immediately upon construction.
When the Connection is closed, the referenced DBAPI connection is released to the connection pool.
From the perspective of the database itself, the connection pool will not actually “close” the connection assuming the
pool has room to store this connection for the next use. When the connection is returned to the pool for re-use,
the pooling mechanism issues a rollback() call on the DBAPI connection so that any transactional state or locks are removed,
and the connection is ready for its next use.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment