Jump to content

How can I test to see if a connection is closed?


kjtocool

Recommended Posts

I have a class I use to handle my various DB objects.  I want to close any connections, if needed, in my destructor.  I want to do this even though I already know the connection has been closed previously.  What I am looking for is a way to test the $connection variable I have to see if it's in need of closing.  I can't figure out how to do this.

 

if (isset($this->connection))
{
   	echo "is set";
}
   		
if ($this->connection)
{
   	echo "here";
}

 

The above would output "is set" and "here", because in both tests say the already closed connection is still there.  Can anyone offer any help?

Link to comment
Share on other sites

That thread is the same question, but the user in that thread decided to simply manually close the connection.  Never was it answered HOW you can test if a connection is open.

 

I am attempting to always close the connection in my destruct, but if it was already closed, i'm running into an error.  I understand how PHP cleanup works, but I still want to close it manually in the destruct if possible as there are some benefits.  To do this, I need to know how to test it, and can't figure it out.  I even tried:

 

$this->connection->ping()

 

But that too throws the same error: mysqli::ping() [mysqli.ping]: Couldn't fetch mysqli

Link to comment
Share on other sites

The only way I've found that I can do this, is if I unset the connection variable after I close it, like so:

 

$this->connection->close();

unset($this->connection);

 

That allows me to check isset and correctly know if the object needs closed.  I would prefer not to do that.  I want to do something like:

 

$this->connection->close();

 

if (*some test to see if it's open*)

{

    $this->connection->close();

}

else

{

    // Already closed, don't need to do anything

}

Link to comment
Share on other sites

Its also safe to assume (in most cases at least) a mysql connection will genneraly close its connection when the page load ends, grant it its always good to practice closing a connection manually but from what I have read generally the connection closes at end of load. If I am wrong by all means correct me, Im just going based on what I have been lead to believe through my reading over time.

Link to comment
Share on other sites

You're not wrong (for typical connections).  The garbage collection will clean it up.  There are some performance benefits however to closing it explicitly, you regain the memory quicker and other minor things.  The main reason I'd advocate closing it manually is simply because it's good practice.

 

In this instance, the call to close the connection is actually a redundant one.  It doesn't NEED to be done, it was just infuriating me that I didn't know HOW to do it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.