Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2005

Tip: Looking for answers? Try searching our database.

Standard way to setup JDBC connection pooling?

Thread view: 
Aquila Deus - 01 Feb 2005 06:41 GMT
Does such a thing exist?? Or any de-facto standard way?
Mario Winterer - 01 Feb 2005 09:58 GMT
Hi!

That depends upon your environment. If you are using e.g. Tomcat webserver, connection pooling is already included if you use JNDI
datasources.
If you want to do connection pooling without any special environment (e.g. a simple application based on J2SE), you should consider
to take a ready-to-use connection-pool implementation.
Some JDBC-driver already implement connection pooling or at least support it, but many others don't.

An easy-to-use connection pool that is open source can be found at http://jakarta.apache.org/commons/dbcp/. This library requires an
implementation of an object pool which can be found at http://jakarta.apache.org/commons/pool/.

The following code shows how to create a DataSource with DBCP that uses a connection-pool for the connections:

// create object pool (see API docs for configuration details)
GenericObjectPool connectionPool = new GenericObjectPool(null);
// create a factory that 'produces' JDBC connections
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:some:connect:string", "username", "password");
// create a factory that reuses connection from the pool or creates new ones using the connectionFactory
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null,
false, true);
// create a pooling JDBC DataSource (that uses the pool configured above)
PoolingDataSource dataSource = new PoolingDataSource(connectionPool);

// Now get connections from the dataSource. The connections will be pooled!
// Do not forget to close every connection after use to let the pool do its work!

The fine thing is that those four lines above can be modified for maximum flexibility. So you can use the DriverManager for getting
connections (as above), or a DataSource that you've created before. You can create a DataSource that pools connections or you can
create a pooling JDBC Driver and register it with the DriverManager:

// replace the last line above (PoolingDataSource dataSource = ...):
// create pooling driver
PoolingDriver driver = new PoolingDriver();
// register driver with DriverManager
driver.registerPool("example",connectionPool);

// now you can open pooled connections by using the DriverManager:
Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example");

Tip: Maybe it is wise to use a higher-level API for accessing database (instead of low level JDBC). Hibernate is a very good
Object/Relational layer that also supports connection pooling!

Best regards,
 Tex

> Does such a thing exist?? Or any de-facto standard way?
huy - 01 Feb 2005 10:39 GMT
> Does such a thing exist?? Or any de-facto standard way?

Try

http://jakarta.apache.org/commons/dbcp/

Regards,

Huy
anonymous - 01 Feb 2005 15:13 GMT
> Does such a thing exist?? Or any de-facto standard way?

YMMV.

We have mixed success with Oracle OCI driver and 9i, 10G. We have also
tried their datasource driver, but it seems to be hard to get it right.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.