CodeIgniter database connections not being closed

ArthurGuy picture ArthurGuy · Aug 24, 2011 · Viewed 35.5k times · Source

I have built a social community website in CodeIgniter which is now getting a fair bit of traffic, the hosting company have started complaining and saying that the database is receiving null connections as well as connections which aren't being closed.

I am not entirely sure what a null query is or how one would end up being issued, any ideas?

I have added in additional code to force close connections when the code reaches an end but apparently this isn't working.

Can anyone offer any suggestions as to where to look or start debugging an issue like this?

Thanks

I have the following at the bottom of my core MY_Controller

public function __destruct() {
    $this->db->close();
}

Answer

Wallysson Nunes picture Wallysson Nunes · Sep 8, 2011

I think that you're gettting problemns with the initial configurations from Codeigniter connection database.

At this page, you can see each value from config array:
https://www.codeigniter.com/user_guide/database/configuration.html

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "database_name";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
$db['default']['swap_pre'] = "";
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

This it's an basic array config database, try to set the variable of pconnect to FALSE, when it is turned on, the system doesn't close any connection and it stay opened to news queries at any time.

If you set it to false like i said, your system will continue working perfectly, but the codeigniter will close and open the connection when he need's to use the database.

Here, you can find and post inside codeigniter forum with a guy that's have a problem with pconnect variables, may help you! http://codeigniter.com/forums/viewthread/177573/#842016