I use aiohttp
and sqlalchemy
, and I created Singleton, which helps me connect when I need an instance of SQLAlchemy (the following code). Unfortunately, every time after a while I get the following error (which I "solve" when the server restarts):
Dec 11 09:35:29 ip-xxx-xxx-xxx-xxx gunicorn [16513]: sqlalchemy.exc.StatementError: (sqlalchemy.exc.InvalidRequestError) Unable to reconnect until the invalid transaction is canceled [SQL : '... \ nFROM ... \ nWHERE ... =% (username_1) s \ n LIMIT% (param_1) s'] [parameters: [{}]] `` `
Is there a way to fix the current code? Thanks:)
CONNECTION_DETAILS = { 'driver': 'pymysql', 'dialect': 'mysql', 'host': os.environ.get('HOST'), 'port': 3306, 'user': 'master', 'password': os.environ.get('PASSWORD'), 'database': 'ourdb', 'charset': 'utf8' } _instance = None def __new__(cls, *args, **kwargs): if not cls._instance: con_str = '{dialect}+{driver}://{user}:{password}@' \ '{host}:{port}/{database}?charset={charset}'\ .format(**cls.CONNECTION_DETAILS) try: engine = sqlalchemy.create_engine(con_str) Session = scoped_session(sessionmaker(bind=engine)) session = Session()
source share