Running out of DB connections!

前端 未结 5 1690
误落风尘
误落风尘 2021-02-03 11:52

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

相关标签:
5条回答
  • 2021-02-03 12:21

    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.

    0 讨论(0)
  • 2021-02-03 12:30

    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.

    0 讨论(0)
  • 2021-02-03 12:33

    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

    0 讨论(0)
  • 2021-02-03 12:34

    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).

    0 讨论(0)
  • 2021-02-03 12:41

    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).

    0 讨论(0)
提交回复
热议问题