简体   繁体   中英

C3P0 preferredTestQuery configuration

I am using c3p0 0.9.1.2 config c3p0 ComboPooledDataSource. Everything seem ok, but preferredTestQuery takes a lot of time and uses high CPU on DB server.

This is my configuration:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass"><value>${server.database.driver}</value></property>
        <property name="jdbcUrl"><value>${server.database.url}</value></property>
        <property name="user"><value>${server.database.user}</value></property>
        <property name="password"><value>${server.database.password}</value></property>
        <property name="initialPoolSize"><value>2</value></property>
        <property name="minPoolSize"><value>2</value></property>
        <property name="maxPoolSize"><value>10</value></property>
        <property name="idleConnectionTestPeriod"><value>600</value></property>
        <property name="maxIdleTime"><value>0</value></property>
        <property name="preferredTestQuery"><value>SELECT 1 FROM DUAL</value></property>
        <property name="testConnectionOnCheckin"><value>true</value></property>
    </bean>

During the day SELECT 1 FROM DUAL runs about 1 million times and takes about 30% of CPU.

So, can I decrease the total number of preferredTestQuery executions a day? Or shall I use another query to test?

Could you please advise me on a better configuration of c3p0?

First :

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" >
        <property name="driverClass" value="${server.database.driver}" />
        <property name="jdbcUrl" value="${server.database.url}" />
        <property name="user" value="${server.database.user}" />
        <property name="password" value="${server.database.password}" />
        <property name="initialPoolSize" value="2" />
        <property name="minPoolSize" value="2" />
        <property name="maxPoolSize" value="10" />
        <property name="idleConnectionTestPeriod" value="600" />
        <property name="maxIdleTime" value="0" />
        <property name="preferredTestQuery" value="SELECT 1 FROM DUAL" />
        <property name="testConnectionOnCheckin" value="true" />
    </bean>

Are there a million Connections checked in per day, and do the Connections do very little on checkout? That's the most straightforward explanation for what you are seeing, lots of Connections checked out and then checked in without doing any meaningful work.

Still, what you describe is weird. The whole point of "SELECT 1 FROM DUAL" is that it's supposed to be a very easy and efficient query to resolve. Is it an Oracle DB, the backend for which that query makes sense? (For several other DBMSs, just "SELECT 1" is fine.) I'd make sure "SELECT 1 FROM DUAL" is the appropriate query for your back end, that DUAL is actually the built-in dummy table it is supposed to be. If the query is doing real work that's a problem.

An easy thing to do, if you can, would be to upgrade from the very old 0.9.1.x series to a more recent version. The 0.9.5.x series uses the Connection 's native isValid() method to test (if no preferredTestQuery is set, which usually it should not be with 0.9.5+ and a JDBC4+ driver). Hopefully your JDBC driver does a good job of selecting an efficient test query.

There's some advice about Connection testing setup in c3p0's docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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