yoavs       2005/07/30 13:35:44

  Modified:    docs     HISTORY.txt
               src/java/org/apache/log4j/db DBAppender.java
  Log:
  Bugzila 34747: http://issues.apache.org/bugzilla/show_bug.cgi?id=34747.
  
  Revision  Changes    Path
  1.34      +3 -0      logging-log4j/docs/HISTORY.txt
  
  Index: HISTORY.txt
  ===================================================================
  RCS file: /home/cvs/logging-log4j/docs/HISTORY.txt,v
  retrieving revision 1.33
  retrieving revision 1.34
  diff -u -r1.33 -r1.34
  --- HISTORY.txt       28 Jul 2005 19:40:13 -0000      1.33
  +++ HISTORY.txt       30 Jul 2005 20:35:43 -0000      1.34
  @@ -56,6 +56,9 @@
    
    - Fixed Bugzilla 30623, better JavaDoc for 
DailyRollingFileAppender#getDatePattern.
      http://issues.apache.org/bugzilla/show_bug.cgi?id=30623 [*]
  +
  + - Fixed Bugzilla 34747: DBAppender reused Statement objects inappropriately.
  +   http://issues.apache.org/bugzilla/show_bug.cgi?id=34747 [*]
    
     
     January 20th, 2005
  
  
  
  1.22      +120 -108  
logging-log4j/src/java/org/apache/log4j/db/DBAppender.java
  
  Index: DBAppender.java
  ===================================================================
  RCS file: 
