简体   繁体   English

在dbcp中使用PreparedStatement池

[英]Using PreparedStatement pooling in dbcp

Can someone explain how exactly prepared connection pooling using dbcp can be used? 有人可以解释如何使用dbcp准确准备连接池吗? (with some example code if possible). (如果可能,请使用一些示例代码)。 I've figured out how to turn it on - passing a KeyedObjectPoolFactory to the PoolableConnectionFactory. 我已经想出如何打开它 - 将KeyedObjectPoolFactory传递给PoolableConnectionFactory。 But how should the specific prepared statements be defined after that? 但是之后应该如何定义具体的准备陈述呢? Right now I'm only using a PoolingDataSource to get connections from the pool. 现在我只使用PoolingDataSource从池中获取连接。 How do I use the prepared statements from the pool? 如何使用池中的预准备语句?

Well talking about getting connection from the pool vs getting "not-pooled" connection, do you have any change in your code :)? 那么谈论从池中获取连接与获得“非池化”连接,你的代码有任何改变:)? I bet you do not. 我打赌你没有。 Same way with prepared statements. 与准备好的陈述相同。 Your code should not change. 您的代码不应该更改。 So, there is no useful code example to this. 因此,没有有用的代码示例。

You should read docs for your JDBC Datasource implementation and see what developers have to say about pooling. 您应该阅读JDBC数据源实现的文档,并了解开发人员对池化的看法。 There is no other source of reliable info on this. 没有其他可靠信息来源。

From here : This component has also the ability to pool PreparedStatements. 这里 :该组件还具有池PreparedStatements的能力。 When enabled a statement pool will be created for each Connection and PreparedStatements created by one of the following methods will be pooled: 启用后,将为每个Connection创建一个语句池,并且将汇集由以下方法之一创建的PreparedStatements:

* public PreparedStatement prepareStatement(String sql)
* public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)

So, you just keep using prepareStatement() call and your dbcp will in theory take care of pooling (ie if you are trying to create "select * from users u where u.name like :id", it will try to find this statement in the pool first) 所以,你只是继续使用prepareStatement()调用,你的dbcp理论上会处理池(即如果你试图创建“select * from users u where u.name like:id”,它会尝试查找这个语句在游泳池第一)

Here's basic code I use. 这是我使用的基本代码。

    GenericObjectPool connectionPool = new GenericObjectPool(null);
    connectionPool.setMinEvictableIdleTimeMillis(1000 * 60 * 30);
    connectionPool.setTimeBetweenEvictionRunsMillis(1000 * 60 * 30);
    connectionPool.setNumTestsPerEvictionRun(3);
    connectionPool.setTestOnBorrow(true);
    connectionPool.setTestWhileIdle(false);
    connectionPool.setTestOnReturn(false);

    props = new Properties();
    props.put("user", username);
    props.put("password", password);
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, props);

    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, "SELECT 1", false, true);
    PoolingDataSource dataSource = new PoolingDataSource(connectionPool);

The thing is if you use a single Connection , it will cache PreparedStatement s whether you want this or not, the only possible way to impact on this is to use DataSource properties or to use vendor-specific API. 问题是如果您使用单个Connection ,它将缓存PreparedStatement无论您是否想要这样,唯一可能的方法是使用DataSource属性或使用特定于供应商的API。 But these statements are not visible by other connections and if you prepare the same statement using another connection, it will recreate it again. 但是其他连接看不到这些语句,如果使用其他连接准备相同的语句,它将再次重新创建它。 So Connection Pools like DBCP under the hood allow reusing of PreparedStatement s betwixt different connections (it uses PooledConnection interface instead of simple Connection ), they keep track of all the statements prepared by all connections. 因此,像DBCP这样的连接池允许在不同的连接之间重用PreparedStatement (它使用PooledConnection接口而不是简单的Connection ),它们跟踪所有连接准备的所有语句。

UPDATE: it seems I was wrong on this info, at least I couldn't find this functionality in C3P0. 更新:这个信息似乎我错了,至少我在C3P0中找不到这个功能。

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

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