Connection pooling options with JDBC: DBCP vs C3P0

前端 未结 16 1888
遥遥无期
遥遥无期 2020-11-22 06:17

What is the best connection pooling library available for Java/JDBC?

I\'m considering the 2 main candidates (free / open-source):

  • Apache DBCP - http:/
16条回答
  •  遇见更好的自我
    2020-11-22 06:52

    Have been using DBCP for a couple of years now in production. It is stable, survives DB server reboot. Just configure it properly. It only requires a handful of parameters to be specified so don't be lazy. Here is a snippet from our system production code which lists parameters that we explicitly set to make it work:

    DriverAdapterCPDS driverAdapterCPDS = new DriverAdapterCPDS();
    driverAdapterCPDS.setUrl(dataSourceProperties.getProperty("url"));
    driverAdapterCPDS.setUser(dataSourceProperties.getProperty("username"));
    driverAdapterCPDS.setPassword(dataSourceProperties.getProperty("password"));
    driverAdapterCPDS.setDriver(dataSourceProperties.getProperty("driverClass"));
    
    driverAdapterCPDS.setMaxActive(Integer.valueOf(dataSourceProperties.getProperty("maxActive")));
    driverAdapterCPDS.setMaxIdle(Integer.valueOf(dataSourceProperties.getProperty("maxIdle")));
    driverAdapterCPDS.setPoolPreparedStatements(Boolean.valueOf(dataSourceProperties.getProperty("poolPreparedStatements")));
    
    SharedPoolDataSource poolDataSource = new SharedPoolDataSource();
    poolDataSource.setConnectionPoolDataSource(driverAdapterCPDS);
    poolDataSource.setMaxWait(Integer.valueOf(dataSourceProperties.getProperty("maxWait")));
    poolDataSource.setDefaultTransactionIsolation(Integer.valueOf(dataSourceProperties.getProperty("defaultTransactionIsolation")));
    poolDataSource.setDefaultReadOnly(Boolean.valueOf(dataSourceProperties.getProperty("defaultReadOnly")));
    poolDataSource.setTestOnBorrow(Boolean.valueOf(dataSourceProperties.getProperty("testOnBorrow")));
    poolDataSource.setValidationQuery("SELECT 0");
    

提交回复
热议问题