简体   繁体   English

在DBCP中启用PreparedStatement池

[英]Enabling PreparedStatement pooling in DBCP

I want to know how to set dbcp to use pool PreparedStatements. 我想知道如何设置dbcp以使用池PreparedStatements。 I seem to have connection pooling working but can't find much in the way of examples for prepared statements. 我似乎有连接池工作,但在准备语句的示例方式中找不到多少。 There's a related question which goes into using the PreparedStatement pool but skips over the details of getting it set up in the first place. 有一个相关的问题涉及使用 PreparedStatement池,但跳过了首先设置它的细节。 Apparently it involves passing a KeyedObjectPoolFactory to the PoolableConnectionFactory , but it's not at all clear to me which implementation I should use or how to create the objects needed by their constructors. 显然,它涉及将KeyedObjectPoolFactory传递给PoolableConnectionFactory ,但我根本不清楚我应该使用哪个实现或如何创建构造函数所需的对象。

Here's how I'm currently setting up connection pooling: 这是我目前正在设置连接池的方式:

private PoolingDataSource setupDataSource() {
    ConnectionFactory connection_factory = new ConnectionFactoryImpl();
    ObjectPool connection_pool = new GenericObjectPool();
    PoolableConnectionFactory poolable_connection_factory = new PoolableConnectionFactory(connection_factory, connection_pool, null, "select 1", false, true);
    PoolingDataSource data_source = new PoolingDataSource(connection_pool);

    return data_source;
}

private static class ConnectionFactoryImpl implements ConnectionFactory {
    private Properties connection_properties = new Properties();

    public ConnectionFactoryImpl() {
        connection_properties.put("user", USER);
        connection_properties.put("password", PASSWORD);
        connection_properties.put("zeroDateTimeBehavior", "convertToNull");
        connection_properties.put("jdbcCompliantTruncation", "false");
    }

    @Override
    public Connection createConnection() throws SQLException {
        return DriverManager.getConnection("jdbc:mysql://" + SERVER + "/" + DEFAULT_DB, connection_properties);
    }
}

It's the third parameter to the PoolableConnectionFactory that controls prepared statement pooling, but I'm not sure what to use there. 这是PoolableConnectionFactory控制预处理语句池的第三个参数,但我不确定在那里使用什么。

If you are using spring, it has a helper been that sets it all up. 如果你正在使用弹簧,它有一个帮助设置它。 However, statement pooling is disabled by default, so you need to add the last 2 settings in: 但是,默认情况下禁用语句池,因此您需要在以下位置添加最后2个设置:

<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
    <property name="driverClassName" value="${database.driverClassName}"/>
    <property name="url" value="${database.url}"/>
    <property name="username" value="${database.username}"/>
    <property name="password" value="${database.password}"/>
    <property name="validationQuery" value="SELECT 1 FROM DUAL"/>
    <property name="testOnBorrow" value="true"/>
    <property name="initialSize" value="1"/>
    <property name="minIdle" value="1"/>
    <property name="maxActive" value="10"/>
    <property name="poolPreparedStatements" value="true"/>
    <property name="maxOpenPreparedStatements" value="20"/>
</bean>

Here is an example of creating a BasicDataSource for standalone apps: http://www.kodejava.org/examples/803.html 以下是为独立应用程序创建BasicDataSource的示例: http//www.kodejava.org/examples/803.html

once you have that, do the following: 完成后,请执行以下操作:

dataSource.setPoolPreparedStatements(true)
dataSource.setMaxOpenPreparedStatements(20);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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