I\'m running a Spring/Hibernate connecting to MySQL setup using c3p0 as my connection pool. For some bizarre reason it runs out of connections when the system is under load (of
I also had this problem. The cause was that the user does not has the grants for the host, because the /etc/hosts entry has been modified.
It's pretty unlikely that @Transactional
leaks connections - otherwise, your site would stop working after the first 100 requests.
But there is another reason why this happens:
Maybe you have set a timeout for "dead" connections and some queries take longer than that. That means that your pool removed a busy connection as "dead" from the pool and requests another from the DB - until the DB pulls the plug.
To debug this, enable logging for your connection pool, so you can see when it requests new connections.
I also had this problem and I solved it by setting the property checkoutTimeout of C3P0 to 0 instead of a value higher than 0.
In fact I had lots of threads waiting for a connection and after 10s, the same erros as yours occured.
See the doc here : http://www.mchange.com/projects/c3p0/#checkoutTimeout
Try enabling logging and setting the c3p0.debugUnreturnedConnectionStackTraces
property to true. Also set c3p0.unreturnedConnectionTimeout
to something smaller than your average query time (1 sec?). Then any thing that takes longer than the timeout will log a stack trace. This should allow you to narrow down things pretty quickly.
If there's no pattern to the stack traces, it could just be that your pool is too small. You said 100 concurrent users, but any idea how many queries per second this is? If it's 100 queries per second and you have 20 connections, then each sql execution needs to take less than 200 ms (20 connections => 20 total seconds of work per sec of wall clock time to do 100 queries).
Regardless of the configuration you have for C3P0 (through hibernate) you might have a restriction imposed by MySQL itself. Keep in mind that by default the maximum number of connections allowed by MySQL is 100! So even if you tell C3P0 to pool up to 200, 500 or 1000 connections, this will be unachievable. Open a MySQL shell using:
$ msql -u [user] -p
And type the following to get the maximum number of connections allowed:
$ show variables where Variable_name='max_connections';
If the number returned is too low for your application, consider changing it (edit your my.cnf file, usually located inside /etc/mysql/ on Linux systems).