from __future__ import annotations
import cx_Oracle as oracle
class Connection(oracle.Connection):
__connection = None
@classmethod
def create(cls) -> Connection:
"""Returns a singleton cx_Oracle.Connection object."""
if not cls.__connection:
cls.__connection = oracle.connect(
user="xxx",
password="xxx",
dsn="xxx",
mode=oracle.SYSDBA
)
return cls.__connection
I want to use a singleton database connection object in my project to avoid creating a connection more than one. In this example oracle.connection
method returns the instance of the parent class.(which is cx_Oracle.Connection
)
My question is how can i return Connection
instance (which is the class i created) instead of cx_Oracle.Connection
class. Because I want to invoke some methods on my singleton object like;
connection = Connection.create()
connection.execute()
Only my Connection
class will have execute
method, not the parent class.
Aucun commentaire:
Enregistrer un commentaire