This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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
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
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
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.
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
Joined: Apr 20, 2000
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 ]