Open Source Repository

Home /hibernate/hibernate-3.2.4.ga | Repository Home



org/hibernate/util/JDBCExceptionReporter.java
//$Id: JDBCExceptionReporter.java 10669 2006-10-31 21:24:28Z epbernard $
package org.hibernate.util;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLWarning;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class JDBCExceptionReporter {

  public static final Log log = LogFactory.getLog(JDBCExceptionReporter.class);
  public static final String DEFAULT_EXCEPTION_MSG = "SQL Exception";
  public static final String DEFAULT_WARNING_MSG = "SQL Warning";

  private JDBCExceptionReporter() {}
  
  public static void logAndClearWarnings(Connection connection) {
    if log.isWarnEnabled() ) {
      try {
        logWarningsconnection.getWarnings() );
      }
      catch (SQLException sqle) {
        //workaround for WebLogic
        log.debug("could not log warnings", sqle);
      }
    }
    try {
      //Sybase fail if we don't do that, sigh...
      connection.clearWarnings();
    }
    catch (SQLException sqle) {
      log.debug("could not clear warnings", sqle);
    }
  }

  public static void logWarnings(SQLWarning warning) {
    logWarnings(warning, null);
  }

  public static void logWarnings(SQLWarning warning, String message) {
    if log.isWarnEnabled() ) {
      if log.isDebugEnabled() && warning != null ) {
        message = StringHelper.isNotEmpty(message? message : DEFAULT_WARNING_MSG;
        log.debugmessage, warning );
      }
      while (warning != null) {
        StringBuffer buf = new StringBuffer(30)
                .append"SQL Warning: ")
            .appendwarning.getErrorCode() )
            .append", SQLState: ")
            .appendwarning.getSQLState() );
        log.warnbuf.toString() );
        log.warnwarning.getMessage() );
        warning = warning.getNextWarning();
      }
    }
  }

  public static void logExceptions(SQLException ex) {
    logExceptions(ex, null);
  }

  public static void logExceptions(SQLException ex, String message) {
    if log.isErrorEnabled() ) {
      if log.isDebugEnabled() ) {
        message = StringHelper.isNotEmpty(message? message : DEFAULT_EXCEPTION_MSG;
        log.debugmessage, ex );
      }
      while (ex != null) {
        StringBuffer buf = new StringBuffer(30)
            .append"SQL Error: " )
                .appendex.getErrorCode() )
                .append", SQLState: " )
                .appendex.getSQLState() );
        log.warnbuf.toString() );
        log.errorex.getMessage() );
        ex = ex.getNextException();
      }
    }
  }

//  public static JDBCException newJDBCException(String string, SQLException root, String sql) {
//    string = string + " [" + sql + ']';
//    log.error(string, root);
//    logExceptions(root);
//    return new JDBCException(string, root, sql);
//  }
//
//  public static JDBCException newJDBCException(String string, SQLException root) {
//    log.error(string, root);
//    logExceptions(root);
//    return new JDBCException(string, root);
//  }

}