打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
org.apache.commons.dbcp (Commons DBCP 1.3-SNAPSHOT API)

Package org.apache.commons.dbcp

Database Connection Pool API.

See:
          Description

Interface Summary
ConnectionFactory Abstract factory interface for creating Connections.
 

Class Summary
AbandonedConfig Deprecated. This will be removed in a future version of DBCP.
AbandonedObjectPool Deprecated. This will be removed in a future version of DBCP.
AbandonedTrace Deprecated. This will be removed in a future version of DBCP.
BasicDataSource Basic implementation of javax.sql.DataSource that is configured via JavaBeans properties.
BasicDataSourceFactory JNDI object factory that creates an instance of BasicDataSource that has been configured based on the RefAddr values of the specified Reference, which must match the names and data types of the BasicDataSource bean properties.
DataSourceConnectionFactory A DataSource-based implementation of ConnectionFactory.
DelegatingCallableStatement A base delegating implementation of CallableStatement.
DelegatingConnection A base delegating implementation of Connection.
DelegatingPreparedStatement A base delegating implementation of PreparedStatement.
DelegatingResultSet A base delegating implementation of ResultSet.
DelegatingStatement A base delegating implementation of Statement.
DriverConnectionFactory A Driver-based implementation of ConnectionFactory.
DriverManagerConnectionFactory A DriverManager-based implementation of ConnectionFactory.
PoolableConnection A delegating connection that, rather than closing the underlying connection, returns itself to an ObjectPool when closed.
PoolableConnectionFactory A PoolableObjectFactory that creates PoolableConnections.
PoolablePreparedStatement A DelegatingPreparedStatement that cooperates with PoolingConnection to implement a pool of PreparedStatements.
PoolingConnection A DelegatingConnection that pools PreparedStatements.
PoolingDataSource A simple DataSource implementation that obtains Connections from the specified ObjectPool.
PoolingDriver A Driver implementation that obtains Connections from a registered ObjectPool.
 

Exception Summary
DbcpException Deprecated. This will be removed in a future version of DBCP.
SQLNestedException A SQLException subclass containing another Throwable
 

Package org.apache.commons.dbcp Description

Database Connection Pool API.

Overview in Dialog Form

Q: How do I use the DBCP package?

A: There are two primary ways to access the DBCP pool, as aDriver, or as a DataSource.You‘ll want to create an instance of PoolingDriver orPoolingDataSource. When using one of theseinterfaces, you can just use your JDBC objects the way you normally would.Closing a Connection will simply return it to its pool.

Q: But PoolingDriver andPoolingDataSource both expect anObjectPool as an input. Where do Iget one of those?

A: The ObjectPool interface is definedin the org.apache.commons.pool package (Commons-Pool).The org.apache.commons.pool.impl package has a couple of implementations,and you can always create your own.

Q: Ok, I‘ve found an ObjectPoolimplementation that I think suits my connection pooling needs. But it wantsa PoolableObjectFactory.What should I use for that?

A: The DBCP package provides a class for this purpose. It‘s calledPoolableConnectionFactory.It implements the factory and lifecycle methods ofPoolableObjectFactoryfor Connections. But it doesn‘t create the actual databaseConnections itself, if uses aConnectionFactory for that.The PoolableConnectionFactory will takeConnections created by the ConnectionFactoryand wrap them with classes that implement the pooling behaviour.

Several implementations of ConnectionFactory areprovided--one that uses DriverManager to create connections(DriverManagerConnectionFactory),one that uses a Driver to create connections(DriverConnectionFactory),one that uses a DataSource to create connections(DataSourceConnectionFactory).

Q: I think I‘m starting to get it, but can you walk me though it again?

A: Sure. Let‘s assume you want to create a DataSourcethat pools Connections. Let‘s also assume that thatthose pooled Connections should be obtained fromthe DriverManager.You‘ll want to create a PoolingDataSource.

The PoolingDataSource uses an underlyingObjectPool to create and store itsConnection.

To create a ObjectPool, you‘ll needa PoolableObjectFactory that createsthe actual Connections. That‘s whatPoolableConnectionFactory is for.

