sdeboy      2003/12/02 15:28:31

  Modified:    src/java/org/apache/log4j/jdbc JDBCReceiver.java
  Log:
  Updated jdbc connection handling
  Added IDField param which determines which column holds the 'id' field. 
  This field is added to a where clause to ensure the receiver only retrieves rows 
that haven't already been seen
  
  Revision  Changes    Path
  1.11      +46 -5     
jakarta-log4j-sandbox/src/java/org/apache/log4j/jdbc/JDBCReceiver.java
  
  Index: JDBCReceiver.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-log4j-sandbox/src/java/org/apache/log4j/jdbc/JDBCReceiver.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- JDBCReceiver.java 24 Jun 2003 21:03:11 -0000      1.10
  +++ JDBCReceiver.java 2 Dec 2003 23:28:31 -0000       1.11
  @@ -129,7 +129,6 @@
    *
    */
   public class JDBCReceiver extends Receiver {
  -  private boolean isActive = false;
   
     /**
      * URL of the DB for default connection handling
  @@ -148,7 +147,14 @@
     protected Connection connection = null;
     protected String sqlStatement = "";
     protected String refreshMillis = null;
  +  protected String idField = null;
  +  int lastID = -1;
  +  private static final String WHERE_CLAUSE = " WHERE ";
  +  private static final String AND_CLAUSE = " AND ";
  +  private boolean whereExists = false;
   
  +  public static final String LOG4J_ID_KEY = "log4jid";
  +  
     public JDBCReceiver() {
     }
   
  @@ -156,6 +162,7 @@
      * Start a thread which will handle the retrieval.
      */
     public void activateOptions() {
  +     whereExists = (sqlStatement.toUpperCase().indexOf(WHERE_CLAUSE) > -1);
       new JDBCReceiverThread().start();
     }
   
  @@ -230,6 +237,14 @@
       return databasePassword;
     }
   
  +  public void setIDField(String id) {
  +     idField = id;
  +  }
  +
  +  public String getIDField() {
  +     return idField;
  +  }
  +
     /**
      * Ensures that the given driver class has been loaded for sql connection
      * creation.
  @@ -248,8 +263,7 @@
       }
   
       public void run() {
  -      active = true; 
  -
  +      ResultSet rs = null;
         do {
           try {
             Logger logger = null;
  @@ -267,7 +281,13 @@
             Hashtable properties = null;
   
             Statement statement = getConnection().createStatement();
  -          ResultSet rs = statement.executeQuery(sqlStatement);
  +          String newSql = sqlStatement;
  +          if (whereExists) {
  +             newSql = newSql + AND_CLAUSE + idField + " > " + lastID;
  +          } else {
  +             newSql = newSql + WHERE_CLAUSE + idField + " > " + lastID;
  +          }
  +          rs = statement.executeQuery(newSql);
   
             while (rs.next()) {
               logger = Logger.getLogger(rs.getString("LOGGER"));
  @@ -326,7 +346,17 @@
                   new StringTokenizer(propertiesString, ",");
   
                 while (tok2.countTokens() > 1) {
  -                properties.put(tok2.nextToken(), tok2.nextToken());
  +                     String name = tok2.nextToken();
  +                     String value = tok2.nextToken();
  +                     if (name.equals(LOG4J_ID_KEY)) {
  +                             try {
  +                                     int thisInt = Integer.parseInt(value);
  +                                     if (thisInt > lastID) {
  +                                             lastID = thisInt;
  +                                     }
  +                             } catch (Exception e){}
  +                     }
  +                properties.put(name, value);
                 }
               }
   
  @@ -341,8 +371,18 @@
   
               doPost(event);
             }
  +           rs.close();
  +           rs = null;
           } catch (SQLException se) {
             se.printStackTrace();
  +        } finally {
  +                     try {
  +                             if (rs != null) {
  +                                     rs.close();
  +                             }
  +                     } catch (SQLException se) {
  +                       se.printStackTrace();
  +                     }
           }
   
           if (refreshMillis != null) {
  @@ -352,6 +392,7 @@
             }
           }
         } while (refreshMillis != null);
  +      close();
       }
     }
   }
  
  
  

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

Reply via email to