dbcp2的setup spring bean配置为"PoolableConnectionFactory未链接到池.调用setPool()"

meh*_*n80 0 java apache spring spring-mvc

我需要将dbcp2 java设置代码转换为spring bean

以下代码按预期工作:

protected  void setupDriver(String connectURI, String username, String password) throws ClassNotFoundException, SQLException{

        //
        // First, we'll create a ConnectionFactory that the
        // pool will use to create Connections.
        // We'll use the DriverManagerConnectionFactory,
        // using the connect string passed in the command line
        // arguments.
        //
        ConnectionFactory connectionFactory =
            new DriverManagerConnectionFactory(connectURI, username, password);

        //
        // Next we'll create the PoolableConnectionFactory, which wraps
        // the "real" Connections created by the ConnectionFactory with
        // the classes that implement the pooling functionality.
        //
        poolableConnectionFactory = 
                new PoolableConnectionFactory(connectionFactory, null);
        logger.info("poolableConnectionFactory created");

        //
        // Now we'll need a ObjectPool that serves as the
        // actual pool of connections.
        //
        // We'll use a GenericObjectPool instance, although
        // any ObjectPool implementation will suffice.
        //        
        connectionPool =
                new GenericObjectPool<PoolableConnection>(poolableConnectionFactory,getPoolConfig());
        logger.info("connectionPool created");

        // Set the factory's pool property to the owning pool
        poolableConnectionFactory.setPool(connectionPool);
        logger.info("connectionPool is set to poolableConnectionFactory");

        //
        // Finally, we create the PoolingDriver itself...
        //
        Class.forName("org.apache.commons.dbcp2.PoolingDriver");
        driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
        logger.info("dbcp2 driver is created");

        //
        // ...and register our pool with it.
        //
        driver.registerPool(poolName,connectionPool);
        logger.info("driver is registered");

        //
        // Now, we create the PoolingDriver itself,
        // passing in the object pool we created.
        //
        dataSource = new PoolingDataSource<PoolableConnection>(connectionPool);       
        logger.info("dataSource is created");

        //
        //Finally we create the JdbcTemplate for sql 
        //operations in DbDAO class
        //
        jdbcTemplate = new JdbcTemplate(dataSource);        
        logger.info("jdbcTemplate is setup");

        logger.info("Finally dbcp2 driver setup is completed!");

    }   

    //Pool initial setup values
    private GenericObjectPoolConfig getPoolConfig(){

            logger.info("Let's create the pool config values");
            GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
            poolConfig.setMaxTotal(Integer.parseInt(config.getMaxtotal())); // set number of max connections i.e 25
            poolConfig.setMaxWaitMillis(Long.parseLong(config.getMaxwaitmillis())); //ie. wait for a minute = 60000
            poolConfig.setMaxIdle(Integer.parseInt(config.getMaxidle())); // set max number of idle connections
            /*poolConfig.setTestOnBorrow(true);
            poolConfig.setTestOnReturn(true);*/
            //poolConfig.setTestWhileIdle(true);
            //poolConfig.setTimeBetweenEvictionRunsMillis(10000L);
            //poolConfig.setNumTestsPerEvictionRun(5);
            //poolConfig.setMinEvictableIdleTimeMillis(5000L);

            return poolConfig;
}
Run Code Online (Sandbox Code Playgroud)

这是beans.xml

<!-- ============ Trauma Database Connection Pooling Beans Settings ================== -->  
    <bean id="connectionFactory" class="org.apache.commons.dbcp2.DriverManagerConnectionFactory">
        <constructor-arg index="0"  value="${tir.jdbc.url}" />
        <constructor-arg index="1"  value="${tir.jdbc.username}" />
        <constructor-arg index="2"  value="${tir.jdbc.password}" />
    </bean>
    <!-- Connection Factory -->
    <bean id="poolableConnectionFactory" class="org.apache.commons.dbcp2.PoolableConnectionFactory">
        <constructor-arg index="0" ref="connectionFactory"/>
        <constructor-arg index="1" >  <null/> </constructor-arg>
    </bean>
    <!-- Pool Configs -->
    <bean id="poolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig">
        <property name="maxTotal" value="${pool.maxtotal}"/>
        <property name="maxIdle" value="${pool.maxidle}"/>
        <property name="minIdle" value="${pool.minidle}"/>
        <property name="maxWaitMillis" value="${pool.maxwaitmillis}"/>
    </bean>    
    <!-- Connection Pool -->
    <bean id="connectionPool" class="org.apache.commons.pool2.impl.GenericObjectPool">
        <constructor-arg index="0" ref="poolableConnectionFactory"/>
        <constructor-arg index="1" ref="poolConfig"/>
    </bean>       

    <!-- Datasource gets connection pool -->
    <bean id="dataSource" class="org.apache.commons.dbcp2.PoolingDataSource">
        <constructor-arg ref="connectionPool"/>
    </bean>
    <!-- JdbcTemplate bean gets the datasource -->
    <bean id="jdbcTemplateTIR" class="org.springframework.jdbc.core.JdbcTemplate">  
        <constructor-arg ref="dataSource" />       
    </bean>
    <!-- Finally, we create our Database object bean -->
    <bean id="dbdao" class="edu.uams.dao.impl.DBDAO">
        <property name="jdbcTemplate" ref="jdbcTemplateTIR" />
    </bean>
    <!-- ============= END OF Trauma Database Connection Pooling Settings =========== -->
Run Code Online (Sandbox Code Playgroud)

在这两种情况下我都可以使用jDBCTemplate对象,但它会发出以下警告:
"WARN 0 [main] - org.apache.commons.dbcp2.PoolingDataSource.(PoolingDataSource.java:65)PoolableConnectionFactory未链接到池.调用setPool( )修复配置"

原因很明显:在我的java代码中,我设置了

poolableConnectionFactory.setPool(connectionPool);
Run Code Online (Sandbox Code Playgroud)

如何从bean调用setPool方法?

我怎样才能在我的bean中设置这个java代码?DriverManagerConnectionFactory的构造函数没有得到DriverClassName的任何构造函数?

            // Finally, we create the PoolingDriver itself...
            //
            Class.forName("org.apache.commons.dbcp2.PoolingDriver");
            driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
            logger.info("dbcp2 driver is created");

            //
            // ...and register our pool with it.
            //
            driver.registerPool(poolName,connectionPool);
Run Code Online (Sandbox Code Playgroud)

Jef*_*eff 6

如果你刚刚使用它会不会更容易org.apache.commons.dbcp2.BasicDataSource?根据文档,它为基本连接池提供了"一站式购物"解决方案.

<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp2.BasicDataSource">
    <property name="driverClassName" value="${db.driver}"/>
    <property name="url" value="${db.jdbcurl}"/>
    <property name="username" value="${db.username}"/>
    <property name="password" value="${db.password}"/>
    <property name="initialSize" value="3"/>
</bean> 
Run Code Online (Sandbox Code Playgroud)