/home/cvs/logging-log4j/src/java/org/apache/log4j/db/DBAppender.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- DBAppender.java   8 Mar 2005 22:32:57 -0000       1.21
  +++ DBAppender.java   30 Jul 2005 20:35:44 -0000      1.22
  @@ -1,5 +1,5 @@
   /*
  - * Copyright 1999,2004 The Apache Software Foundation.
  + * Copyright 1999,2004-2005 The Apache Software Foundation.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
  @@ -183,120 +183,132 @@
     }
   
     protected void append(LoggingEvent event) {
  -    Connection connection = null;
  -    try {
  -      connection = connectionSource.getConnection();
  -      connection.setAutoCommit(false);
  -
  -      PreparedStatement insertStatement =
  -        connection.prepareStatement(insertSQL);
  -      insertStatement.setLong(1, event.getSequenceNumber());
  -      insertStatement.setLong(2, event.getTimeStamp());
  -      insertStatement.setString(3, event.getRenderedMessage());
  -      insertStatement.setString(4, event.getLoggerName());
  -      insertStatement.setString(5, event.getLevel().toString());
  -      insertStatement.setString(6, event.getNDC());
  -      insertStatement.setString(7, event.getThreadName());
  -      insertStatement.setShort(8, DBHelper.computeReferenceMask(event));
  -
  -      LocationInfo li;
  -
  -      if (event.locationInformationExists() || locationInfo) {
  -        li = event.getLocationInformation();
  -      } else {
  -        li = LocationInfo.NA_LOCATION_INFO;
  -      }
  -
  -      insertStatement.setString(9, li.getFileName());
  -      insertStatement.setString(10, li.getClassName());
  -      insertStatement.setString(11, li.getMethodName());
  -      insertStatement.setString(12, li.getLineNumber());
  -
  -      int updateCount = insertStatement.executeUpdate();
  -      if (updateCount != 1) {
  -        getLogger().warn("Failed to insert loggingEvent");
  -      }
  -
  -      Statement idStatement = connection.createStatement();
  -      idStatement.setMaxRows(1);
  -
  -      ResultSet rs = null;
  -      if (cnxSupportsGetGeneratedKeys) {
  -        rs = insertStatement.getGeneratedKeys();
  -      } else {
  -        rs = idStatement.executeQuery(sqlDialect.getSelectInsertId());
  -      }
  -
  -      // A ResultSet cursor is initially positioned before the first row; 
the 
  -      // first call to the method next makes the first row the current row
  -      rs.next();
  -      int eventId = rs.getInt(1);
  -
  -      // we no longer need the insertStatement
  -      insertStatement.close();
  -      insertStatement = null;
  -
  -      Set propertiesKeys = event.getPropertyKeySet();
  -
  -      if (propertiesKeys.size() > 0) {
  -        PreparedStatement insertPropertiesStatement =
  -          connection.prepareStatement(insertPropertiesSQL);
  -
  -        for (Iterator i = propertiesKeys.iterator(); i.hasNext();) {
  -          String key = (String) i.next();
  -          String value = (String) event.getProperty(key);
  -
  -          //LogLog.info("id " + eventId + ", key " + key + ", value " + 
value);
  -          insertPropertiesStatement.setInt(1, eventId);
  -          insertPropertiesStatement.setString(2, key);
  -          insertPropertiesStatement.setString(3, value);
  -
  -          if (cnxSupportsBatchUpdates) {
  -            insertPropertiesStatement.addBatch();
  +      Connection connection = null;
  +      try {
  +          connection = connectionSource.getConnection();
  +          connection.setAutoCommit(false);
  +          
  +          PreparedStatement insertStatement =
  +              connection.prepareStatement(insertSQL);
  +          insertStatement.setLong(1, event.getSequenceNumber());
  +          insertStatement.setLong(2, event.getTimeStamp());
  +          insertStatement.setString(3, event.getRenderedMessage());
  +          insertStatement.setString(4, event.getLoggerName());
  +          insertStatement.setString(5, event.getLevel().toString());
  +          insertStatement.setString(6, event.getNDC());
  +          insertStatement.setString(7, event.getThreadName());
  +          insertStatement.setShort(8, DBHelper.computeReferenceMask(event));
  +          
  +          LocationInfo li;
  +          
  +          if (event.locationInformationExists() || locationInfo) {
  +              li = event.getLocationInformation();
             } else {
  -            insertPropertiesStatement.execute();
  +              li = LocationInfo.NA_LOCATION_INFO;
  +          }
  +          
  +          insertStatement.setString(9, li.getFileName());
  +          insertStatement.setString(10, li.getClassName());
  +          insertStatement.setString(11, li.getMethodName());
  +          insertStatement.setString(12, li.getLineNumber());
  +          
  +          int updateCount = insertStatement.executeUpdate();
  +          if (updateCount != 1) {
  +              getLogger().warn("Failed to insert loggingEvent");
  +          }
  +          
  +          ResultSet rs = null;
  +          Statement idStatement = null;
  +          if (cnxSupportsGetGeneratedKeys) {
  +              rs = insertStatement.getGeneratedKeys();
  +          } else {
  +              insertStatement.close();
  +              insertStatement = null;
  +              
  +              idStatement = connection.createStatement();
  +              idStatement.setMaxRows(1);
  +              rs = idStatement.executeQuery(sqlDialect.getSelectInsertId());
  +          }
  +          
  +          // A ResultSet cursor is initially positioned before the first 
row; the 
  +          // first call to the method next makes the first row the current 
row
  +          rs.next();
  +          int eventId = rs.getInt(1);
  +          
  +          rs.close();
  +
  +          // we no longer need the insertStatement
  +          if(insertStatement != null) {
  +              insertStatement.close();
  +              insertStatement = null;
             }
  -        }
  -
  -        if (cnxSupportsBatchUpdates) {
  -          insertPropertiesStatement.executeBatch();
  -        }
  -
  -        insertPropertiesStatement.close();
  -        insertPropertiesStatement = null;
  -      }
  -
  -      String[] strRep = event.getThrowableStrRep();
  -
  -      if (strRep != null) {
  -        getLogger().debug("Logging an exception");
   
  -        PreparedStatement insertExceptionStatement =
  -          connection.prepareStatement(insertExceptionSQL);
  +          if(idStatement != null) {
  +              idStatement.close();
  +              idStatement = null;
  +          }
   
  -        for (short i = 0; i < strRep.length; i++) {
  -          insertExceptionStatement.setInt(1, eventId);
  -          insertExceptionStatement.setShort(2, i);
  -          insertExceptionStatement.setString(3, strRep[i]);
  -          if (cnxSupportsBatchUpdates) {
  -            insertExceptionStatement.addBatch();
  -          } else {
  -            insertExceptionStatement.execute();
  +          Set propertiesKeys = event.getPropertyKeySet();
  +          
  +          if (propertiesKeys.size() > 0) {
  +              PreparedStatement insertPropertiesStatement =
  +                  connection.prepareStatement(insertPropertiesSQL);
  +              
  +              for (Iterator i = propertiesKeys.iterator(); i.hasNext();) {
  +                  String key = (String) i.next();
  +                  String value = (String) event.getProperty(key);
  +                  
  +                  //LogLog.info("id " + eventId + ", key " + key + ", value 
" + value);
  +                  insertPropertiesStatement.setInt(1, eventId);
  +                  insertPropertiesStatement.setString(2, key);
  +                  insertPropertiesStatement.setString(3, value);
  +                  
  +                  if (cnxSupportsBatchUpdates) {
  +                      insertPropertiesStatement.addBatch();
  +                  } else {
  +                      insertPropertiesStatement.execute();
  +                  }
  +              }
  +              
  +              if (cnxSupportsBatchUpdates) {
  +                  insertPropertiesStatement.executeBatch();
  +              }
  +              
  +              insertPropertiesStatement.close();
  +              insertPropertiesStatement = null;
             }
  +          
  +          String[] strRep = event.getThrowableStrRep();
  +          
  +          if (strRep != null) {
  +              getLogger().debug("Logging an exception");
  +              
  +              PreparedStatement insertExceptionStatement =
  +                  connection.prepareStatement(insertExceptionSQL);
  +              
  +              for (short i = 0; i < strRep.length; i++) {
  +                  insertExceptionStatement.setInt(1, eventId);
  +                  insertExceptionStatement.setShort(2, i);
  +                  insertExceptionStatement.setString(3, strRep[i]);
  +                  if (cnxSupportsBatchUpdates) {
  +                      insertExceptionStatement.addBatch();
  +                  } else {
  +                      insertExceptionStatement.execute();
  +                  }
           }
  -        if (cnxSupportsBatchUpdates) {
  -          insertExceptionStatement.executeBatch();
  -        }
  -        insertExceptionStatement.close();
  -        insertExceptionStatement = null;
  +              if (cnxSupportsBatchUpdates) {
  +                  insertExceptionStatement.executeBatch();
  +              }
  +              insertExceptionStatement.close();
  +              insertExceptionStatement = null;
  +          }
  +          
  +          connection.commit();
  +      } catch (SQLException sqle) {
  +          getLogger().error("problem appending event", sqle);
  +      } finally {
  +          DBHelper.closeConnection(connection);
         }
  -
  -      connection.commit();
  -    } catch (SQLException sqle) {
  -      getLogger().error("problem appending event", sqle);
  -    } finally {
  -      DBHelper.closeConnection(connection);
  -    }
     }
   
     public void close() {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to