0

How can I explicitly close a MySql Connection in Drupal 7? I have a requirement of inserting back to rows to a DB table in Drupal.. I tried using db_insert/db_update and db_query.. In both cases I got the error Max number of SQL connections reached.. I am speculating that Drupal is maintaining these connections one per query and not closing them.. Hence I need to know if there is any way I can do it explicitly.

  • Most webservers reuse connections, and they are closed or freed for reuse at the end of PHP script anyway, so either 1) you have simply not enough of them to serve your traffic and explicit close is not likely to help or 2) your server is seriously misconfigured and explicit close is not likely to help. Also, if you close connection before Drupal ends it's natural flow, you may encounter errors, like Drupal failing unable to write to watchdog about some minor issue. So don't do it. Not until you really, really well know how DB access works in Drupal. – Mołot May 5 '14 at 7:02
2

The specific function you are looking for in the database abstraction API is db_close() but you may be able to solve your problem through another route by increasing your max connections.

See this earlier thread on drupal.stackexchange for guidance on increasing max_connections. You might also be having a problem with slow / stalled queries that are trying up limited query slots, this thread offers advice on how to debug.

  • I am running just 1 script with multiple db operations i dont think incresing DB connections should be an advisable solution as this is just being called for a single user.. – mjs Sep 9 '13 at 5:56
  • I will try db_close and see if that helps .. Thanks – mjs Sep 9 '13 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.