Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8465343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:01:04+00:00 2026-06-10T15:01:04+00:00

I have daemon script which runs forever in while loop. I have a prepared

  • 0

I have daemon script which runs forever in while loop. I have a prepared statement and this statement is executed on every loop.

Example:

  my $dbh;
  sub get_dbh {
      return DBI->connect(...);
  }

  my $dbh = get_dbh();
  my $sth = $dbh->prepare("SELECT ....") or die $DBI::errstr;

  while (1) {
      // is connection still there??
      //if (!$dbh->ping) {
      //    $dbh = get_dbh();
      //}

      $sth->execute('param1');

      // do some other things ... sleep between 0 and 3600
  }

Problem occurs (or might occur) if prepared statement is prepared a few hours ago. Connection could die and my execute too. Checking $dbh->ping before every execute looks like an overkill.

MySQL supports mysql_auto_reconnect which works really. DBD::Pg doesn’t have something like that. I read about DBI::Apache but as I see it depends on mod_perl etc. It is obviously meant for web applications.

Is there a “best-practice” way to do check for connection state and reconnect if needed?#

I could prepare statement on every loop but that is not the solution but just a way around the problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:01:05+00:00Added an answer on June 10, 2026 at 3:01 pm

    Is there a “best-practice” way to do check for connection state and reconnect if needed?#

    Yes, at least in my view, because there’s only one approach that’s free of race conditions, and that’s to execute the query in a retry loop that handles errors if they arise.

    Otherwise you still have:

    1. PREPARE
    2. SELECT 1; or whatever your test statement is
    3. Network drops out, backend crashes, admin restarts server, whatever
    4. EXECUTE
    5. splat.

    Correct behaviour requires something like the pseudocode:

    while not succeeded:
        try:
            execute_statement()
            succeeded = True
        except some_database_exception:
            if transaction_is_valid():
                // a `SELECT 1` or `select 1 from pg_prepared_statements where name = 'blah'
                // succeeded in transaction_is_valid(), so the issue was probably
                // transient. Retry, possibly with a retry counter that resets the 
                // connection if more than a certain number of retries.
                // It can also be useful to examine the exception or error state to 
                // see if the error is recoverable so you don't do things like retry
                // repeatedly for a transaction that's in the error state.
            else if test_connection_usable_after_rollback():
                // Connection is OK but transaction is invalid. You might determine
                // this from the exception state or by seeing if sending a `ROLLBACK`
                // succeeds. In this case you don't have to re-prepare, just open
                // a new transaction. This case is not needed if you're using autocommit.
            else:
                // If you tried a SELECT 1; and a ROLLBACK and neither succeeded, or
                // the exception state suggests the connection is dead. Re-establish
                // it, re-prepare, and restart the last transaction from the beginning.
                reset_connection_and_re_prepare()
    

    Verbose and annoying? Yep, but usually easily wrapped in a helper or library. Everything else is still subject to races.

    Most importantly, if your application is issuing transactions where it does more than one thing, it needs to remember everything it did until the transaction commits, and be able to retry the whole transaction if there’s an error. That, or tell the user “oops, I ate your data, please re-enter it and try again”.

    If you don’t mind the races and just want to handle any obviously dead connections with a periodic check, just store the time of the last query in a variable. When issuing queries check if the timestamp is more than a few minutes old and if it is, issue a SELECT 1; or a query against pg_prepared_statements to check for your prepared statement. You’ll either need to be prepared to barf errors at the user or wrap the whole thing in proper error handling anyway … in which case there’s no point bothering with the time check and test at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Perl script which runs as a Linux daemon using an
I have a PHP script running as a daemon. Every once in a while,
I have a python script which manages an Erlang daemon. Everything works fine when
I have this Python based service daemon which is doing a lot of multiplexed
If I have a ruby script Daemon that, as it's name implies, runs as
I have a daemon script written in ruby which responds to commands like daemon
I have a little script to run a search daemon, like: run.sh : cd
I have been trying to make an init script using start-stop-daemon. I am stuck
the procedure: i have a script which uploads images (via hmtl-form and php) for
I have to write test for deployment script which uploads files through SSH, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.