When is it the best time to close a database connect? Is it good to close it on every scripting page i do that uses it?

Dani AI

Generated

Short answer: for normal, short web requests you usually do not need to explicitly close a non‑persistent DB connection — PHP will drop it when the request ends — so is essentially correct. Explicitly closing a connection becomes useful for long‑running scripts, workers/CLI jobs, or when you do expensive post‑query processing and want to free the socket and memory before the script exits. See the PHP docs for connection lifetime and persistent options (PDO connections).

If you want deterministic cleanup inside a request, create the connection in a single place and close it when finished (or let the object go out of scope). Example pattern with PDO:

try {
    $pdo = new PDO($dsn, $user, $pass, $options);
    // perform queries
} finally {
    $pdo = null; // ensures the connection is released now
}

’s singleton idea centralizes creation/close, but beware: singletons hide dependencies and make testing harder. Prefer passing the connection into functions/classes (dependency injection) or a simple connection factory/service you can mock.

About persistent connections: they can reduce connect overhead, but they also tie connections to worker processes and can exhaust server-side connection pools if used without measurement. Use them only after measuring benefit; PDO supports persistent connections (see PDO connections).

For the “Out of memory”/recursion issue mentioned by , infinite or very deep recursion is a common cause. Debug steps: log stack depth with count(debug_backtrace()), check memory_get_usage() during runs, run with Xdebug to get a stack trace, and add a recursion guard or convert recursion to an iterative approach. Relevant references: debug_backtrace and memory_get_usage.

Recommended Answers

All 7 Replies

When a PHP script is done executing all non-persistent connections will be closed.
Non-persistent connections are made with the _connect database functions so as long as you stick to that, there's nothing to worry about.

okay thanks!...

One more question what is a recursion? And how do i fix those? My website use to read "Out of memory on line 12"?

To read more about fatal error:"OUT OF MEMORY" ,Clickhere

Error is based on coding and logic.
You have to increase your memory limit.
ini_set(’memory_limit’, '20M');

But if your script is in end level recursion, above solution wont work.
Make sure your recursive function has one limit and break somewhere.

okay, so the only thing I have to do is fix the PHP ini file to accept more memory?

When is it the best time to close a database connect? Is it good to close it on every scripting page i do that uses it?

I use a database singleton design pattern. That makes easy with single close throughout the session!

okay thank you! I will read up on that. ^_^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.