44 */
55package org .hibernate .engine .jdbc .spi ;
66
7- import java .lang .invoke .MethodHandles ;
87import java .sql .Connection ;
98import java .sql .SQLException ;
109import java .sql .SQLWarning ;
1615import org .hibernate .exception .internal .SQLStateConversionDelegate ;
1716import org .hibernate .exception .internal .StandardSQLExceptionConverter ;
1817import org .hibernate .exception .spi .SQLExceptionConverter ;
19- import org .hibernate .internal .CoreMessageLogger ;
2018
21- import org .jboss .logging .Logger ;
19+ import static org .hibernate .engine .jdbc .spi .SQLExceptionLogging .ERROR_LOG ;
20+ import static org .hibernate .engine .jdbc .spi .SQLExceptionLogging .WARNING_LOG ;
2221import org .jboss .logging .Logger .Level ;
2322
2423/**
2726 * @author Steve Ebersole
2827 */
2928public class SqlExceptionHelper {
30- private static final CoreMessageLogger LOG = Logger .getMessageLogger (
31- MethodHandles .lookup (),
32- CoreMessageLogger .class ,
33- SqlExceptionHelper .class .getName ()
34- );
3529
3630 private final boolean logWarnings ;
3731 private final boolean logErrors ;
@@ -142,18 +136,18 @@ public JDBCException convert(SQLException sqlException, Supplier<String> message
142136 */
143137 public void logExceptions (SQLException sqlException , String message ) {
144138 if ( logErrors ) {
145- if ( LOG .isEnabled ( Level .WARN ) ) {
139+ if ( ERROR_LOG .isEnabled ( Level .WARN ) ) {
146140 SQLException currentException = sqlException ;
147141 while ( currentException != null ) {
148142 if ( !isDuplicate ( currentException , sqlException ) ) {
149- LOG . warn ( errorCodeMessage ( sqlException ) );
150- LOG .warn ( sqlException .getMessage () );
143+ ERROR_LOG . logErrorCodes ( sqlException . getErrorCode (), sqlException . getSQLState ( ) );
144+ ERROR_LOG .warn ( sqlException .getMessage () );
151145 }
152146 currentException = currentException .getNextException ();
153147 }
154148 }
155- if ( LOG .isDebugEnabled () ) {
156- LOG .debug ( message , sqlException );
149+ if ( ERROR_LOG .isDebugEnabled () ) {
150+ ERROR_LOG .debug ( message , sqlException );
157151 }
158152 }
159153 }
@@ -175,18 +169,10 @@ private static boolean isDuplicate(SQLException currentException, SQLException b
175169 return false ;
176170 }
177171
178- private static String errorCodeMessage (SQLException sqlException ) {
179- return "ErrorCode: "
180- + sqlException .getErrorCode ()
181- + ", SQLState: "
182- + sqlException .getSQLState ()
183- + ( sqlException instanceof SQLWarning ? " [warning]" : " [error]" );
184- }
185-
186172 // SQLWarning ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187173
188174 /**
189- * Contract for handling {@linkplain SQLWarning warnings}
175+ * Contract for handling {@linkplain SQLWarning warnings}.
190176 */
191177 public interface WarningHandler {
192178 /**
@@ -218,8 +204,11 @@ public interface WarningHandler {
218204 */
219205 public abstract static class WarningHandlerLoggingSupport implements WarningHandler {
220206 @ Override
221- public final void handleWarning (SQLWarning warning ) {
222- logWarning ( errorCodeMessage ( warning ), warning .getMessage () );
207+ public void handleWarning (SQLWarning warning ) {
208+ logWarning (
209+ "SQL Warning Code: " + warning .getErrorCode () + ", SQLState: " + warning .getSQLState (),
210+ warning .getMessage ()
211+ );
223212 }
224213
225214 /**
@@ -248,18 +237,24 @@ public StandardWarningHandler(String introMessage) {
248237
249238 @ Override
250239 public boolean doProcess () {
251- return LOG .isEnabled ( Level .WARN );
240+ return WARNING_LOG .isEnabled ( Level .WARN );
252241 }
253242
254243 @ Override
255244 public void prepare (SQLWarning warning ) {
256- LOG .debug ( introMessage , warning );
245+ WARNING_LOG .debug ( introMessage , warning );
246+ }
247+
248+ @ Override
249+ public final void handleWarning (SQLWarning warning ) {
250+ WARNING_LOG .logErrorCodes ( warning .getErrorCode (), warning .getSQLState () );
251+ WARNING_LOG .warn ( warning .getMessage () );
257252 }
258253
259254 @ Override
260255 protected void logWarning (String description , String message ) {
261- LOG .warn ( description );
262- LOG .warn ( message );
256+ WARNING_LOG .warn ( description );
257+ WARNING_LOG .warn ( message );
263258 }
264259 }
265260
@@ -275,9 +270,7 @@ protected void logWarning(String description, String message) {
275270 * @param warning The warning to walk
276271 * @param handler The handler
277272 */
278- public void walkWarnings (
279- SQLWarning warning ,
280- WarningHandler handler ) {
273+ public void walkWarnings (SQLWarning warning , WarningHandler handler ) {
281274 if ( warning != null && handler .doProcess () ) {
282275 handler .prepare ( warning );
283276 while ( warning != null ) {
@@ -310,24 +303,22 @@ public void logAndClearWarnings(Statement statement) {
310303 *
311304 * @see #walkWarnings
312305 */
313- public void handleAndClearWarnings (
314- Connection connection ,
315- WarningHandler handler ) {
306+ public void handleAndClearWarnings (Connection connection , WarningHandler handler ) {
316307 try {
317308 if ( logWarnings ) {
318309 walkWarnings ( connection .getWarnings (), handler );
319310 }
320311 }
321312 catch (SQLException sqle ) {
322313 // workaround for WebLogic
323- LOG .debug ( "could not log warnings" , sqle );
314+ WARNING_LOG .debug ( "could not log warnings" , sqle );
324315 }
325316 try {
326317 // Sybase fail if we don't do that, sigh...
327318 connection .clearWarnings ();
328319 }
329320 catch (SQLException sqle ) {
330- LOG .debug ( "could not clear warnings" , sqle );
321+ WARNING_LOG .debug ( "could not clear warnings" , sqle );
331322 }
332323 }
333324
@@ -339,9 +330,7 @@ public void handleAndClearWarnings(
339330 *
340331 * @see #walkWarnings
341332 */
342- public void handleAndClearWarnings (
343- Statement statement ,
344- WarningHandler handler ) {
333+ public void handleAndClearWarnings (Statement statement , WarningHandler handler ) {
345334 // See HHH-9174. Statement.getWarnings() can be an expensive call for some JDBC drivers.
346335 // Don't do it unless the log level would actually allow a warning to be logged.
347336 if ( logWarnings ) {
@@ -350,15 +339,15 @@ public void handleAndClearWarnings(
350339 }
351340 catch (SQLException sqlException ) {
352341 // workaround for WebLogic
353- LOG .debug ( "could not log warnings" , sqlException );
342+ WARNING_LOG .debug ( "could not log warnings" , sqlException );
354343 }
355344 }
356345 try {
357346 // Sybase fail if we don't do that, sigh...
358347 statement .clearWarnings ();
359348 }
360349 catch (SQLException sqle ) {
361- LOG .debug ( "could not clear warnings" , sqle );
350+ WARNING_LOG .debug ( "could not clear warnings" , sqle );
362351 }
363352 }
364353}
0 commit comments