File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Performance and the fly likes Connection Pooling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Connection Pooling" Watch "Connection Pooling" New topic
Author

Connection Pooling

Srividya Shiv
Ranch Hand

Joined: Jul 20, 2000
Posts: 31
How do u achieve connection pooling thru Java. Its there in JDBC2.0, But I am not sure how to go about that.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
There are a couple of articles here and here.


"I'm not back." - Bill Harding, Twister
Neetu Gupta
Greenhorn

Joined: Nov 07, 2000
Posts: 4
You can create your own class for connection pooling. In that class keep a vector of predefined number of jdbc connections and a flag for eac connection indicating if the current connection is in use or not. Also keep methods to get and free the connection. In case of increase of load, add some more connections to the vector.
Derek True
Greenhorn

Joined: Nov 07, 2000
Posts: 10
Originally posted by Srividya Shiv:
How do u achieve connection pooling thru Java. Its there in JDBC2.0, But I am not sure how to go about that.

In addition to the above, you might want to go to: http://www.sys-con.com/java/archives/subscribe/0510/index.html
And check out the article,"Implementing a lightweight web server for resource pooling and scalability" on pg. 60.
Derek
KASI VISHWANATH
Ranch Hand

Joined: Aug 06, 2000
Posts: 60
Every time u need to have a connection u have to create it at that time, it takes lot of time to create the connection. To improve the performance we go for connection pooling. Connection pooling involves creation of pool of connections in advance, it can be either JDBC connections or Thread Connections (in case of thread pooling) and when a request comes for that connection, the connection already created in the pool is assigned. These are generally stored in a vector. The pool is created anticipating the demand. A minimum of few connections are created when the server is started, a after that connections are created checking the need. When the allotment of the connection has exceed the max_connections to be created the an warning is send. This will help the performance improvance drastically and a good concept for improving the speed.
------------------
I.K.VISHWANATH


I.K.VISHWANATH
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Just for fun, here's my code for a very simple pooling system which I have used successfully on many projects over the last few years. The class "com.efsol.util.Pool" is a general purpose pooling class which can be used to build classes to pool connections, threads or anything else you fancy. The class "com.efsol.database.ConnectionPool" is a Pool for database connections. I know they don't quite match with the Java Ranch official style, but feel free to use them for whatever you like.
File Pool.java

File ConnectionPool.java

I hope this is useful (or at least interesting!)


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
I am thinking about that, too.
With struts there is a connection pool, but if I use this, its strongly coupled with struts and its database layer, not presentation layer.
There seems to be some activity based on the jarkarta.commons.pool package:
http://sourceforge.net/projects/jdbcpool/
But nothing yet published.
In chapter 3 or 5 or 6 of the now not yet downloadable struts-beta-book on theServerside they explained how to use an open source object-relational-maper. Will give this a try the weekend.
At least I will have Franks code if I will run into problems. Need some demo running next week.
Thank you
Axel
Nelson David
Greenhorn

Joined: Oct 01, 2002
Posts: 13
Hello All,
I have develped my ConnetionPooling module, but I am having some problems in long run, the problem is I am caching connections in Vector object, the database server later on closes the connection when ther is no activity on connection , leaving my Vector object holding closed connections, how do i prevent it?
I am thinking of closing all the connections in Vector object when they are no longer in use after some specified time.
but that may become performance overhead instead of performance gain, I have seen Frank Carver in same thread but I wonder Frank
will be having same problem also, the cached connection may be closed database server after some time.

Any JDBC guru comment on this ,

Nelson
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Axel Janssen:
With struts there is a connection pool, but if I use this, its strongly coupled with struts and its database layer, not presentation layer.
The struts 1.1 connection pool is no more than a wrapper around the Commons DataBase Connection Pool.
Please mind that in an application server (servlet/JSP or EJB) environment, you already have a connection pool. Configure the server to make a DataSource available in the JNDI tree. That DataSource will support connection pooling.
- Peter
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Nelson David:
I have develped my ConnetionPooling module
Unless it's done as a learning exercise, I would really discourage you from doing this. There are plenty of good connection pools around (including your favourite application server and the Apache Jakarta DBCP mentioned above) and the world doesn't need another one. Also, a solid connection pool is not trivial to develop. Doing your own thing is reinventing a wheel that's likely to be inferior.
[...] I am caching connections in Vector object
Forget about Vector! It's obsolete, superfluous, has a bloated API, and its synchronization usually buys you nothing but trouble. Use a List implementation from the Collections framework. The equivalent of Vector would be an ArrayList, but for a connection pool a LinkedList would actually be more appropriate. I will leave the reasons why as an exercise for the reader A hint follows immediately below.
the database server later on closes the connection when ther is no activity on connection, leaving my Vector object holding closed connections, how do i prevent it?
Ensure each connection gets a fair amount of use (i.e. use a FIFO list rather than a LIFO list). Use a test query to test each connection before giving it to the client code, and/or actively expire connections after a certain time if inactivity, and/or use a reaper thread to periodically test connections. Those are the naive approaches. Much better would be to use the connection pool support that is probably offered by the database driver -- i.e. PooledDataSource and friends. This has an event model which can notify your pool when a connection is knocked out due to some fatal error.
Do you see what I mean by "a solid connection pool is not trivial to develop"?
- Peter
[ April 28, 2003: Message edited by: Peter den Haan ]
Jon Strayer
Ranch Hand

Joined: Dec 04, 2002
Posts: 133
Originally posted by Srividya Shiv:
How do u achieve connection pooling thru Java. Its there in JDBC2.0, But I am not sure how to go about that.

There is no portable way to do it in J2SE. In J2EE you can get a DataSource from a JNDI server.
If you are using Oracle you can go here http://technet.oracle.com/sample_code/tech/java/sqlj_jdbc/files/jdbc20/jdbc20.htm
and look at the second example.


Jon
Bruno Arantes
Greenhorn

Joined: Apr 30, 2003
Posts: 24
Jakarta's Commons Project develops a connection pool, the Commons Database Connection Pool. You can take a look at Jakarta's web site: http://jakarta.apache.org/.
Bruno.


Bruno Arantes Bueno.<br />Certified Lotus Specialist, SCJP 1.4.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connection Pooling