datasource connections in J2EE app question

Discussions

General J2EE: datasource connections in J2EE app question

  1. datasource connections in J2EE app question (6 messages)

    hi all
    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();
    jndiProps.put(Context.INITIAL_CONTEXT_FACTORY,...);
    jndiProps.put(Context.PROVIDER_URL, ...);
     
    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
  2. That depends on your application server[ Go to top ]

    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.
  3. datasource connections in J2EE app question[ Go to top ]

    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.
  4. Releasing reference might as well do it[ Go to top ]

    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.

    Thanks,
    Prasun
  5. datasource connections in J2EE app question[ Go to top ]

    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.
  6. 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.

    Thanks,
    Prasun
  7. 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."