1 package org.hibernate.util; 3 4 import java.sql.Connection ; 5 import java.sql.SQLException ; 6 import java.sql.SQLWarning ; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 public final class JDBCExceptionReporter { 12 13 public static final Log log = LogFactory.getLog(JDBCExceptionReporter.class); 14 public static final String DEFAULT_EXCEPTION_MSG = "SQL Exception"; 15 public static final String DEFAULT_WARNING_MSG = "SQL Warning"; 16 17 private JDBCExceptionReporter() {} 18 19 public static void logAndClearWarnings(Connection connection) { 20 if ( log.isWarnEnabled() ) { 21 try { 22 logWarnings( connection.getWarnings() ); 23 connection.clearWarnings(); 24 } 25 catch (SQLException sqle) { 26 log.debug("could not log warnings", sqle); 28 } 29 } 30 } 31 32 public static void logWarnings(SQLWarning warning) { 33 logWarnings(warning, null); 34 } 35 36 public static void logWarnings(SQLWarning warning, String message) { 37 if ( log.isWarnEnabled() ) { 38 if ( log.isDebugEnabled() && warning != null ) { 39 message = StringHelper.isNotEmpty(message) ? message : DEFAULT_WARNING_MSG; 40 log.debug( message, warning ); 41 } 42 while (warning != null) { 43 StringBuffer buf = new StringBuffer (30) 44 .append( "SQL Warning: ") 45 .append( warning.getErrorCode() ) 46 .append( ", SQLState: ") 47 .append( warning.getSQLState() ); 48 log.warn( buf.toString() ); 49 log.warn( warning.getMessage() ); 50 warning = warning.getNextWarning(); 51 } 52 } 53 } 54 55 public static void logExceptions(SQLException ex) { 56 logExceptions(ex, null); 57 } 58 59 public static void logExceptions(SQLException ex, String message) { 60 if ( log.isErrorEnabled() ) { 61 if ( log.isDebugEnabled() ) { 62 message = StringHelper.isNotEmpty(message) ? message : DEFAULT_EXCEPTION_MSG; 63 log.debug( message, ex ); 64 } 65 while (ex != null) { 66 StringBuffer buf = new StringBuffer (30) 67 .append( "SQL Error: " ) 68 .append( ex.getErrorCode() ) 69 .append( ", SQLState: " ) 70 .append( ex.getSQLState() ); 71 log.warn( buf.toString() ); 72 log.error( ex.getMessage() ); 73 ex = ex.getNextException(); 74 } 75 } 76 } 77 78 91 } 92 93 94 95 96 97 98 | Popular Tags |