Uploaded image for project: 'Application Server 3  4  5 and 6'
  1. Application Server 3 4 5 and 6
  2. JBAS-680

PreparedStatement leak in HiLoKeyGenerator.java

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Obsolete
    • Icon: Major Major
    • JBossAS-4.0.4.GA
    • JBossAS-4.0.0 Final
    • EJB2
    • None

      SourceForge Submitter: cmunday .
      Hi,

      There is a Statement that does not get closed and is
      left to be finialized within the following method (included
      below) from the HiLoKeyGenerator class.

      The statement create by:
      stmt = con.prepareStatement(nextKeyQuery);

      leaks because the variable "stmt" is re-assigned with:
      stmt = con.prepareStatement(updateStmt);

      This second statement is closed within the finally clause
      but the first one is never closed and should be

      Cheers,
      Craig.

      public synchronized Object generateKey()
      {
      if(lo < hi)
      return new Integer(lo++);

      Connection con = null;
      PreparedStatement stmt = null;
      ResultSet rs = null;
      Transaction currentTx = null;
      try
      {
      currentTx = tm.suspend();
      tm.begin();

      con = datasource.getConnection();
      int rowsUpdated;
      do

      { if(isDebugEnabled) log.debug("Executing SQL: " + nextKeyQuery); stmt = con.prepareStatement(nextKeyQuery); rs = stmt.executeQuery(); if(!rs.next()) throw new Exception("Couldn't read next key: ResultSet is empty."); lo = rs.getInt(1); hi = lo + blockSize; if(isDebugEnabled) log.debug("Executing SQL: " + updateStmt + " [p1=" + hi + ",p2=" + lo + "]"); stmt = con.prepareStatement(updateStmt); stmt.setInt(1, hi); stmt.setInt(2, lo); rowsUpdated = stmt.executeUpdate(); }

      while(rowsUpdated == 0);

      tm.commit();
      }
      catch(Exception e)
      {
      log.error("Error fetching next key:", e);
      try

      { tm.rollback(); }

      catch(SystemException se)

      { log.error("Couldn't rollback transaction: ", se); }

      }
      finally
      {
      JDBCUtil.safeClose(rs);
      JDBCUtil.safeClose(stmt);
      JDBCUtil.safeClose(con);

      if(currentTx != null)
      {
      try

      { tm.resume(currentTx); }

      catch(InvalidTransactionException ite)

      { log.error("Invalid transaction: ", ite); }

      catch(SystemException se)

      { log.error("Unexpected error: ", se); }

      }
      }
      return new Integer(lo++);
      }

            olubyans@redhat.com Alexey Loubyansky
            sourceforge-user SourceForge legacy user (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: