django - single database connection throughout the python application (following singleton pattern) -
my question best way maintain single database connection in entire application? using singleton pattern? how?
conditions needed taken care of:
- in case of multiple requests, should using same connection
- in case connection closed, create new connection
- if connection has timed-out, on new request code should create new connection.
the driver database not supported django orm. , due same driver related issues, using pyodbc
connect database. right having below class creating , managing db connections:
class dbconnection(object): def __init__(self, driver, serve, database, user, password): self.driver = driver self.server = server self.database = database self.user = user self.password = password def __enter__(self): self.dbconn = pyodbc.connect("driver={};".format(self.driver) +\ "server={};".format(self.server) +\ "database={};".format(self.database) +\ "uid={};".format(self.user) +\ "pwd={};".format(self.password) + \ "charset=utf8", # "", ansi=true) return self.dbconn def __exit__(self, exc_type, exc_val, exc_tb): self.dbconn.close()
but issue approach create new database connection each query. better way following singleton pattern? way can think of hold reference connection if connection closed. like:
def get_database_connection(): conn = dbconnection.connection if not conn: conn = dbconnection.connection = dbconnection.create_connection() return conn
what best way achieve this? suggestion/ideas/examples?
ps: checking using weakref
allows create weak references objects. think idea use weakref
along singleton pattern storing connection variable. way won't have keep connection alive
when db not in use. guys this?
for now, going ahead singleton class approach. seeing potential flaws in this, feel mention them :)
dbconnector
class creating connection
class dbconnector(object): def __init__(self, driver, server, database, user, password): self.driver = driver self.server = server self.database = database self.user = user self.password = password self.dbconn = none # creats new connection def create_connection(self): return pyodbc.connect("driver={};".format(self.driver) + \ "server={};".format(self.server) + \ "database={};".format(self.database) + \ "uid={};".format(self.user) + \ "pwd={};".format(self.password) + \ "charset=utf8", ansi=true) # explicitly opening database connection def __enter__(self): self.dbconn = self.create_connection() return self.dbconn def __exit__(self, exc_type, exc_val, exc_tb): self.dbconn.close()
dbconnection
class managing connections
class dbconnection(object): connection = none @classmethod def get_connection(cls, new=false): """creates return new singleton database connection""" if new or not cls.connection: cls.connection = dbconnector().create_connection() return cls.connection @classmethod def execute_query(cls, query): """execute query on singleton db connection""" connection = cls.get_connection() try: cursor = connection.cursor() except pyodbc.programmingerror: connection = cls.get_connection(new=true) # create new connection cursor = connection.cursor() cursor.execute(query) result = cursor.fetchall() cursor.close() return result
Comments
Post a Comment