I have a dilema regarding connection Object issue. Pls assist me. I have created a small application using MVC2.0 architecture where my servlet is a controller. Now to ease database operations i have intialised "Connection Object" conn in init() so that only once connection to database is initialized and every session can use this object and can access database. First of all i would like to know is this approach correct? Its working fine for me but on a discussion with my friend he told me that **ONE** connection object can handle only **ONE** request for user. That means you will have to create connection everytime per SESSION which i think is not at all feasible idea. Can anyone shed some light on connection object and how to reduce creating connection object everytime per session?
I am aware we can use Connection Pooling but i am using Directory Service and not sure how to use connection pooling in Directory Service. Hence for time being is my approach of using **ONE** Global Connection Object in init() and every client/session using the same connection object is correct?
Yes, your friend is right, the connection object to your datasource (directory service) cannot be shared across mutilple requests/users/threads. You have to ensure that each request/user/thread gets exclusive access to the connection object.
now you can do this by locking(synchronized) access to the "single" connection object, but this is not advisablle as this will slow down things for u. the other way round is to create a new connection object for each request/user/thread. or else, the best way, is to create a pool of reusable connection object, which will ensure exclusive access to the connection object.
Thanks a lot kapil! I could find netscape.ldap.util.ConnectionPool class provides good feature of creating connection pool setting **MIN** and **MAX** Pool size, but nowhere there is a provisioning of **disconnecting** the connections if the connection are ideal more than a longer period suppose 2 days or so. For instance if we assign **MIN** connection to 5 and the application is using only 2 connection keeping 3 ideal for long period of time. In such scenario will connection remains ideal unless we destroy the whole connection pool.
Also what if at one stage connection goes to **MAX** and now pool has **MAX** connection but the next day onwards clients access becomes less and number of connection ideal are more in the pool. In such scenario as well there should be a provisioning of **disconnection** the connections if they are ideal for a larger time then specified.
I have not yet came across proper solution. Do anyone have any idea or any discussion forum for java/ldap/Directory Services.