my question is about How the connections stored in a Jndi server datasource pool are managed.
For example, I'm developing a J2EE application with Ejbs and Datasources, etc. I saw that when I want to retrieve a connection from datasource to work on it I need do something like this:
For example to retrieve a datasource connection:
Hashtable jndiProps = new Hashtable();
Context context = new InitialContext(jndiProps);
DataSource ds = (DataSource) context.lookup("jdbc/MyDatasource");
Connection con = ds.getConnection();
I know in this case that the server manages a datasource connection poo, etc
My question is when a get a connection from the Datasource pool of my app server. do I need to release the connection with some special method when I finish with it (in order to return it toe de pool) or it doesn't matter ?
thanks in advance
do I need to release the connection with some special method when I finish with it (in order to return it toe de pool) or it doesn't matter?
As the subject says, it depends. I only know JBoss, and in that case, it will taks care of returning it to the pool when you call the close() method.
I imagine most modern servers are probably similar.
it is neccesary to invoke some method like"close()" as to inform the server that you don't need the special connection from now on.then the server will return it into pool for reuse by the other applications,of course,in fact it won't be shut down.
I think it is possible for an application server to release the connection to the pool, even if the connection wrapper reference is just released without a close call being called on it, by making the wrapper an extension of phantom reference and then polling in the reference queue.
Of course this increases the holding time, and is not advisable at all. But technically it is feasible.
I don't think it is feasible to release a connection without a close call being called on it.
because even if you created a queue and put the active connection into the queue,then when you didn't need the connection any more,you also need inform the queue that the connection has been release,i.e,in any case should you call a method like "close".then why we need a queue? of course we can set a invalid time,I think the datasources has the functionality.
The client releasing the reference (that it gets from ds.getConnection()) itself is an indication that it does not want to use the connection any more. Then, since the conection wrapper is a phantom reference, it is added to the reference queue by the java garbage collection mechanism, when the client releases the reference. Since the reference queue is available with the application server, it can poll and get the references available in the queue (and all the references that are in the queue are releasable to the connection pool, as client has released its reference, and thats why they are in the queue).
Hope it is clear.
Yes it is feasible to release a connection without a close call, but it would be bad style to do so.
Jboss, for example, can do it -- It just spams your logs with messages like "closing a connection for you - do your own housekeeping."