Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter Multiple Databases Persistent Connection?

I'm using multiple database in my CodeIgniter application and have been reading a lot that persistent connections should be turned off.

Why is this measure recommended and is this still necessary in the newest version, 2.0.2?

I'm doing things like

$db2 = $this->load->database("dbname", TRUE);
like image 854
user599146 Avatar asked Apr 20 '11 06:04

user599146


1 Answers

Code Igniter Documentation Doesn't Explain

Unfortunately, the Code Igniter documentation for 2.0.2 does not explain why they should be turned off. It merely explains that there is a setting to do so. The reasoning for this is most likely because it really isn't a Code Igniter functionality but more of an underlying PHP/MySQL functionality. PHP has a very good and detailed page in their documentation about persistent connections.

The Explanation From a Server Admin (me)

Basically, it comes down to performance. If you have a small number of users that are located in California and your MySQL database server is in Switzerland, there will be a substantial overhead to make a connection to the MySQL server (when compared to a connection to MySQL on the same server in California). Having a persistent connection will prevent the overhead of re-connecting every time you want to look something up with MySQL. There's also many other issues that can cause connection overhead, such as the way the server or database is configured. Besides performance, it can also cause issues if a connection closes improperly, leading to locked tables.

Why It's Recommended to be Turned Off

The reason this setting is not ideal for most servers is that if you have more than 60 users using your database simultaneously, or what ever your MySQL configuration has set as the maximum number of connections, you will quickly reach the maximum number of connections and this could cause database errors or server crashes etc.

like image 74
Alex W Avatar answered Sep 20 '22 00:09

Alex W