Using PreparedStatement pooling in dbcp

社会主义新天地 提交于 2019-11-27 22:28:02

问题


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?


回答1:


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)




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/281744/using-preparedstatement-pooling-in-dbcp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!