Welcome Guest, Not a member yet? Register   Sign In
Why to ALWAYS create a new DB connection (MySQL case)?
#1

[eluser]kadishmal[/eluser]
I don't know what is the statistics of how many users use persistent connection in CI or how many use non-persistent dbconnect(), but I wonder why would user want to create a NEW connection every time dbconnect() is called?

The dbconnect() function has the following code:

Code:
public function db_connect()
{
return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);
}

TRUE parameter at the end forces the database driver to establish a new connection even if the parameters are the same with already opened connection.

I can think of one situation when a user would actually want to establish a new connection. It is when user wants to execute certain operations in parallel using asynchronous queries. Other than that.... user would probably want to reuse the open connection.

I tried to look the entire history of changes for mysql_driver.php file, but didn't have patience further than the 3rd page. Would anyone please explain me the background of "forcing a new connection"?

The reason I am asking is because I am updating the CUBRID Database driver for CI and I wonder why there is such a code.

Thank you in advance.
#2

[eluser]InsiteFX[/eluser]
Maybe because they set a timeout limit for mysql to time out on the servers!

Even your localhost mysql has a timeout limit set to close the database.

Also if you check the mysql website they will tell you not to use persistent connections because they are EVIL!
#3

[eluser]kadishmal[/eluser]
[quote author="InsiteFX" date="1329116472"]Maybe because they set a timeout limit for mysql to time out on the servers!

Even your localhost mysql has a timeout limit set to close the database.
[/quote]

That doesn't matter. If connection is closed, the API will reconnect. If that TRUE wasn't passed, the API checks if the connection with identical params is active. If so, return existing connection, otherwise create a new. So, it doesn't matter if the connection was closed as a result of timeout or manually, or due to network failure.

That's why I don't understand why it's forced to create a new connection.

Regarding persistency being "EVIL", I have no comments. That's why pconnection exists and developers need to use it understanding all consequences.




Theme © iAndrew 2016 - Forum software by © MyBB