KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > craftsman > spy > SpyConnection


1 /*
2  * Craftsman Spy.
3  * Copyright (C) 2005 Sébastien LECACHEUR
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */

19 package craftsman.spy;
20
21 import java.sql.CallableStatement JavaDoc;
22 import java.sql.Connection JavaDoc;
23 import java.sql.DatabaseMetaData JavaDoc;
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.SQLWarning JavaDoc;
28 import java.sql.Savepoint JavaDoc;
29 import java.sql.Statement JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 /**
36  * This classe represents the JDBC connection to the database.
37  *
38  * @author Sébastien LECACHEUR
39  */

40 public class SpyConnection implements Connection JavaDoc {
41     /**
42      * Internal used logger.
43      */

44     protected Log log = LogFactory.getLog(this.getClass().getName());
45
46
47     /**
48      * The real JDBC connection instance.
49      */

50     private Connection JavaDoc real = null;
51
52
53     /**
54      * The JDBC connection ID.
55      */

56     private int id = 0;
57
58
59     /**
60      * Constructs a new Spy JDBC connection.
61      * @param con Connection The real JDBC connection.
62      * @param conIdSql String The SQL string to execute
63      * in order to retrieve the real JDBC connection ID.
64      */

65     protected SpyConnection ( Connection JavaDoc con, String JavaDoc conIdSql) {
66         real = con;
67         id = hashCode();
68
69         if ( conIdSql!=null) {
70             try {
71                 Statement JavaDoc s = con.createStatement();
72                 ResultSet JavaDoc r = s.executeQuery(conIdSql);
73                 if ( r.next()) {
74                     id = r.getInt(1);
75                 }
76                 r.close();
77                 s.close();
78             } catch ( SQLException JavaDoc e) {
79                 e.printStackTrace();
80             }
81         }
82     }
83
84
85     /**
86      * Returns the used JDBC connection identifier.
87      * @return int The JDBC connection SPID or hash code
88      * otherwise <code>0</code>.
89      */

90     protected int getId() {
91         return id;
92     }
93
94
95     /**
96      * @see Connection#getHoldability()
97      */

98     public int getHoldability() throws SQLException JavaDoc {
99         return real.getHoldability();
100     }
101
102
103     /**
104      * @see Connection#getTransactionIsolation()
105      */

106     public int getTransactionIsolation() throws SQLException JavaDoc {
107         return real.getTransactionIsolation();
108     }
109
110
111     /**
112      * @see Connection#clearWarnings()
113      */

114     public void clearWarnings() throws SQLException JavaDoc {
115         real.clearWarnings();
116     }
117
118
119     /**
120      * @see Connection#close()
121      */

122     public void close() throws SQLException JavaDoc {
123         real.close();
124     }
125
126
127     /**
128      * @see Connection#commit()
129      */

130     public void commit() throws SQLException JavaDoc {
131         long end, start = System.currentTimeMillis();
132
133
134         try {
135             real.commit();
136             end = System.currentTimeMillis();
137             if ( log.isInfoEnabled()) log.info(getId()+":commit succeed ("+(end-start)+" ms)");
138         } catch ( SQLException JavaDoc e) {
139             end = System.currentTimeMillis();
140             if ( log.isErrorEnabled()) log.error(getId()+":commit failed state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
141             throw e;
142         }
143     }
144
145
146     /**
147      * @see Connection#rollback()
148      */

149     public void rollback() throws SQLException JavaDoc {
150         long end, start = System.currentTimeMillis();
151
152
153         try {
154             real.rollback();
155             end = System.currentTimeMillis();
156             if ( log.isInfoEnabled()) log.info(getId()+":rollback succeed ("+(end-start)+" ms)");
157         } catch ( SQLException JavaDoc e) {
158             end = System.currentTimeMillis();
159             if ( log.isErrorEnabled()) log.error(getId()+":rollback failed state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
160             throw e;
161         }
162     }
163
164
165     /**
166      * @see Connection#getAutoCommit()
167      */

168     public boolean getAutoCommit() throws SQLException JavaDoc {
169         return real.getAutoCommit();
170     }
171
172
173     /**
174      * @see Connection#isClosed()
175      */

176     public boolean isClosed() throws SQLException JavaDoc {
177         return real.isClosed();
178     }
179
180
181     /**
182      * @see Connection#isReadOnly()
183      */

184     public boolean isReadOnly() throws SQLException JavaDoc {
185         return real.isReadOnly();
186     }
187
188
189     /**
190      * @see Connection#setHoldability(int)
191      */

192     public void setHoldability(int holdability) throws SQLException JavaDoc {
193         real.setHoldability(holdability);
194     }
195
196
197     /**
198      * @see Connection#setTransactionIsolation(int)
199      */

200     public void setTransactionIsolation(int level) throws SQLException JavaDoc {
201         real.setTransactionIsolation(level);
202     }
203
204
205     /**
206      * @see Connection#setAutoCommit(boolean)
207      */

208     public void setAutoCommit(boolean autoCommit) throws SQLException JavaDoc {
209         long end, start = System.currentTimeMillis();
210
211
212         try {
213             real.setAutoCommit(autoCommit);
214             end = System.currentTimeMillis();
215             if ( log.isInfoEnabled()) log.info(getId()+":autocommit("+autoCommit+") succeed ("+(end-start)+" ms)");
216         } catch ( SQLException JavaDoc e) {
217             end = System.currentTimeMillis();
218             if ( log.isErrorEnabled()) log.error(getId()+":autocommit("+autoCommit+") failed state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
219             throw e;
220         }
221     }
222
223
224     /**
225      * @see Connection#setReadOnly(boolean)
226      */

227     public void setReadOnly(boolean readOnly) throws SQLException JavaDoc {
228         real.setReadOnly(readOnly);
229     }
230
231
232     /**
233      * @see Connection#getCatalog()
234      */

235     public String JavaDoc getCatalog() throws SQLException JavaDoc {
236         return real.getCatalog();
237     }
238
239
240     /**
241      * @see Connection#setCatalog(java.lang.String)
242      */

243     public void setCatalog(String JavaDoc catalog) throws SQLException JavaDoc {
244         real.setCatalog(catalog);
245     }
246
247
248     /**
249      * @see Connection#getMetaData()
250      */

251     public DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc {
252         return real.getMetaData();
253     }
254
255
256     /**
257      * @see Connection#getWarnings()
258      */

259     public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc {
260         SQLWarning JavaDoc current, sw = real.getWarnings();
261
262
263         if ( (current = sw)!=null) {
264             do {
265                 if ( log.isInfoEnabled()) log.info(getId()+":sql warning state="+current.getSQLState()+",code="+current.getErrorCode()+",message="+current.getMessage());
266             } while ( (current = current.getNextWarning())!=null);
267         }
268
269         return sw;
270     }
271
272
273     /**
274      * @see Connection#setSavepoint()
275      */

276     public Savepoint JavaDoc setSavepoint() throws SQLException JavaDoc {
277         long end, start = System.currentTimeMillis();
278         Savepoint JavaDoc savepoint = null;
279
280
281         try {
282             savepoint = real.setSavepoint();
283             end = System.currentTimeMillis();
284             if ( log.isInfoEnabled()) log.info(getId()+":savepoint("+savepoint.getSavepointName()+") succeed ("+(end-start)+" ms)");
285         } catch ( SQLException JavaDoc e) {
286             end = System.currentTimeMillis();
287             if ( log.isErrorEnabled()) log.error(getId()+":savepoint("+"unnamed"+") failed state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
288             throw e;
289         }
290
291         return savepoint;
292     }
293
294
295     /**
296      * @see Connection#releaseSavepoint(java.sql.Savepoint)
297      */

298     public void releaseSavepoint(Savepoint JavaDoc savepoint) throws SQLException JavaDoc {
299         long end, start = System.currentTimeMillis();
300
301
302         try {
303             real.releaseSavepoint(savepoint);
304             end = System.currentTimeMillis();
305             if ( log.isInfoEnabled()) log.info(getId()+":savepoint("+savepoint.getSavepointName()+") succeed ("+(end-start)+" ms)");
306         } catch ( SQLException JavaDoc e) {
307             end = System.currentTimeMillis();
308             if ( log.isErrorEnabled()) log.error(getId()+":release savepoint("+savepoint.getSavepointName()+") failed state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
309             throw e;
310         }
311     }
312
313
314     /**
315      * @see Connection#rollback(java.sql.Savepoint)
316      */

317     public void rollback(Savepoint JavaDoc savepoint) throws SQLException JavaDoc {
318         real.rollback(savepoint);
319     }
320
321
322     /**
323      * @see Connection#createStatement()
324      */

325     public Statement JavaDoc createStatement() throws SQLException JavaDoc {
326         return new SpyStatement ( this, real.createStatement());
327     }
328
329
330     /**
331      * @see Connection#createStatement(int, int)
332      */

333     public Statement JavaDoc createStatement(int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc {
334         return new SpyStatement ( this, real.createStatement(resultSetType,resultSetConcurrency));
335     }
336
337
338     /**
339      * @see Connection#createStatement(int, int, int)
340      */

341     public Statement JavaDoc createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException JavaDoc {
342         return new SpyStatement ( this, real.createStatement(resultSetType,resultSetConcurrency,resultSetHoldability));
343     }
344
345
346     /**
347      * @see Connection#getTypeMap()
348      */

349     public Map JavaDoc getTypeMap() throws SQLException JavaDoc {
350         return real.getTypeMap();
351     }
352
353
354     /**
355      * @see Connection#setTypeMap(java.util.Map)
356      */

357     public void setTypeMap(Map JavaDoc map) throws SQLException JavaDoc {
358         real.setTypeMap(map);
359     }
360
361
362     /**
363      * @see Connection#nativeSQL(java.lang.String)
364      */

365     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException JavaDoc {
366         return real.nativeSQL(sql);
367     }
368
369
370     /**
371      * @see Connection#prepareCall(java.lang.String)
372      */

373     public CallableStatement JavaDoc prepareCall(String JavaDoc sql) throws SQLException JavaDoc {
374         return new SpyCallableStatement ( this, real.prepareCall(sql), sql);
375     }
376
377
378     /**
379      * @see Connection#prepareCall(java.lang.String, int, int)
380      */

381     public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc {
382         return new SpyCallableStatement ( this, real.prepareCall(sql,resultSetType,resultSetConcurrency), sql);
383     }
384
385
386     /**
387      * @see Connection#prepareCall(java.lang.String, int, int, int)
388      */

389     public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException JavaDoc {
390         return new SpyCallableStatement ( this, real.prepareCall(sql,resultSetType,resultSetConcurrency,resultSetHoldability), sql);
391     }
392
393
394     /**
395      * @see Connection#prepareStatement(java.lang.String)
396      */

397     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql) throws SQLException JavaDoc {
398         return new SpyPreparedStatement ( this, real.prepareStatement(sql), sql);
399     }
400
401
402     /**
403      * @see Connection#prepareStatement(java.lang.String, int)
404      */

405     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc {
406         return new SpyPreparedStatement ( this, real.prepareStatement(sql,autoGeneratedKeys), sql);
407     }
408
409
410     /**
411      * @see Connection#prepareStatement(java.lang.String, int, int)
412      */

413     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc {
414         return new SpyPreparedStatement ( this, real.prepareStatement(sql,resultSetType,resultSetConcurrency), sql);
415     }
416
417
418     /**
419      * @see Connection#prepareStatement(java.lang.String, int, int, int)
420      */

421     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException JavaDoc {
422         return new SpyPreparedStatement ( this, real.prepareStatement(sql,resultSetType,resultSetConcurrency,resultSetHoldability), sql);
423     }
424
425
426     /**
427      * @see Connection#prepareStatement(java.lang.String, int[])
428      */

429     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc {
430         return new SpyPreparedStatement ( this, real.prepareStatement(sql,columnIndexes), sql);
431     }
432
433
434     /**
435      * @see Connection#setSavepoint(java.lang.String)
436      */

437     public Savepoint JavaDoc setSavepoint(String JavaDoc name) throws SQLException JavaDoc {
438         long end ,start = System.currentTimeMillis();
439         Savepoint JavaDoc savepoint = null;
440
441
442         try {
443             savepoint = real.setSavepoint(name);
444             end = System.currentTimeMillis();
445             if ( log.isInfoEnabled()) log.info(getId()+":savepoint("+name+") succeed ("+(end-start)+" ms)");
446         } catch ( SQLException JavaDoc e) {
447             end = System.currentTimeMillis();
448             if ( log.isErrorEnabled()) log.error(getId()+":savepoint("+name+") failed state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
449             throw e;
450         }
451
452         return savepoint;
453     }
454
455
456     /**
457      * @see Connection#prepareStatement(java.lang.String, java.lang.String[])
458      */

459     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException JavaDoc {
460         return new SpyPreparedStatement ( this, real.prepareStatement(sql,columnNames), sql);
461     }
462 }
463
Popular Tags