Is db_query() closing connections? I recently build a web site with a small-medium profile of user access (around 150.000 pageviews/month) and sometimes, in the middle of the day I get the following error:


  PDOException: SQLSTATE[08004] [1040] Too many connections in lock_may_be_available()


The Drupal application is hosted with the MySQL on the same server, with an 2 CPUs Intel Xeon Quad Core and 8 GB of RAM. 

I'm new on the Drupal world, and someone recomended me to use db_query() to throw my queries to the database. I don't know if this is the best pratice or even if this is the root of the problem. Can anyone help me?
You need to make sure the number of MySQL connections (max_connections in my.cnf) is greater than the number of PHP processes (pm.max_children if you use php-fpm, or MaxClients if you're using Apache mod_php).

The default for Apache is often 300, and the MySQL default is only 100, so you can run out of connections pretty quickly if these values aren't tuned.