Closing connection in finally block

Hi,

Is it a recommend practice to close db connection in finally block?

Here's my code.

public class ChargeService {

      private Connection conn = null;
      private PreparedStatement statement = null;
      private ResultSet rs = null;
      private String sqlStr = "";


      public boolean isCreditEnough(int groupId, String phoneNumber) {

            boolean isEnough = false;
            int creditBalance = 0;
            int creditToCharge = 0;

            try {
                  DbUtils.loadDriver(DbConfig.dbDriver);
                  conn = DriverManager.getConnection(DbConfig.dbUrl);

                  sqlStr = "SELECT totalCredit, credit" + " FROM"
                              + " chat_users, chat_credits" + " WHERE" + " groupId="
                              + groupId + " AND" + " phoneNumber='" + phoneNumber + "'";
                  statement = conn.prepareStatement(sqlStr);
                  rs = statement.executeQuery();

                  while (rs.next()) {
                        creditBalance = rs.getInt("totalCredit");
                        creditToCharge = rs.getInt("credit");
                  }

                  if (creditBalance - creditToCharge >= 0) {
                        isEnough = true;

                  } else {
                        isEnough = false;

                  }

            } catch (SQLException e) {
                  throw new ChatException(getClass().toString(), e);

            } finally {
                  DbUtils.closeQuietly(rs);
                  DbUtils.closeQuietly(statement);
                  DbUtils.closeQuietly(conn);

            }

            return isEnough;

      }
}

BTW, i use DBUtils from Jakarta Common project. So, my problem is,

-I'm got problems with closing my db connections, based on my connection threads i see in my mysqladmin it keeps getting more and more.

My question is,
-Is this a problem of DbUtils.closeQuietly(conn) not executing or my code problem?
-what is the recommend approach to close connection?

Melv

mwhcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tomgalloCommented:
I'll use this code instead:

try{
      Connection conn=null;
}catch(SQLException SQLE){
      throw new Exception(SQLE.getMessage());
}finally{
      try{
            if(conn!=null)
                  conn.close();
      }catch(SQLException SQLE){}
}

The same try/catch for a ResultSet or a CallableStatement/PreparedStatement
0
abdullah716Commented:
I think you should explicitly call the Dispose method of the connection object to close unmanaged resources. Don't know about these Jakarta utils, but check if they call the Dispose method.
0
tomgalloCommented:
Venabili,

The code that I posted is working and answers the question. What type of recomendation do you need?
Help me to understand please.

-tom
0
abdullah716Commented:
Closing a connection is not enough to clear unmanaged resources. These are later handled by the GC. The dispose method of the connection object is required to free resource immediately. I feel I deserve at least some points.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tomgalloCommented:
I agree abdullah
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.