Ouroboros
Ouroboros

Reputation: 1524

Is it safe to use persistent connection using PHP mysqli interface?

There are quite a few blog/links which discourage usage of persistent connections, mainly because the cleanup needs to be done on client side, and cases where transactions/locks have to be correctly rolled back. However, those links are old, and not enirely in context of mysqli PHP interface.

I read the link : The mysqli Extension and Persistent Connections

It clearly suggests that it does most of the desired cleanup when a client terminates unexpectedly:

  • Rollback active transactions
  • Close and drop temporary tables
  • Unlock tables
  • Reset session variables
  • Close prepared statements (always happens with PHP)
  • Close handler
  • Release locks acquired with GET_LOCK()

Now that pretty much does most of the cleanup, including READ/WRITE locks on tables if any acquired. So I believe it should be safe. Can I be wrong?

Also, it says there are some performance penalty in form of extra time needed to do cleanup. I would like to know how much that may be in terms of millisecs? Can it ever be as large as say 100 ms?

The automatic cleanup feature has advantages and disadvantages though. The advantage is that the programmer no longer needs to worry about adding cleanup code, as it is called automatically. However, the disadvantage is that the code could potentially be a little slower, as the code to perform the cleanup needs to run each time a connection is returned from the connection pool.

Upvotes: 0

Views: 181

Answers (1)

Your Common Sense
Your Common Sense

Reputation: 157895

I wonder if you really think you can trust an answer from anonymous passer-by more than official documentation page, that clearly answers your question.

But if you do - yes, you can believe it should be safe.

As of performance penalty, from the way it is asked, I believe you don't really need persistent connections at all.

Upvotes: 1

Related Questions