Can anyone point me in the right direction for keeping a connection to my database opened?

The script I need to write will run 24/7 executing a postgresql stored procedure which inserts records into the database. The problem I think I might have is using a standard procedural syntax of:

Open connection
Loop
    Call stored procedure/function

...if my connection gets interrupted somewhere during the loop any subsequent iterations will obviously fail, so I need a way of reconnecting to the database should the need arise. Basically something like this (I'm just not sure how to actually code this):

my $db_object

sub get_database_object {
    if $db_object exists is functional, return that
    otherwise re-open $db_object and return it
}

sub insert_record {
    my (param1, param2, param3, param4) = @_;
     my $local_db_object = get_database_object();

    # ... code here that utilizes $local_db_object, which should always
    # be functional due to checks performed in get_database_object()
}

Loop
    insert_record("value 1", "value 2", "value 3", "value 4");
End Loop


The main question is, how can I efficiently determine if a db object is usable?

Thanks,
Brandon

Reply via email to