简体   繁体   English

c3p0 getConnection随着连接数的增加而挂起

[英]c3p0 getConnection Hangs as Number of Connections are Increased

i"m running a web service on Heroku and using New Relic to monitor its performance. I'm using MySQL with Hibernate on top. My non default c3p0 settings are the following 我在Heroku上运行Web服务,并使用New Relic监视其性能。我在顶部使用Hibernate的MySQL。我的非默认c3p0设置如下

hibernate.c3p0.maxStatementsPerConnection, 5
hibernate.c3p0.maxPoolSize, 35
hibernate.c3p0.minPoolSize, 5
hibernate.c3p0.initialPoolSize, 10
hibernate.c3p0.acquireIncrement, 10

Every single request to my web service hits the database at least a couple of times. 对我的Web服务的每个单个请求都会至少两次访问数据库。 After running a load test of about 200 requests/minute for 10min I see most of time is spent in 在以每分钟200个请求/分钟的速度运行了10分钟的负载测试后,我发现大部分时间都花在了

com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection

My guess it's waiting for a connection in the connection pool? 我猜它正在等待连接池中的连接? The interesting part is as I increased 有趣的是随着我的增加

hibernate.c3p0.maxPoolSize, 40

the performance was worse off (longer wait time in the same getConnection call. During the test I can see that the max number of c3p0 connections is indeed open at the MySQL server (max connection set on MySQL's end is 300 , definitely not exhausted). 性能变差了(在相同的getConnection调用中等待时间更长。在测试中,我看到c3p0连接的最大数目确实在MySQL服务器上打开(在MySQL端设置的最大连接数为300 ,绝对没有用尽)。

All of my database functions use the same format 我所有的数据库函数都使用相同的格式

public void executeTransaction( Session session, IGenericQuery<T> query, T entity )
{
    Transaction tx = null;

    try
    {
        tx = session.beginTransaction();

        query.execute( session, entity );

        tx.commit();
    }
    catch ( RuntimeException e )
    {
        try
        {
            tx.rollback();
        }
        catch ( RuntimeException e2 )
        {
        }

        throw e;
    }
    finally
    {
        if ( session != null )
        {
            session.close();
        }
    }
}

so I'm certain all sessions are closed, which should translate into connections closing. 因此,我确定所有会话均已关闭,这应转化为连接关闭。 Why is the wait time more as I increase the max number of connections? 为什么增加最大连接数会增加等待时间? It seems like performance increases from hibernate.c3p0.maxPoolSize, 25 to hibernate.c3p0.maxPoolSize, 30 , but drops after hibernate.c3p0.maxPoolSize, 35 . 似乎性能从hibernate.c3p0.maxPoolSize, 25hibernate.c3p0.maxPoolSize, 30增加到hibernate.c3p0.maxPoolSize, 30 ,但是在hibernate.c3p0.maxPoolSize, 35之后下降。 Are my values far off? 我的价值观远吗?

Thanks! 谢谢!

as a guess, i would try increasing numHelperThreads . 作为一个猜测,我会尝试增加numHelperThreads you have a heavy load; 你负担很重; maybe c3p0's administrative Threads are getting backed up. 也许c3p0的管理线程正在备份。 (You should be able to see this if you dump stack traces or use JMX to monitor c3p0. If you have enough helper threads, they should generally be idle(), wait()ing. If they are getting backed up, you'll see them mostly active and runnable, and by JMX you'll see tasks queued.) (如果您转储堆栈跟踪记录或使用JMX监视c3p0,您应该能够看到此消息。如果您有足够的帮助程序线程,则它们通常应该处于idle(),wait()ing。如果要备份它们,则将看到它们大部分处于活动状态且可运行,并且通过JMX,您将看到排队的任务。)

an insufficiency of helper threads is consistent with your observed better-then-worse performance with maxPoolSize. 辅助线程的不足与您通过maxPoolSize观察到的更好然后更差的性能一致。 initially you get what you want, more Connections at the ready, but then the helper Threads fail to keep up and adding more Connections just makes things worse. 最初,您会获得所需的东西,可以准备更多的连接,但是之后辅助线程无法跟上,添加更多的连接只会使情况变得更糟。

given your settings, helper Threads shouldn't have too much work to do, UNLESS maxStatementsPerConnection is too small. 给定您的设置,辅助线程应该没有太多工作要做,除非maxStatementsPerConnection太小。 if your app has more than 5 PreparedStatements that are run frequently, then you will end up churning through Statements and tying up helper Threads with Statement close() tasks. 如果您的应用程序具有5个以上经常运行的PreparedStatement,那么您最终将遍历Statements并使用Statement close()任务捆绑帮助程序线程。 you might try making this value larger. 您可以尝试将该值增大。 it should be approximately (rounding up) the number of distinct PreparedStatements used on an ongoing basis by your application. 它应该大约(四舍五入)应用程序持续使用的不同PreparedStatement的数量。 (You can ignore single or very rarely used PreparedStatements, involved for example in setup or cleanup.) again, monitoring what helper threads are up to would give you information about whether this is the issue. (您可以忽略单个或很少使用的PreparedStatement,例如,涉及设置或清除。)再次,监视要使用的帮助程序线程将为您提供有关此问题的信息。 (you'd see backed-up Statement close() tasks.) (您会看到备份的Statement close()任务。)

so, things to try: increase numHelperThreads, increase maxStatementsPerConnection (or set it to zero, to turn off Statement caching entirely.) 因此,可以尝试:增加numHelperThreads,增加maxStatementsPerConnection (或将其设置为零,以完全关闭语句缓存)。

good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM