Home > Enterprise >  Is freeing result sets necessary for mysqli persistent connections?
Is freeing result sets necessary for mysqli persistent connections?

Time:04-02

According to mysqli document,

Open non-persistent MySQL connections and result sets are automatically closed when their objects are destroyed. Explicitly closing open connections and freeing result sets is optional.

But is it also optional for mysqli persistent connections?

CodePudding user response:

Yes, it is optional. A persistent connection is a connection that is not closed when the PHP process ends. When a new HTTP request is processed by PHP, it will reuse the same connection. Thus, you don't need to close the connection. Calling mysqli_close() has no effect on persistent connections.

As for freeing the result sets, mysqli will clean up any pending result sets regardless of the mode, when the script ends. You never need to free the results manually. The only reason to free result sets is when you have remaining unbuffered rows that you want to fetch and discard, and then execute another operation within the same script. This is the only time that using mysqli_free_result() is justified.

Mysqli will also clean up the connection by default (it can be disabled).


More importantly, persistent connections are often a nightmare to work with and if possible avoid it at all cost. The performance impact of connection is usually so tiny that most apps can live with opening a new connection for every request.

  • Related