在 dbcp 中使用PreparedStatement池

发布于 2024-07-09 09:10:59 字数 211 浏览 11 评论 0原文

有人可以解释一下如何使用 dbcp 准备的连接池吗? (如果可能的话,带有一些示例代码)。 我已经弄清楚如何打开它 - 将 KeyedObjectPoolFactory 传递给 PoolableConnectionFactory。 但是之后具体的prepared statements应该如何定义呢? 现在我只使用 PoolingDataSource 从池中获取连接。 如何使用池中准备好的语句?

Can someone explain how exactly prepared connection pooling using dbcp can be used? (with some example code if possible). I've figured out how to turn it on - passing a KeyedObjectPoolFactory to the 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. How do I use the prepared statements from the pool?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凑诗 2024-07-16 09:10:59

好吧,谈论从池中获取连接与获取“非池化”连接,您的代码有任何变化吗:)? 我打赌你不会。 与准备好的语句相同。 您的代码不应更改。 因此,没有有用的代码示例。

您应该阅读 JDBC 数据源实现的文档,并了解开发人员对池化的看法。 对此没有其他可靠信息来源。

来自此处
该组件还能够池化PreparedStatements。 启用后,将为每个连接创建一个语句池,并且通过以下方法之一创建的PreparedStatements将被池化:

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

因此,您只需继续使用prepareStatement()调用,理论上您的dbcp将负责池化(即,如果您尝试要创建“select * from users u where u.name like :id”,它会首先尝试在池中找到此语句)

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. There is no other source of reliable info on this.

From here:
This component has also the ability to pool PreparedStatements. When enabled a statement pool will be created for each Connection and PreparedStatements created by one of the following methods will be pooled:

* 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 (i.e. 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)

疯到世界奔溃 2024-07-16 09:10:59

这是我使用的基本代码。

    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);

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);
北城半夏 2024-07-16 09:10:59

问题是,如果您使用单个Connection,无论您是否想要,它都会缓存PreparedStatement,对此产生影响的唯一可能方法是使用DataSource 属性或使用特定于供应商的 API。 但是这些语句对其他连接不可见,如果您使用另一个连接准备相同的语句,它将再次重新创建它。 因此,像 DBCP 这样的连接池允许在不同的连接之间重用 PreparedStatement(它使用 PooledConnection 接口而不是简单的 Connection),它们保留跟踪所有连接准备的所有语句。

更新:看来我对这个信息的理解是错误的,至少我在C3P0中找不到这个功能。

The thing is if you use a single Connection, it will cache PreparedStatements 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. 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 PreparedStatements betwixt different connections (it uses PooledConnection interface instead of simple Connection), they keep track of all the statements prepared by all connections.

UPDATE: it seems I was wrong on this info, at least I couldn't find this functionality in C3P0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文