After adding "recycle" support for #224, I also changed the semantics of invalidate() so that a connection-holding _ConnectionRecord can invalidate and reopen a new connection in place, without the Pool having anything to do with it. Identifying an actual "closed" situation is very tough, and the ones in your patch didnt really work. it seems like one place to really decide that a connection is "bad" is when you call a cursor off of it; so i have added logic to the underlying call to cursor to invalidate the connection. if this works it would be great since its independent of all db's and error messages. it was also the only thing that I could actually get to work while testing with stopping and starting a postgres database.
you might want to try it out....this is very hard to test. when the database is stopped, youll definitely get a lot of nasty errors and stack traces (because the execute call fails, then hits all the auto-rollback stuff which also fails, and its hard to see what actually went wrong); but the main idea is that when the db starts again, calling pool.connect() should immediately resume giving you fresh new connections, and an application would not have to be restarted. reopen the ticket if it totally doesnt work.