与数据库的连接将被正确关闭?psycopg2



我已经使用这个代码约10天了,它工作得很好。今天它崩溃了,并显示了一个错误信息:

File "/home/ubuntu/jagj -bot/utils/db.py",第22行,在check_user . conf文件中self.connection:psycopg2。接口错误:连接已关闭

代码:

@dataclass
class Database:
def __init__(self):
self.connection = psycopg2.connect(config.DB_URI, sslmode='require')
self.cursor = self.connection.cursor()
self.current_date = utc.localize(datetime.now())
def check_user(self, user_id):
with self.connection:
self.cursor.execute(f"SELECT user_id FROM mango WHERE user_id = {user_id}")
result = self.cursor.fetchone()
if result:
return True
return False

更新:修正了错误处理。我需要这条线吗?Cursor = self.connection.cursor()' in except block?

def check_user(self, user_id):
try:
with self.connection:
self.cursor.execute(f"SELECT user_id FROM mango WHERE user_id = {user_id}")
except psycopg2.InterfaceError as exc:
self.connection = psycopg2.connect(DB_URI)
# self.cursor = self.connection.cursor() What about this string?
with self.connection:
self.cursor.execute(f"SELECT user_id FROM mango WHERE user_id = {user_id}")
finally:
result = self.cursor.fetchone()
if result:
return True
return False

这是一个已知的问题,Jagster的维护者已经修复了。请将数据库模块从PyPI更新到0.1.5或更高版本。您可以在这里找到如何这样做的说明:https://github.com/Jagster-Bot/Jagster-Bot/issues/1218

最新更新