Java文件如下: Connection conn = null;
Statement stm = null;
ResultSet rst = null; public void exec() {
ComboPooledDataSource cpds = new ComboPooledDataSource();
try {
conn = cpds.getConnection();
stm = conn.createStatement();
rst = stm.executeQuery("select count(1) from help_keyword");
if (rst.next()) {
System.out.println(rst.getInt(1));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rst.close();
stm.close();
conn.close();
} catch (Exception e) {
System.out.println(e);
}
}
} public static void main(String[] args) {
UserC3POPerformance ucp = new UserC3POPerformance();
long start=System.currentTimeMillis();
for (int i = 0; i < 20; i++) {
ucp.exec();
}
System.out.println("共消耗: "+(System.currentTimeMillis()-start)+"ms");
}
配置文件如下: <default-config> <!--用户名。Default: null-->
<property name="user">root</property> <!--密码。Default: null-->
<property name="password">jason</property> <!--DRIVER。Default: null-->
<property name="driverClass">com.mysql.jdbc.Driver</property> <!--URL。Default: null-->
<property name="jdbcUrl">
jdbc:mysql://localhost:3306/mysql
</property> <property name="minPoolSize">10</property> <property name="idleConnectionTestPeriod">60</property>
  <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement">5</property>

<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize">20</property>

<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime">60</property>

<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize">25</property>
  
<!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 
  属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。 
  如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->
  
<property name="maxStatements">20</property>

<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection">0</property>
</default-config>为什么大概打印6次后就开始报错.
错误如下:

401
401
401
401
401
401
java.sql.SQLException: Connections could not be acquired from the underlying database!
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
at org.mavyet.compare.UserC3POPerformance.exec(UserC3POPerformance.java:30)
at org.mavyet.compare.UserC3POPerformance.main(UserC3POPerformance.java:53)
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
... 3 more

解决方案 »

  1.   

    估计你的配置参数有问题,把
    <property name="minPoolSize">10</property>        <property name="idleConnectionTestPeriod">60</property>
                     <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
            <property name="acquireIncrement">5</property>
            
            <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
            <property name="initialPoolSize">20</property>
            
            <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
            <property name="maxIdleTime">60</property>
            
            <!--连接池中保留的最大连接数。Default: 15 -->
            <property name="maxPoolSize">25</property>
              
            <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements 
                  属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。 
                  如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->
              
            <property name="maxStatements">20</property>
            
            <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
            <property name="maxStatementsPerConnection">0</property>都注释掉看看
      

  2.   

    注释掉没有报错了.
    我测试的结果为:
      使用c3p0:共消耗1704ms.
      而使用JDBC只消耗:609ms.
    我想知道我哪里错了?
      

  3.   

    你的问题应该是new了多次ComboPooledDataSource(),改一下。
    public static ComboPooledDataSource cpds = new ComboPooledDataSource();public void exec() {
        //ComboPooledDataSource cpds = new ComboPooledDataSource();
        try {
            conn = UserC3POPerformance.cpds.getConnection();
            ....
      

  4.   

    the c3p0 component blindly and repeatedly performs the same basic task, over and over and over:so you need:<property name="hibernate.c3p0.idle_test_period">100</property>
    <property name="hibernate.hibernate.c3p0.acquire_increment">
     3
    </property>if you use open session in view model,the c3p0 component maybe work well.