Best configuration of c3p0 [closed]

2019-01-21 10:34发布

问题:

I'm struggling with a problem facing c3p0 configuration. I posted a question last week- C3P0 Configurations! Where and How?, but no answer received. Any help is appreciated.

回答1:

This is a configuration I am using which keeps resources to a minimum. Of course you'll want to tailor your application to use the resources it needs...

Reference: http://www.mchange.com/projects/c3p0/index.html

  • testConnectionOnCheckin validates the connection when it is returned to the pool. testConnectionOnCheckOut, although would ensure active connections before use, would be too expensive to do.
  • idleConnectionTestPeriod sets a limit to how long a connection will stay idle before testing it. Without preferredTestQuery, the default is DatabaseMetaData.getTables() - which is database agnostic, and although a relatively expensive call, is probably fine for a relatively small database. If you're paranoid about performance use a query specific to your database (i.e. preferredTestQuery="SELECT 1")
  • maxIdleTimeExcessConnections will bring back the connectionCount back down to minPoolSize after a spike in activity.

Below configuration sets poolsize between 3-20. Idle connections are retested every 5 minutes to keep them active. Because of idleConnectionTestPeriod, this will only keep the the minumum number of connections alive. If there are more than 3 connections at the 4-minute mark, it kills those connections freeing resources back to the minimum.

Use of maxIdleTimeExcessConnections and idleConnectionTestPeriod negates the need for maxIdleTime

<Context docBase="myapp" path="/myapp" reloadable="true">
    <Resource description="My DB Datasource" name="jdbc/mydb"
        auth="Container" factory="org.apache.naming.factory.BeanFactory"
        type="com.mchange.v2.c3p0.ComboPooledDataSource" 
        user="myuser" password="******"
        minPoolSize="3"
        maxPoolSize="20"
        acquireIncrement="1" 
        driverClass="com.mysql.jdbc.Driver" 
        jdbcUrl="jdbc:mysql://localhost:3306/mydb"
        testConnectionOnCheckin="true" 
        idleConnectionTestPeriod="300"
        maxIdleTimeExcessConnections="240"
    />
</Context>


回答2:

Best configuration is to setup JPA to use the container environment to get DataSource.

This allows the container to provide the connection pooling rather than configuration it directly into your JPA project.

You do not indicate which container enviroment you are working with. I almost always use c3p0 with Standalone applications, such as Java SE desktop or server applications. I also use it with Spring framework.

When using a container such as Servlet (Tomcat/Jetty) or Application Server (such as Glashfish or JBoss AS) these already provide a DataSource connection pooler implementation that is usually not c3p0 but provides equivalent function.

I have never tried to configure a connection pooler inside a JPA project because this means editing configuration to customize it for every environment it needs to run in. This is a headache so it best that JPA part be given the DataSource to use during bootstrap.