diff --git a/src/database/Connector.py b/src/database/Connector.py index ae84339b2f9513d24ba4baa86a2f4a478c32a678..808ba273a05d511674af80283162bc105571ee93 100644 --- a/src/database/Connector.py +++ b/src/database/Connector.py @@ -3,17 +3,25 @@ from sqlalchemy.orm import sessionmaker from scrapy.conf import settings -class Connector: - connection = None - engine = create_engine(settings['CONNECTION_STRING']) - Session = sessionmaker(bind=engine) +class Singleton(type): + _instances = {} + def __call__(cls, *args, **kwargs): + if cls not in cls._instances: + cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) + return cls._instances[cls] + + +class Connector(metaclass=Singleton): + __connection = None + __engine = create_engine(settings['CONNECTION_STRING']) + __Session = sessionmaker(bind=__engine) @staticmethod def get_session(): - return Connector.Session() + return Connector.__Session() @staticmethod def get_connection(): - if not Connector.connection: - Connector.connection = Connector.engine.connect() - return Connector.connection + if not Connector.__connection: + Connector.__connection = Connector.__engine.connect() + return Connector.__connection