KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > util > JDBCExceptionReporter


1 //$Id: JDBCExceptionReporter.java,v 1.6 2005/04/23 07:25:27 oneovthafew Exp $
2
package org.hibernate.util;
3
4 import java.sql.Connection JavaDoc;
5 import java.sql.SQLException JavaDoc;
6 import java.sql.SQLWarning JavaDoc;
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 JavaDoc DEFAULT_EXCEPTION_MSG = "SQL Exception";
15     public static final String JavaDoc DEFAULT_WARNING_MSG = "SQL Warning";
16
17     private JDBCExceptionReporter() {}
18     
19     public static void logAndClearWarnings(Connection JavaDoc connection) {
20         if ( log.isWarnEnabled() ) {
21             try {
22                 logWarnings( connection.getWarnings() );
23                 connection.clearWarnings();
24             }
25             catch (SQLException JavaDoc sqle) {
26                 //workaround for WebLogic
27
log.debug("could not log warnings", sqle);
28             }
29         }
30     }
31
32     public static void logWarnings(SQLWarning JavaDoc warning) {
33         logWarnings(warning, null);
34     }
35
36     public static void logWarnings(SQLWarning JavaDoc warning, String JavaDoc 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 JavaDoc buf = new StringBuffer JavaDoc(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 JavaDoc ex) {
56         logExceptions(ex, null);
57     }
58
59     public static void logExceptions(SQLException JavaDoc ex, String JavaDoc 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 JavaDoc buf = new StringBuffer JavaDoc(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 // public static JDBCException newJDBCException(String string, SQLException root, String sql) {
79
// string = string + " [" + sql + ']';
80
// log.error(string, root);
81
// logExceptions(root);
82
// return new JDBCException(string, root, sql);
83
// }
84
//
85
// public static JDBCException newJDBCException(String string, SQLException root) {
86
// log.error(string, root);
87
// logExceptions(root);
88
// return new JDBCException(string, root);
89
// }
90

91 }
92
93
94
95
96
97
98
Popular Tags