WebwareForPython / DBUtils

Database connections for multi-threaded environments
MIT License
335 stars 52 forks source link

can not reconnect to database when restart postgres service #22

Closed wukongGuoGuo closed 4 years ago

wukongGuoGuo commented 5 years ago

Issue/Concerns: OS:win10 python:3.6.5 postgres:9.5.15 dbutils: 1.3

set the max_connection_num to postgres to 5 and used pg_terminate_backend function of postgres in windows to kill 2 of the connections, the connection pool will not recreate 2 new connection for the pool and when restart postgres service all connection will be invalid in program, and will not reconnect. connection creation code is : PooledDB(psycopg2, maxconnections = 5, mincached = 5, maxcached = 5, maxshared = 5, application_name = configs.application_name_for_database_connection, host = configs.database_host, port=configs.database_port, dbname = configs.database_name, user = configs.database_user_name, password=configs.database_password)

Cito commented 5 years ago

Just tried this with Win 10, Py 3.7, Pg 10, DbUtils 1.3. I have created a pool with the same parameters, and ran a loop executing a query every second, printing the result. When I kill sessions using PgAdmin 4, I can see that they are recreated and my loop continues to run and print results without problems.

Maybe you have the wrong expectations? The connections are not automatically recreated, but only if you do something with them, like execute a query. In this case, DBUtils detects that the connection taken from the pool is dead, catches the error, recreates the connection, uses that one instead, gives you back the result, and puts the newly created connection back into the pool instead of the dead one.

If you think there is still a problem, please submit a simple test program for me to reproduce what you are doing.

Cito commented 5 years ago

Only noticed now that you not only kill the connections, but also restart the whole Postgres service. In that case you may see errors when the program is running while the server is restarted, because in that short time, no new connection can be created. But after the restart, the program should continue to run without problems again, and the connections in the pool should be replaced. Tried that as well.

wukongGuoGuo commented 5 years ago

thanks your reply Christoph ,sorry for my misunderstand,I only used 2 connections in the pool, when decrease the max connection number to 2, then kill the connection, it will recreate connection.But restart postgres service scene, It will not, I'll try with python 3.7 and Pg10