Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter database connections not being closed

Tags:

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(); } 
like image 248
ArthurGuy Avatar asked Aug 24 '11 09:08

ArthurGuy


People also ask

How to close db connection in CodeIgniter?

There is a db->close() method. In a PHP environment that is not using persistent connections there is no real need to call it. For MySQL in particular, all open non-persistent MySQL connections and result sets are automatically destroyed when a PHP script finishes its execution.

How do you check database is connected or not in CodeIgniter 4?

I don't know that there is a right way, but one possibility is to use \CodeIgniter\Database\Config::getConnections(), which "Returns an array of all db connections currently made."

Can we use multiple database in CodeIgniter?

If your application is built with the CodeIgniter framework, it's exceptionally simple to use multiple databases. CodeIgniter provides an easy way to put through and utilize numerous databases on the same or distinct server.


2 Answers

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

like image 121
Wallysson Nunes Avatar answered Oct 21 '22 21:10

Wallysson Nunes


Codeigniter should automatically close the database connection but you can implicitly call it with $this->db->close();

See http://codeigniter.com/user_guide/database/connecting.html

like image 43
simnom Avatar answered Oct 21 '22 20:10

simnom