"be consistent" | |
PerlMonks |
Handle DBI connection lostby bash (Scribe) |
on Feb 22, 2017 at 19:17 UTC ( [id://1182551]=perlquestion: print w/replies, xml ) | Need Help?? |
bash has asked for the wisdom of the Perl Monks concerning the following question:
Hello, I have script that is connected to PostgreSQL and run 24x7. Some times we need to restart PostgreSQL or some connection problem can occur, so script is needed to reconnect on this situations. There are two ways: 1) check connection before every statement execution 2) check connection on errors I decided to go by way 2, because I don't want to add extra overhead by calling ping() every time we execute statement So my problem is how to be sure that DBI error is about "connection". Here is my prototype based on 'Active' attribute and on state() method. Is it correct way? And how to emulate connection lost for testing (maybe fork and close handler from child)? update 2017-02-23 I emulated connection lost via fork: My observation shows that: $dbh->{Active} == 1 even if dbh return "DBD::Pg::db selectrow_array failed: no connection to the server". $dbh->{Active} == '' only if you explicitly call $dbh->disconnect; But $dbh->state works like expected. Still interesting in any production experience with robust handling for connection lost. PS. To be sure that ping is really slow down I performed simple test
Back to
Seekers of Perl Wisdom
|
|