I use sqlalchemy scoped_session to work with the Postgresql database. This is how session () is created:

 from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker, scoped_session from contextlib import contextmanager Base = declarative_base() engine = create_engine('postgresql://пользователь:пароль@localhost:5432/users', pool_size=100, max_overflow=100, echo=True) @contextmanager def session(): connection = engine.connect() db_session = scoped_session(sessionmaker(autocommit=True, autoflush=True, bind=engine)) try: yield db_session except Exception as e: print(e) finally: db_session.remove() connection.close() 

And this is how I use it:

 with session() as ssion: *какой-то код* 

The with block is called by Flask and after about 10 minutes an error appears in the console (during the method call):

 psycopg2.DatabaseError: SSL SYSCALL error: EOF detected 

I tried changing the values ​​of pool_size, max_overflow to more - did not help, tried to set autoflush = False, which also did not help

UPD1 Postgresql Logs:

 2018-05-10 19:51:23 MSK [9087-193] pibotuser@users LOG: statement: ROLLBACK 2018-05-10 19:52:26 MSK [9088-181] pibotuser@users LOG: statement: BEGIN 2018-05-10 19:52:26 MSK [9088-182] pibotuser@users LOG: statement: select * from users_activity where score=(select max(score) from users_activity); 2018-05-10 19:52:26 MSK [9088-183] pibotuser@users LOG: statement: ROLLBACK 2018-05-10 19:52:26 MSK [9088-184] pibotuser@users LOG: statement: BEGIN 2018-05-10 19:52:26 MSK [9088-185] pibotuser@users LOG: statement: select * from users_activity where sub_date=(select max(sub_date) from users_activity); 2018-05-10 19:52:26 MSK [9088-186] pibotuser@users LOG: statement: ROLLBACK 2018-05-10 19:53:27 MSK [9087-194] pibotuser@users LOG: statement: BEGIN 2018-05-10 19:53:27 MSK [9087-195] pibotuser@users LOG: statement: select * from users_activity where score=(select max(score) from users_activity); 2018-05-10 19:53:27 MSK [9087-196] pibotuser@users LOG: statement: ROLLBACK 2018-05-10 19:53:27 MSK [9087-197] pibotuser@users LOG: statement: BEGIN 2018-05-10 19:53:27 MSK [9087-198] pibotuser@users LOG: statement: select * from users_activity where sub_date=(select max(sub_date) from users_activity); 2018-05-10 19:53:27 MSK [9087-199] pibotuser@users LOG: statement: ROLLBACK 2018-05-10 19:53:28 MSK [10577-16] pibotuser@users LOG: statement: BEGIN 2018-05-10 19:53:28 MSK [10577-17] pibotuser@users LOG: statement: select * from users where id=466818033; 2018-05-10 19:53:28 MSK [10577-18] pibotuser@users LOG: statement: select * from users where id=466818033 2018-05-10 19:53:30 MSK [10577-19] pibotuser@users LOG: statement: COMMIT 2018-05-10 19:54:13 MSK [10577-20] pibotuser@users LOG: SSL error: decryption failed or bad record mac 2018-05-10 19:54:13 MSK [10577-21] pibotuser@users LOG: could not receive data from client: Connection reset by peer 2018-05-10 19:54:28 MSK [9088-187] pibotuser@users LOG: statement: BEGIN 2018-05-10 19:54:28 MSK [9088-188] pibotuser@users LOG: statement: select * from users_activity where score=(select max(score) from users_activity); 2018-05-10 19:54:28 MSK [9088-189] pibotuser@users LOG: statement: ROLLBACK 2018-05-10 19:54:28 MSK [9088-190] pibotuser@users LOG: statement: BEGIN 2018-05-10 19:54:28 MSK [9088-191] pibotuser@users LOG: statement: select * from users_activity where sub_date=(select max(sub_date) from users_activity); 2018-05-10 19:54:28 MSK [9088-192] pibotuser@users LOG: statement: ROLLBACK 2018-05-10 19:55:29 MSK [9087-200] pibotuser@users LOG: statement: BEGIN 2018-05-10 19:55:29 MSK [9087-201] pibotuser@users LOG: statement: select * from users_activity where score=(select max(score) from users_activity); 2018-05-10 19:55:29 MSK [9087-202] pibotuser@users LOG: statement: ROLLBACK 2018-05-10 19:55:30 MSK [9087-203] pibotuser@users LOG: statement: BEGIN 2018-05-10 19:55:30 MSK [9087-204] pibotuser@users LOG: statement: select * from users_activity where sub_date=(select max(sub_date) from users_activity); 2018-05-10 19:55:30 MSK [9087-205] pibotuser@users LOG: statement: ROLLBACK 2018-05-10 19:56:31 MSK [9088-193] pibotuser@users LOG: statement: BEGIN 2018-05-10 19:56:31 MSK [9088-194] pibotuser@users LOG: statement: select * from users_activity where score=(select max(score) from users_activity); 2018-05-10 19:56:31 MSK [9088-195] pibotuser@users LOG: statement: ROLLBACK 2018-05-10 19:56:31 MSK [9088-196] pibotuser@users LOG: statement: BEGIN 2018-05-10 19:56:31 MSK [9088-197] pibotuser@users LOG: statement: select * from users_activity where sub_date=(select max(sub_date) from users_activity); 2018-05-10 19:56:31 MSK [9088-198] pibotuser@users LOG: statement: ROLLBACK 2018-05-10 19:57:32 MSK [9087-206] pibotuser@users LOG: statement: BEGIN 2018-05-10 19:57:32 MSK [9087-207] pibotuser@users LOG: statement: select * from users_activity where score=(select max(score) from users_activity); 2018-05-10 19:57:32 MSK [9087-208] pibotuser@users LOG: statement: ROLLBACK 2018-05-10 19:57:32 MSK [9087-209] pibotuser@users LOG: statement: BEGIN 2018-05-10 19:57:32 MSK [9087-210] pibotuser@users LOG: statement: select * from users_activity where sub_date=(select max(sub_date) from users_activity); 2018-05-10 19:57:32 MSK [9087-211] pibotuser@users LOG: statement: ROLLBACK 2018-05-10 19:58:33 MSK [9088-199] pibotuser@users LOG: statement: BEGIN 2018-05-10 19:58:33 MSK [9088-200] pibotuser@users LOG: statement: select * from users_activity where score=(select max(score) from users_activity); 2018-05-10 19:58:33 MSK [9088-201] pibotuser@users LOG: statement: ROLLBACK 2018-05-10 19:58:34 MSK [9088-202] pibotuser@users LOG: statement: BEGIN 2018-05-10 19:58:34 MSK [9088-203] pibotuser@users LOG: statement: select * from users_activity where sub_date=(select max(sub_date) from users_activity); 2018-05-10 19:58:34 MSK [9088-204] pibotuser@users LOG: statement: ROLLBACK 

UPD2 I ran the following code:

 while True: with session() as ssion: ssion.execute('select * from users') ssion.execute('update users set mode=0') 

He worked on this database for about 5 minutes and no error occurred. It looks like the problem is in the working code, but I do not know how to detect it

0