我想知道如何设置 dbcp 以使用池 PreparedStatements。我似乎可以使用连接池,但在准备好的语句的示例中找不到太多。有一个相关的问题涉及使用PreparedStatement 池,但首先跳过了设置它的细节。显然它涉及将 a 传递KeyedObjectPoolFactory
给PoolableConnectionFactory
,但我完全不清楚我应该使用哪个实现或如何创建其构造函数所需的对象。
这是我当前设置连接池的方式:
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);
}
}
这PoolableConnectionFactory
是控制准备好的语句池的第三个参数,但我不确定在那里使用什么。