4
4
*/
5
5
package org .hibernate .engine .jdbc .spi ;
6
6
7
- import java .lang .invoke .MethodHandles ;
8
7
import java .sql .Connection ;
9
8
import java .sql .SQLException ;
10
9
import java .sql .SQLWarning ;
16
15
import org .hibernate .exception .internal .SQLStateConversionDelegate ;
17
16
import org .hibernate .exception .internal .StandardSQLExceptionConverter ;
18
17
import org .hibernate .exception .spi .SQLExceptionConverter ;
19
- import org .hibernate .internal .CoreMessageLogger ;
20
18
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 ;
22
21
import org .jboss .logging .Logger .Level ;
23
22
24
23
/**
27
26
* @author Steve Ebersole
28
27
*/
29
28
public class SqlExceptionHelper {
30
- private static final CoreMessageLogger LOG = Logger .getMessageLogger (
31
- MethodHandles .lookup (),
32
- CoreMessageLogger .class ,
33
- SqlExceptionHelper .class .getName ()
34
- );
35
29
36
30
private final boolean logWarnings ;
37
31
private final boolean logErrors ;
@@ -142,18 +136,18 @@ public JDBCException convert(SQLException sqlException, Supplier<String> message
142
136
*/
143
137
public void logExceptions (SQLException sqlException , String message ) {
144
138
if ( logErrors ) {
145
- if ( LOG .isEnabled ( Level .WARN ) ) {
139
+ if ( ERROR_LOG .isEnabled ( Level .WARN ) ) {
146
140
SQLException currentException = sqlException ;
147
141
while ( currentException != null ) {
148
142
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 () );
151
145
}
152
146
currentException = currentException .getNextException ();
153
147
}
154
148
}
155
- if ( LOG .isDebugEnabled () ) {
156
- LOG .debug ( message , sqlException );
149
+ if ( ERROR_LOG .isDebugEnabled () ) {
150
+ ERROR_LOG .debug ( message , sqlException );
157
151
}
158
152
}
159
153
}
@@ -175,18 +169,10 @@ private static boolean isDuplicate(SQLException currentException, SQLException b
175
169
return false ;
176
170
}
177
171
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
-
186
172
// SQLWarning ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187
173
188
174
/**
189
- * Contract for handling {@linkplain SQLWarning warnings}
175
+ * Contract for handling {@linkplain SQLWarning warnings}.
190
176
*/
191
177
public interface WarningHandler {
192
178
/**
@@ -218,8 +204,11 @@ public interface WarningHandler {
218
204
*/
219
205
public abstract static class WarningHandlerLoggingSupport implements WarningHandler {
220
206
@ 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
+ );
223
212
}
224
213
225
214
/**
@@ -248,18 +237,24 @@ public StandardWarningHandler(String introMessage) {
248
237
249
238
@ Override
250
239
public boolean doProcess () {
251
- return LOG .isEnabled ( Level .WARN );
240
+ return WARNING_LOG .isEnabled ( Level .WARN );
252
241
}
253
242
254
243
@ Override
255
244
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 () );
257
252
}
258
253
259
254
@ Override
260
255
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 );
263
258
}
264
259
}
265
260
@@ -275,9 +270,7 @@ protected void logWarning(String description, String message) {
275
270
* @param warning The warning to walk
276
271
* @param handler The handler
277
272
*/
278
- public void walkWarnings (
279
- SQLWarning warning ,
280
- WarningHandler handler ) {
273
+ public void walkWarnings (SQLWarning warning , WarningHandler handler ) {
281
274
if ( warning != null && handler .doProcess () ) {
282
275
handler .prepare ( warning );
283
276
while ( warning != null ) {
@@ -310,24 +303,22 @@ public void logAndClearWarnings(Statement statement) {
310
303
*
311
304
* @see #walkWarnings
312
305
*/
313
- public void handleAndClearWarnings (
314
- Connection connection ,
315
- WarningHandler handler ) {
306
+ public void handleAndClearWarnings (Connection connection , WarningHandler handler ) {
316
307
try {
317
308
if ( logWarnings ) {
318
309
walkWarnings ( connection .getWarnings (), handler );
319
310
}
320
311
}
321
312
catch (SQLException sqle ) {
322
313
// workaround for WebLogic
323
- LOG .debug ( "could not log warnings" , sqle );
314
+ WARNING_LOG .debug ( "could not log warnings" , sqle );
324
315
}
325
316
try {
326
317
// Sybase fail if we don't do that, sigh...
327
318
connection .clearWarnings ();
328
319
}
329
320
catch (SQLException sqle ) {
330
- LOG .debug ( "could not clear warnings" , sqle );
321
+ WARNING_LOG .debug ( "could not clear warnings" , sqle );
331
322
}
332
323
}
333
324
@@ -339,9 +330,7 @@ public void handleAndClearWarnings(
339
330
*
340
331
* @see #walkWarnings
341
332
*/
342
- public void handleAndClearWarnings (
343
- Statement statement ,
344
- WarningHandler handler ) {
333
+ public void handleAndClearWarnings (Statement statement , WarningHandler handler ) {
345
334
// See HHH-9174. Statement.getWarnings() can be an expensive call for some JDBC drivers.
346
335
// Don't do it unless the log level would actually allow a warning to be logged.
347
336
if ( logWarnings ) {
@@ -350,15 +339,15 @@ public void handleAndClearWarnings(
350
339
}
351
340
catch (SQLException sqlException ) {
352
341
// workaround for WebLogic
353
- LOG .debug ( "could not log warnings" , sqlException );
342
+ WARNING_LOG .debug ( "could not log warnings" , sqlException );
354
343
}
355
344
}
356
345
try {
357
346
// Sybase fail if we don't do that, sigh...
358
347
statement .clearWarnings ();
359
348
}
360
349
catch (SQLException sqle ) {
361
- LOG .debug ( "could not clear warnings" , sqle );
350
+ WARNING_LOG .debug ( "could not clear warnings" , sqle );
362
351
}
363
352
}
364
353
}
0 commit comments