user2505019
user2505019

Reputation: 53

Glassfish JDBC Connection Pooling Issue on Ubuntu

A sever issue I am facing after deployment on Glassfish4 and Ubuntu 14.04 . In java, I am not using hibernate due to some reasons. I am manually getting the resultset as a result of query, sending the result set to JSP page and iterating over it.

Problem is, I have set the finally block as below:

 finally {
            try {

                if (conn != null) {
                    conn.close();
                }
                if (ctx != null) {
                    ctx.close();
                }
                if (cstatement != null) {
                    cstatement.closeOnCompletion();
                }

            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            } catch (NamingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

And because I am sending the Resultset to JSP page through session, I am closing the result on JSP page as follows:

if(resultset!=null) resultset.close();

Now, problem is, after few minutes my application starts giving exception that max of the connection pools has been used.

What should I do to avoid exception?

Upvotes: 0

Views: 121

Answers (1)

waterscar
waterscar

Reputation: 876

The problem with your current finally block is:

If any exception is thrown from one of the close command, the following command will not be run. Since the issue seems to be resource leak so that might be the cause.

If you are running under JDK7+ and the resource is auto-closable, try to use

try(Connection conn=DriverManager.getConnection();
  Statement stat=conn.createStatement){
}

Then the resources will be closed safely exiting try block.

And if you need to close resource from finally block, try to wrap a try catch for each close statement.

For ordering of closing resource, always close the resource you created later first.

Upvotes: 1

Related Questions