Am new to java. Am using data source in OC4J.. I have a query
I heard Connection pooling reusing the connections. I have an application that gets a connection from datasource and executing a Query and then am closing a connection. How come the connection pool maintain this closed connection in a pool..
Forgive me if this question is foolish..
I guess while we closing some connections the pool will open a new connection.. [ December 17, 2008: Message edited by: Meet Gaurav ]
Hi, Connection pooling is some thing like a pool where in the connections objects are available and ready to use kind of.. We can use a connection and release from use to the pool(we need not close the pool, we are leaving the object for further use)..So we are actually avoiding spending time in opening and closing the connections.
Connection pool does not contain the connection object of type Connection but PooledConnection.And connection pool actually returns you the handle(or proxy) for the physical connection held by PooledConnection object. so when you call the close() method on handle or proxy,the Datasource implementation merely remove the association between the handle or proxy) object and PooledConnection object. Hope it will clear the concept to certain level. [ December 18, 2008: Message edited by: raj malhotra ]
Originally posted by raj malhotra: Connection pool does not contain the connection object of type Connection but PooledConnection.
That is confusing at two ways.
The pooled connection is certainly of type java.sql.Conenction. How would one use it as a Connection otherwise? The "PooledConnection" is not necessarily called that. It can be called anything, depending on the implementor.
Point is that the connection pool wraps each connection with its own implementation of java.sql.Connection. It decorates (HAS-A) the actual Conntection returned by the JDBC driver and overrides the close() method. The overriden close() method determines whether to return the connection to the pool or to actually close it.
Hi Bauke I am not sure how implementer has implemented it, may be the same way as Ejb implements proxy pattern. But the below text is from sun j2se 1.4 - public interface PooledConnection.Its is a part of API ,which Sun provides for vendor imlementation.Also connection pooling can be maintained in our own application,rather than using from application server ,then your suggested approach seems to be suitable.
public interface PooledConnection An object that provides hooks for connection pool management. A PooledConnection object represents a physical connection to a data source. The connection can be recycled rather than being closed when an application is finished with it, thus reducing the number of connections that need to be made.
An application programmer does not use the PooledConnection interface directly; rather, it is used by a middle tier infrastructure that manages the pooling of connections.
When an application calls the method DataSource.getConnection, it gets back a Connection object. If connection pooling is being done, that Connection object is actually a handle to a PooledConnection object, which is a physical connection.
The connection pool manager, typically the application server, maintains a pool of PooledConnection objects. If there is a PooledConnection object available in the pool, the connection pool manager returns a Connection object that is a handle to that physical connection. If no PooledConnection object is available, the connection pool manager calls the PooledConnection method getConnection to create a new physical connection and returns a handle to it.
When an application closes a connection, it calls the Connection method close. When connection pooling is being done, the connection pool manager is notified because it has registered itself as a ConnectionEventListener object using the ConnectionPool method addConnectionEventListener. The connection pool manager deactivates the handle to the PooledConnection object and returns the PooledConnection object to the pool of connections so that it can be used again. Thus, when an application closes its connection, the underlying physical connection is recycled rather than being closed.
The physical connection is not closed until the connection pool manager calls the PooledConnection method close. This method is generally called to have an orderly shutdown of the server or if a fatal error has made the connection unusable.
[ December 20, 2008: Message edited by: raj malhotra ]