Order of closing db resources in java


EJB programming & troubleshooting: Order of closing db resources in java

  1. Order of closing db resources in java (4 messages)

             Can anyone give me the order of closing database resources in java like which should be first,second and third to be closed( connection,resultset,statement)..

    thanks in advance......


    Threaded Messages (4)

  2. Order of closing db resources in java[ Go to top ]

    You shuld close the Resultset first and then you should close the statement, though it is not absolutely necessary to do so. I guess the statement object will be garbage collected as soon as it goes out of scope. And close the connection in the finally { } block.

    you will find some good info here:


    I hope it helps you.



  3. You dont need to bother about the result set. When you close the statement - the associated result set is automatically closed.

    After this, you should close the connection.
  4. I think (althout I'm not a 100% sure this is mandated by the spec) that you can just close the Connection. The Connection's cursors should go with it... Atleast I know it works with Oracle drivers :)

  5. be carefull[ Go to top ]

    maybe invoking the close method on the connection works, but what if you're using a connection pool?.

    the close method will be replaced to make the connection go back to the pool, and depending on the implementation of the pool, nothing might get closed at all.

    you should close all the resources first the ResultSet, then the Statement, and finally the Connection