1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jmonit.support.jdbc;
17
18 import java.sql.Connection;
19 import java.sql.ResultSet;
20 import java.sql.SQLException;
21 import java.sql.SQLWarning;
22 import java.sql.Statement;
23
24 import org.jmonit.Stopwatch;
25
26
27
28
29
30
31 public class MonitoredStatement
32 implements Statement
33 {
34
35 private Connection connection;
36
37
38 private Statement statement;
39
40
41 private JdbcMonitor monitor;
42
43
44
45
46
47
48 public MonitoredStatement( Statement statement, Connection connection, JdbcMonitor monitor )
49 {
50 super();
51 this.statement = statement;
52 this.connection = connection;
53 this.monitor = monitor;
54 }
55
56
57
58
59
60 protected SQLException handleException( SQLException sqle )
61 {
62 monitor.monitorSQLException( sqle );
63 return sqle;
64 }
65
66
67
68
69
70
71 public final void addBatch( String sql )
72 throws SQLException
73 {
74 statement.addBatch( sql );
75 }
76
77
78
79
80
81
82 public final void cancel()
83 throws SQLException
84 {
85 statement.cancel();
86 }
87
88
89
90
91
92
93 public final void clearBatch()
94 throws SQLException
95 {
96 statement.clearBatch();
97 }
98
99
100
101
102
103
104 public final void clearWarnings()
105 throws SQLException
106 {
107 statement.clearWarnings();
108 }
109
110
111
112
113
114
115 public final void close()
116 throws SQLException
117 {
118 statement.close();
119 }
120
121
122
123
124
125
126 public final boolean execute( String sql )
127 throws SQLException
128 {
129 Stopwatch stopwatch = getStopwatch( sql );
130 try
131 {
132 return statement.execute( sql );
133 }
134 catch ( SQLException sqle )
135 {
136 throw handleException( sqle );
137 }
138 finally
139 {
140 stopwatch.stop();
141 }
142 }
143
144
145
146
147
148
149 public final int[] executeBatch()
150 throws SQLException
151 {
152 Stopwatch stopwatch = Stopwatch.start( monitor.getStatementMonitor( "executeBatch" ) );
153 try
154 {
155 return statement.executeBatch();
156 }
157 catch ( SQLException sqle )
158 {
159 throw handleException( sqle );
160 }
161 finally
162 {
163 stopwatch.stop();
164 }
165 }
166
167
168
169
170
171
172 public final ResultSet executeQuery( String sql )
173 throws SQLException
174 {
175 Stopwatch stopwatch = getStopwatch( sql );
176 try
177 {
178 return statement.executeQuery( sql );
179 }
180 catch ( SQLException sqle )
181 {
182 throw handleException( sqle );
183 }
184 finally
185 {
186 stopwatch.stop();
187 }
188 }
189
190
191
192
193
194
195 public final int executeUpdate( String sql )
196 throws SQLException
197 {
198 Stopwatch stopwatch = getStopwatch( sql );
199 try
200 {
201 return statement.executeUpdate( sql );
202 }
203 catch ( SQLException sqle )
204 {
205 throw handleException( sqle );
206 }
207 finally
208 {
209 stopwatch.stop();
210 }
211 }
212
213
214
215
216
217
218 public final Connection getConnection()
219 throws SQLException
220 {
221 return connection;
222 }
223
224
225
226
227
228
229 public final int getFetchDirection()
230 throws SQLException
231 {
232 return statement.getFetchDirection();
233 }
234
235
236
237
238
239
240 public final int getFetchSize()
241 throws SQLException
242 {
243 return statement.getFetchSize();
244 }
245
246
247
248
249
250
251 public final int getMaxFieldSize()
252 throws SQLException
253 {
254 return statement.getMaxFieldSize();
255 }
256
257
258
259
260
261
262 public final int getMaxRows()
263 throws SQLException
264 {
265 return statement.getMaxRows();
266 }
267
268
269
270
271
272
273 public final boolean getMoreResults()
274 throws SQLException
275 {
276 return statement.getMoreResults();
277 }
278
279
280
281
282
283
284 public final int getQueryTimeout()
285 throws SQLException
286 {
287 return statement.getQueryTimeout();
288 }
289
290
291
292
293
294
295 public final ResultSet getResultSet()
296 throws SQLException
297 {
298 return statement.getResultSet();
299 }
300
301
302
303
304
305
306 public final int getResultSetConcurrency()
307 throws SQLException
308 {
309 return statement.getResultSetConcurrency();
310 }
311
312
313
314
315
316
317 public final int getResultSetType()
318 throws SQLException
319 {
320 return statement.getResultSetType();
321 }
322
323
324
325
326
327
328 public final int getUpdateCount()
329 throws SQLException
330 {
331 return statement.getUpdateCount();
332 }
333
334
335
336
337
338
339 public final SQLWarning getWarnings()
340 throws SQLException
341 {
342 return statement.getWarnings();
343 }
344
345
346
347
348
349
350 public final void setCursorName( String name )
351 throws SQLException
352 {
353 statement.setCursorName( name );
354 }
355
356
357
358
359
360
361 public final void setEscapeProcessing( boolean enable )
362 throws SQLException
363 {
364 statement.setEscapeProcessing( enable );
365 }
366
367
368
369
370
371
372 public final void setFetchDirection( int direction )
373 throws SQLException
374 {
375 statement.setFetchDirection( direction );
376 }
377
378
379
380
381
382
383 public final void setFetchSize( int rows )
384 throws SQLException
385 {
386 statement.setFetchSize( rows );
387 }
388
389
390
391
392
393
394 public final void setMaxFieldSize( int max )
395 throws SQLException
396 {
397 statement.setMaxFieldSize( max );
398 }
399
400
401
402
403
404
405 public final void setMaxRows( int max )
406 throws SQLException
407 {
408 statement.setMaxRows( max );
409 }
410
411
412
413
414
415
416 public final void setQueryTimeout( int seconds )
417 throws SQLException
418 {
419 statement.setQueryTimeout( seconds );
420 }
421
422
423
424
425
426
427
428
429 public final boolean execute( String sql, int autoGeneratedKeys )
430 throws SQLException
431 {
432 Stopwatch stopwatch = getStopwatch( sql );
433 try
434 {
435 return statement.execute( sql, autoGeneratedKeys );
436 }
437 catch ( SQLException sqle )
438 {
439 throw handleException( sqle );
440 }
441 finally
442 {
443 stopwatch.stop();
444 }
445 }
446
447 private Stopwatch getStopwatch( String sql )
448 {
449 return Stopwatch.start( monitor.getStatementMonitor( sql ) );
450 }
451
452
453
454
455
456
457 public final boolean execute( String sql, int[] columnIndexes )
458 throws SQLException
459 {
460 Stopwatch stopwatch = getStopwatch( sql );
461 try
462 {
463 return statement.execute( sql, columnIndexes );
464 }
465 catch ( SQLException sqle )
466 {
467 throw handleException( sqle );
468 }
469 finally
470 {
471 stopwatch.stop();
472 }
473 }
474
475
476
477
478
479
480 public final boolean execute( String sql, String[] columnNames )
481 throws SQLException
482 {
483 Stopwatch stopwatch = getStopwatch( sql );
484 try
485 {
486 return statement.execute( sql, columnNames );
487 }
488 catch ( SQLException sqle )
489 {
490 throw handleException( sqle );
491 }
492 finally
493 {
494 stopwatch.stop();
495 }
496 }
497
498
499
500
501
502
503 public final int executeUpdate( String sql, int autoGeneratedKeys )
504 throws SQLException
505 {
506 Stopwatch stopwatch = getStopwatch( sql );
507 try
508 {
509 return statement.executeUpdate( sql, autoGeneratedKeys );
510 }
511 catch ( SQLException sqle )
512 {
513 throw handleException( sqle );
514 }
515 finally
516 {
517 stopwatch.stop();
518 }
519 }
520
521
522
523
524
525
526 public final int executeUpdate( String sql, int[] columnIndexes )
527 throws SQLException
528 {
529 Stopwatch stopwatch = getStopwatch( sql );
530 try
531 {
532 return statement.executeUpdate( sql, columnIndexes );
533 }
534 catch ( SQLException sqle )
535 {
536 throw handleException( sqle );
537 }
538 finally
539 {
540 stopwatch.stop();
541 }
542 }
543
544
545
546
547
548
549
550 public final int executeUpdate( String sql, String[] columnNames )
551 throws SQLException
552 {
553 Stopwatch stopwatch = getStopwatch( sql );
554 try
555 {
556 return statement.executeUpdate( sql, columnNames );
557 }
558 catch ( SQLException sqle )
559 {
560 throw handleException( sqle );
561 }
562 finally
563 {
564 stopwatch.stop();
565 }
566 }
567
568
569
570
571
572
573 public final ResultSet getGeneratedKeys()
574 throws SQLException
575 {
576 return statement.getGeneratedKeys();
577 }
578
579
580
581
582
583
584 public final boolean getMoreResults( int current )
585 throws SQLException
586 {
587 return statement.getMoreResults( current );
588 }
589
590
591
592
593
594
595 public final int getResultSetHoldability()
596 throws SQLException
597 {
598 return statement.getResultSetHoldability();
599 }
600
601 public final boolean isClosed()
602 throws SQLException
603 {
604 return statement.isClosed();
605 }
606
607 public final boolean isPoolable()
608 throws SQLException
609 {
610 return statement.isPoolable();
611 }
612
613 public final boolean isWrapperFor( Class<?> iface )
614 throws SQLException
615 {
616 return statement.isWrapperFor( iface );
617 }
618
619 public final void setPoolable( boolean poolable )
620 throws SQLException
621 {
622 statement.setPoolable( poolable );
623 }
624
625 public final <T> T unwrap( Class<T> iface )
626 throws SQLException
627 {
628 return statement.unwrap( iface );
629 }
630 }