Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2011, All rights reserved. This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License v1.0 as published by the Eclipse Foundation or (per the licensee's choosing) under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
 package ch.qos.logback.core.db;

Ceki Gülcü
Ray DeCampo
Sébastien Pennec
 public abstract class DBAppenderBase<E> extends UnsynchronizedAppenderBase<E> {
   protected boolean cnxSupportsGetGeneratedKeys = false;
   protected boolean cnxSupportsBatchUpdates = false;
   protected SQLDialect sqlDialect;
   protected abstract Method getGeneratedKeysMethod();
   protected abstract String getInsertSQL();
   public void start() {
     if ( == null) {
       throw new IllegalStateException(
           "DBAppender cannot function without a connection source");
      = DBUtil
     if (getGeneratedKeysMethod() != null) {
     } else {
        = false;
     if (! && ( == null)) {
       throw new IllegalStateException(
           "DBAppender cannot function if the JDBC driver does not support getGeneratedKeys method *and* without a specific SQL dialect");
     // all nice and dandy on the eastern front


Returns the connectionSource.
     return ;


connectionSource The connectionSource to set.
   public void setConnectionSource(ConnectionSource connectionSource) {
     this. = connectionSource;
   public void append(E eventObject) {
     Connection connection = null;
     try {
       connection = .getConnection();
       PreparedStatement insertStatement;
       if () {
         String EVENT_ID_COL_NAME = "EVENT_ID";
         // see
           EVENT_ID_COL_NAME = EVENT_ID_COL_NAME.toLowerCase();
         insertStatement = connection.prepareStatement(getInsertSQL(),
            new String[] { EVENT_ID_COL_NAME });
      } else {
        insertStatement = connection.prepareStatement(getInsertSQL());
      long eventId;
      // inserting an event and getting the result must be exclusive
      synchronized (this) {
        eventId = selectEventId(insertStatementconnection);
      // we no longer need the insertStatement
    } catch (Throwable sqle) {
      addError("problem appending event"sqle);
    } finally {
  protected abstract void subAppend(E eventObjectConnection connection,
      PreparedStatement statementthrows Throwable;
  protected abstract void secondarySubAppend(E eventObjectConnection connection,
      long eventIdthrows Throwable;
  protected long selectEventId(PreparedStatement insertStatement,
      Connection connectionthrows SQLExceptionInvocationTargetException {
    ResultSet rs = null;
    Statement idStatement = null;
    boolean gotGeneratedKeys = false;
      try {
        rs = (ResultSetgetGeneratedKeysMethod().invoke(insertStatement,
            (Object[]) null);
        gotGeneratedKeys = true;
      } catch (InvocationTargetException ex) {
        Throwable target = ex.getTargetException();
        if (target instanceof SQLException) {
          throw (SQLExceptiontarget;
        throw ex;
      } catch (IllegalAccessException ex) {
            "IllegalAccessException invoking PreparedStatement.getGeneratedKeys",
    if (!gotGeneratedKeys) {
      idStatement = connection.createStatement();
      String selectInsertIdStr = .getSelectInsertId();
      rs = idStatement.executeQuery(selectInsertIdStr);
    // A ResultSet cursor is initially positioned before the first row;
    // the first call to the method next makes the first row the current row;
    long eventId = rs.getLong(1);
    return eventId;
  void close(Statement statementthrows SQLException {
    if (statement != null) {
  public void stop() {
New to GrepCode? Check out our FAQ X