View Javadoc

1   /*
2    ~ Copyright 2006-2007 Nicolas De Loof.
3    ~
4    ~ Licensed under the Apache License, Version 2.0 (the "License");
5    ~ you may not use this file except in compliance with the License.
6    ~ You may obtain a copy of the License at
7    ~
8    ~      http://www.apache.org/licenses/LICENSE-2.0
9    ~
10   ~ Unless required by applicable law or agreed to in writing, software
11   ~ distributed under the License is distributed on an "AS IS" BASIS,
12   ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   ~ See the License for the specific language governing permissions and
14   ~ limitations under the License.
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   * Wrapped to monitor JDBC Statement
28   * 
29   * @author <a href="mailto:ndeloof@sourceforge.net">Nicolas De Loof</a>
30   */
31  public class MonitoredStatement
32      implements Statement
33  {
34      /** Monitored connection */
35      private Connection connection;
36  
37      /** delegate statement */
38      private Statement statement;
39  
40      /** The monitor used on this connection */
41      private JdbcMonitor monitor;
42  
43      /**
44       * @param statement target statement
45       * @param connection monitored connection
46       * @param monitor the JDBC monitor on this connection
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       * @param sqle catched exception
58       * @return SQLException exception to throw
59       */
60      protected SQLException handleException( SQLException sqle )
61      {
62          monitor.monitorSQLException( sqle );
63          return sqle;
64      }
65  
66      /**
67       * {@inheritDoc}
68       * 
69       * @see java.sql.Statement#addBatch(java.lang.String)
70       */
71      public final void addBatch( String sql )
72          throws SQLException
73      {
74          statement.addBatch( sql );
75      }
76  
77      /**
78       * {@inheritDoc}
79       * 
80       * @see java.sql.Statement#cancel()
81       */
82      public final void cancel()
83          throws SQLException
84      {
85          statement.cancel();
86      }
87  
88      /**
89       * {@inheritDoc}
90       * 
91       * @see java.sql.Statement#clearBatch()
92       */
93      public final void clearBatch()
94          throws SQLException
95      {
96          statement.clearBatch();
97      }
98  
99      /**
100      * {@inheritDoc}
101      * 
102      * @see java.sql.Statement#clearWarnings()
103      */
104     public final void clearWarnings()
105         throws SQLException
106     {
107         statement.clearWarnings();
108     }
109 
110     /**
111      * {@inheritDoc}
112      * 
113      * @see java.sql.Statement#close()
114      */
115     public final void close()
116         throws SQLException
117     {
118         statement.close();
119     }
120 
121     /**
122      * {@inheritDoc}
123      * 
124      * @see java.sql.Statement#execute(java.lang.String)
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      * {@inheritDoc}
146      * 
147      * @see java.sql.Statement#executeBatch()
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      * {@inheritDoc}
169      * 
170      * @see java.sql.Statement#executeQuery(java.lang.String)
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      * {@inheritDoc}
192      * 
193      * @see java.sql.Statement#executeUpdate(java.lang.String)
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      * {@inheritDoc}
215      * 
216      * @see java.sql.Statement#getConnection()
217      */
218     public final Connection getConnection()
219         throws SQLException
220     {
221         return connection;
222     }
223 
224     /**
225      * {@inheritDoc}
226      * 
227      * @see java.sql.Statement#getFetchDirection()
228      */
229     public final int getFetchDirection()
230         throws SQLException
231     {
232         return statement.getFetchDirection();
233     }
234 
235     /**
236      * {@inheritDoc}
237      * 
238      * @see java.sql.Statement#getFetchSize()
239      */
240     public final int getFetchSize()
241         throws SQLException
242     {
243         return statement.getFetchSize();
244     }
245 
246     /**
247      * {@inheritDoc}
248      * 
249      * @see java.sql.Statement#getMaxFieldSize()
250      */
251     public final int getMaxFieldSize()
252         throws SQLException
253     {
254         return statement.getMaxFieldSize();
255     }
256 
257     /**
258      * {@inheritDoc}
259      * 
260      * @see java.sql.Statement#getMaxRows()
261      */
262     public final int getMaxRows()
263         throws SQLException
264     {
265         return statement.getMaxRows();
266     }
267 
268     /**
269      * {@inheritDoc}
270      * 
271      * @see java.sql.Statement#getMoreResults()
272      */
273     public final boolean getMoreResults()
274         throws SQLException
275     {
276         return statement.getMoreResults();
277     }
278 
279     /**
280      * {@inheritDoc}
281      * 
282      * @see java.sql.Statement#getQueryTimeout()
283      */
284     public final int getQueryTimeout()
285         throws SQLException
286     {
287         return statement.getQueryTimeout();
288     }
289 
290     /**
291      * {@inheritDoc}
292      * 
293      * @see java.sql.Statement#getResultSet()
294      */
295     public final ResultSet getResultSet()
296         throws SQLException
297     {
298         return statement.getResultSet();
299     }
300 
301     /**
302      * {@inheritDoc}
303      * 
304      * @see java.sql.Statement#getResultSetConcurrency()
305      */
306     public final int getResultSetConcurrency()
307         throws SQLException
308     {
309         return statement.getResultSetConcurrency();
310     }
311 
312     /**
313      * {@inheritDoc}
314      * 
315      * @see java.sql.Statement#getResultSetType()
316      */
317     public final int getResultSetType()
318         throws SQLException
319     {
320         return statement.getResultSetType();
321     }
322 
323     /**
324      * {@inheritDoc}
325      * 
326      * @see java.sql.Statement#getUpdateCount()
327      */
328     public final int getUpdateCount()
329         throws SQLException
330     {
331         return statement.getUpdateCount();
332     }
333 
334     /**
335      * {@inheritDoc}
336      * 
337      * @see java.sql.Statement#getWarnings()
338      */
339     public final SQLWarning getWarnings()
340         throws SQLException
341     {
342         return statement.getWarnings();
343     }
344 
345     /**
346      * {@inheritDoc}
347      * 
348      * @see java.sql.Statement#setCursorName(java.lang.String)
349      */
350     public final void setCursorName( String name )
351         throws SQLException
352     {
353         statement.setCursorName( name );
354     }
355 
356     /**
357      * {@inheritDoc}
358      * 
359      * @see java.sql.Statement#setEscapeProcessing(boolean)
360      */
361     public final void setEscapeProcessing( boolean enable )
362         throws SQLException
363     {
364         statement.setEscapeProcessing( enable );
365     }
366 
367     /**
368      * {@inheritDoc}
369      * 
370      * @see java.sql.Statement#setFetchDirection(int)
371      */
372     public final void setFetchDirection( int direction )
373         throws SQLException
374     {
375         statement.setFetchDirection( direction );
376     }
377 
378     /**
379      * {@inheritDoc}
380      * 
381      * @see java.sql.Statement#setFetchSize(int)
382      */
383     public final void setFetchSize( int rows )
384         throws SQLException
385     {
386         statement.setFetchSize( rows );
387     }
388 
389     /**
390      * {@inheritDoc}
391      * 
392      * @see java.sql.Statement#setMaxFieldSize(int)
393      */
394     public final void setMaxFieldSize( int max )
395         throws SQLException
396     {
397         statement.setMaxFieldSize( max );
398     }
399 
400     /**
401      * {@inheritDoc}
402      * 
403      * @see java.sql.Statement#setMaxRows(int)
404      */
405     public final void setMaxRows( int max )
406         throws SQLException
407     {
408         statement.setMaxRows( max );
409     }
410 
411     /**
412      * {@inheritDoc}
413      * 
414      * @see java.sql.Statement#setQueryTimeout(int)
415      */
416     public final void setQueryTimeout( int seconds )
417         throws SQLException
418     {
419         statement.setQueryTimeout( seconds );
420     }
421 
422     // JDBC 3
423 
424     /**
425      * {@inheritDoc}
426      * 
427      * @see java.sql.Statement#execute(java.lang.String, int)
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      * {@inheritDoc}
454      * 
455      * @see java.sql.Statement#execute(java.lang.String, int[])
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      * {@inheritDoc}
477      * 
478      * @see java.sql.Statement#execute(java.lang.String, java.lang.String[])
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      * {@inheritDoc}
500      * 
501      * @see java.sql.Statement#executeUpdate(java.lang.String, int)
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      * {@inheritDoc}
523      * 
524      * @see java.sql.Statement#executeUpdate(java.lang.String, int[])
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      * {@inheritDoc}
546      * 
547      * @see java.sql.Statement#executeUpdate(java.lang.String,
548      * java.lang.String[])
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      * {@inheritDoc}
570      * 
571      * @see java.sql.Statement#getGeneratedKeys()
572      */
573     public final ResultSet getGeneratedKeys()
574         throws SQLException
575     {
576         return statement.getGeneratedKeys();
577     }
578 
579     /**
580      * {@inheritDoc}
581      * 
582      * @see java.sql.Statement#getMoreResults(int)
583      */
584     public final boolean getMoreResults( int current )
585         throws SQLException
586     {
587         return statement.getMoreResults( current );
588     }
589 
590     /**
591      * {@inheritDoc}
592      * 
593      * @see java.sql.Statement#getResultSetHoldability()
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 }