2

I have a code similar to:

$conn = pg_connect($string_connection );

while(true ){
     check if $conn is alive 

     $result= pg_query ($conn,"SELECT * FROM foo");

     do something 

    sleep(60);
}

How can I check if $conn is alive?

2 Answers 2

3

int pg_connection_status($conn) seems like a good indicator.

bool pg_connection_busy($conn) may be interesting as well.

Sign up to request clarification or add additional context in comments.

Comments

3

@cobra_fast has answered the question literally, so that answer should be marked correct. However...

You should generally not check if the connection is valid. Except in the case of connection pooler idle checking it's almost always wrong to do so.

Simply use the connection. If something goes wrong, handle the resulting error/exception by closing the connection, opening a new one, and retrying at the beginning of the transaction.

Attempts to "test" the connection first are a waste of time and effort because the connection can break between when you test it and use it, or while you are actively using it. This means you have to handle those error cases anyway... and if you're going to handle them, there's no point bothering to test the connection first.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.