psycopg2.errors.IdleInTransactionSessionTimeout



今天我想和大家分享一下使用psycopg2的不便:

Traceback (most recent call last):
Mar 8 14:21:24 812495ebf967 mi-app-dev INFO sqlalchemy.engine.base.Engine {'order_id_1': 6056197, 'param_1': 1}
Mar 8 14:21:24 812495ebf967 mi-app-dev   File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1276, in _execute_context
Mar 8 14:21:24 812495ebf967 mi-app-dev     self.dialect.do_execute(
Mar 8 14:21:24 812495ebf967 mi-app-dev   File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 609, in do_execute
Mar 8 14:21:24 812495ebf967 mi-app-dev     cursor.execute(statement, parameters)
Mar 8 14:21:24 812495ebf967 mi-app-dev   File "/usr/local/lib/python3.8/site-packages/dbapi_opentracing/psycopg2_tracing.py", line 38, in execute
Mar 8 14:21:24 812495ebf967 mi-app-dev     return self._traced_execution(self._cursor_factory.execute, self, *args, **kwargs)
Mar 8 14:21:24 812495ebf967 mi-app-dev   File "/usr/local/lib/python3.8/site-packages/dbapi_opentracing/tracing.py", line 145, in _traced_execution
Mar 8 14:21:24 812495ebf967 mi-app-dev     val = func(*args, **kwargs)
Mar 8 14:21:24 812495ebf967 mi-app-dev psycopg2.errors.IdleInTransactionSessionTimeout: terminating connection due to idle-in-transaction timeout
Mar 8 14:21:24 812495ebf967 mi-app-dev SSL connection has been closed unexpectedly

我正在使用下一个连接:

import logging
from contextlib import contextmanager
from sqlalchemy import Table
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from sqlalchemy.orm import sessionmaker, scoped_session
from ..models.base_model import Base

class DataAccessLayer:
def __init__(self, base_model: Base):
self.engine = None
self.Session = None
self.Base = base_model
def connect(self, db_connection: str):
self.engine = create_engine(db_connection, pool_size=20, max_overflow=0, pool_pre_ping=True, echo=True)
self.Session = scoped_session(sessionmaker(bind=self.engine))
@contextmanager
def transaction(self):
session = self.Session()
try:
yield session
session.commit()
except Exception as e:
logging.error(e)
session.rollback()
logging.warning('session rolled back')
raise
finally:
session.close()
logging.info('session closed')
def get_session(self) -> Session:
return self.Session()
def reset_db(self) -> None:
self.Base.metadata.drop_all(self.engine)
def create_all(self) -> None:
self.Base.metadata.create_all(bind=self.engine)
def drop_table(self, table: Table) -> None:
table.drop(self.engine)

我不太明白连接发生了什么,因为我们控制了异常。但是,我们收到这个错误,:(,如果你能帮助我我真的很感激,对不起,我正在努力提高我的英语技能,事先非常感谢你

你的代码可以空闲(没有选择/更新/…)与postgres查询,和Postgres可以关闭连接(在你的代码到达提交之前)基于它IdleInTransactionSessionTimeout设置在config =>如果你不需要事务,你可以用Idle来修改postgresql的配置,或者重新连接到postgresql。

相关内容

  • 没有找到相关文章

最新更新