1

In my Project i have a server application and a client application and one mediator application.Client communicates with the server through the mediator application.The mediator application inturn connects to the server application.

For Every client it needs to connect to the server application. I know that for every client request opening a new connection is expensive. I think i can make use of the connection pooling concept. How to implement this in java. Any pointers appreciated...

Thanks and Regards,

Sunny.

Dungeon Hunter
  • 19,827
  • 13
  • 59
  • 82

3 Answers3

2

Use an object pooling api, such as Apache Commons Pool to implement your own pooling mechanism, or use an existing and feature rich caching solution such as Ehcache.

Sylar
  • 2,273
  • 2
  • 18
  • 26
0

Please go through this doc, valuable information connection pooling

developer
  • 9,116
  • 29
  • 91
  • 150
0

This can be made possible by using ExecutorService in java.util.concurrent package. Executors is a factory method that provides implementation for thread pools with either fixed number of threads or thread pool size bounded by the memory available.

From personal experience, fixed size thread pools are reliable than cached thread pools due to overhead involved in destroying and creating threads.

kuriouscoder
  • 5,394
  • 7
  • 26
  • 40
  • Out of curiosity, how does a thread pool help in connection pooling? – Vineet Reynolds May 19 '11 at 05:12
  • look at the example in http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html – kuriouscoder May 19 '11 at 05:18
  • I've looked at the example. Where the connection pool? I think you've confused the concept of a thread pool with a connection pool. A thread pool executes a task when it is requested for; a connection pool on the other hand is a pool of physical connections to a resource that can be reused without tearing down and re-creating the connections (which is expensive). – Vineet Reynolds May 19 '11 at 05:25
  • Hmm -- this where lack of conceptual ideas differ from raw terms. Underneath below connection pools are implemented using some kind of thread pools - where the number of threads restrict the connections be it to database or server. To help further - try implementing a small connection cool. Here's Neil Gater's quandry - http://gafter.blogspot.com/2006/11/thread-pool-puzzler.html – kuriouscoder May 19 '11 at 05:33
  • I don't think you know how connection pools have been implemented. They do not use thread pools, especially in the case of JDBC. Check the implementation of any JDBC driver, specifically that of the classes implementing and using the [PooledConnection](http://download.oracle.com/javase/6/docs/api/javax/sql/PooledConnection.html) interface. – Vineet Reynolds May 19 '11 at 05:43