To create the PoolableConnectionFactory,you‘ll need at least two things:

  1. A ConnectionFactory from which the actual database Connections will be obtained.
  2. An empty and factory-less ObjectPool in which the Connections will be stored.
    When you pass an ObjectPool into the PoolableConnectionFactory, it will automatically register itself as the PoolableObjectFactory for that pool.
You can optionally provide a KeyedObjectPoolFactorythat will be used to create KeyedObjectPools forpooling PreparedStatements for each Connection.

In code, that might look like this:

GenericObjectPool connectionPool = new GenericObjectPool(null);ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:some:connect:string", "username", "password");PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);PoolingDataSource dataSource = new PoolingDataSource(connectionPool);

To create a PoolingDriver, we do the same thing,except that instead of creating a DataSource on the last line,we create a PoolingDriver, and register theconnectionPool with it. E.g.,:

GenericObjectPool connectionPool = new GenericObjectPool(null);ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:some:connect:string", "username", "password");PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);PoolingDriver driver = new PoolingDriver();driver.registerPool("example",connectionPool);

Since the PoolingDriver registers itselfwith the DriverManager when it is created, now you can justgo to the DriverManager to create your Connections,like you normally would:

Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example");

Q: Sounds complicated, is there an easier way?

A: If you‘re using the PoolingDriver, youdon‘t need to do this configuration in code. Instead, you can providea JOCL document that describes the connection pool,and let the PoolingDriver discover it atruntime.

Specifically, if the PoolingDriver is asked fora Connection from a pool that has not yet been registered,it will look for a named resource from which to read the pool‘s configuration,and create that pool.

For example, suppose you create a pool named "/eg" from a JOCLdocument. The "connect string" for this pool will be"jdbc:apache:commons:dbcp:/eg". To do this, you‘ll need a createa resource (just a file in your classpath) containing a JOCL descriptionof the pool. Specifically, this JOCL document should define aPoolableConnectionFactory from which thepool will be obtained. For example:

<object class="org.apache.commons.dbcp.PoolableConnectionFactory" xmlns="http://apache.org/xml/xmlns/jakarta/commons/jocl"><!-- the first argument is the ConnectionFactory --><object class="org.apache.commons.dbcp.DriverManagerConnectionFactory"><string value="jdbc:some:connect:string"/><object class="java.util.Properties" null="true"/></object><!-- the next argument is the ObjectPool --><object class="org.apache.commons.pool.impl.GenericObjectPool"><object class="org.apache.commons.pool.PoolableObjectFactory" null="true"/><int value="10"/> <!-- max active --><byte value="1"/> <!-- when exhausted action, 0 = fail, 1 = block, 2 = grow --><long value="2000"/> <!-- max wait --><int value="10"/> <!-- max idle --><boolean value="false"/> <!-- test on borrow --><boolean value="false"/> <!-- test on return --><long value="10000"/> <!-- time between eviction runs --><int value="5"/> <!-- number of connections to test per eviction run --><long value="5000"/> <!-- min evictable idle time --><boolean value="true"/> <!-- test while idle --></object><!-- the next argument is the KeyedObjectPoolFactory --><object class="org.apache.commons.pool.impl.StackKeyedObjectPoolFactory"><int value="5"/> <!-- max idle --></object><string value="SELECT COUNT(*) FROM DUAL"/> <!-- validation query --><boolean value="false"/> <!-- default read only --><boolean value="true"/> <!-- default auto commit --></object>

Simply save that file somewhere in your classpath as eg.jocl,and the PoolingDriver will find itautomatically. You need only register the PoolingDriver(for example, using the jdbc.drivers property), and use thethe DriverManager to create your Connections,like you normally would:

Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:/eg");

(Note that without the leading slash, the pool must be located atorg/apache/commons/dbcp/PoolingDriver/eg.jocl within your classpath.See Class.getResource(java.lang.String) for details.)



Copyright © The Apache Software Foundation. All Rights Reserved.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
commons-dbutils的封装方法
Tomcat 7 的新JDBC连接池的使用说明
java dbcp连接池在数据库异常恢复后不能正常工作问题解决
class "org.apache.commons.dbcp.BasicDataSource" not found
mysql报错"Host '1' is blocked because of many connection errors; unblock with 'mysqladmin flush-hos
Apache common-pool, common-dbcp源码解读与对象池原理剖析 -...
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服