• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How CodeIgniter close db connections?

#1
I'm wondering how CodeIgniter close db connections. In the doc, it says "CodeIgniter intelligently takes care of closing your database connections". But how does CodeIgniter do this? Seems I didn't find the db driver's "close" method get called in CodeIgniter 3.0.3 source code.

I've also posted in StackOverflow: http://stackoverflow.com/questions/43922...onnections

Thanks!
Reply

#2
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. Which in CodeIgniter happens right after the controller method finishes. So you could call $this->db->close(); in some model or in a controller. But all you would accomplish is freeing up the connection resources a few microseconds earlier than if you let script execution finish.

If you have a high-demand situation and are therefore using persistent connections that is a different story. http://php.net/manual/en/features.persis...ctions.php
Reply

#3
(05-15-2017, 03:44 PM)dave friend Wrote: 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. Which in CodeIgniter happens right after the controller method finishes. So you could call $this->db->close(); in some model or in a controller. But all you would accomplish is freeing up the connection resources a few microseconds earlier than if you let script execution finish.

If you have a high-demand situation and are therefore using persistent connections that is a different story. http://php.net/manual/en/features.persis...ctions.php

My application structure: 
  • CI 3.1.4 with HMVC - All controllers/models/views are in modules
  • "pconnect" is set to FALSE
  • "mysql.allow_persistent = Off" is added to php.ini
  • Module A is called for loading a page (an extensive form)
  • Module A then calls Modules B to M for loading various (reusable) sections of the page (the form)
  • Modules B to M may initiate connections to databases if needed (queries are not overly complex, though have a few cross-database joins)
Working perfectly on localhost. Gives following error on live server (shared hosting).

Quote:Message:  mysqli::real_connect(): (42000/1203): User <user> already has more than 'max_user_connections' active connections
Filename:mysqli/mysqli_driver.php
Line Number: 201


Now, will CI close database connections after finishing execution of Module A script or after finishing individual execution of Modules B to M scripts (which are actually creating database connections). Logically (and "intelligently"), it should be the latter.

The connections in cross-database joins within CI are not clear. How are they opened, managed, and closed? Can those connections cause "max_user_connections" error in MySQL on live server where allowed limit is 10-15?

Any help is appreciated. Thanks.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.