KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > sql > spy > SpyConnection


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.sql.spy;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.L10N;
33
34 import java.sql.*;
35 import java.util.Map JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 /**
39  * Spying on a connection.
40  */

41 public class SpyConnection implements java.sql.Connection JavaDoc {
42   protected final static Logger JavaDoc log = Log.open(SpyConnection.class);
43   protected final static Logger JavaDoc logXA =
44     Logger.getLogger(SpyConnection.class.getName() + ".XA");
45   protected final static L10N L = new L10N(SpyConnection.class);
46
47   private int _id;
48   private int _stmtCount;
49
50   // The underlying connection
51
private Connection _conn;
52
53   /**
54    * Creates a new SpyConnection.
55    */

56   public SpyConnection(Connection conn, int id)
57   {
58     _conn = conn;
59     _id = id;
60   }
61
62   /**
63    * Returns the underlying connection.
64    */

65   public Connection getConnection()
66   {
67     return _conn;
68   }
69
70   /**
71    * JDBC api to return the connection's catalog.
72    *
73    * @return the JDBC catalog.
74    */

75   public String JavaDoc getCatalog()
76     throws SQLException
77   {
78     try {
79       String JavaDoc catalog = _conn.getCatalog();
80
81       log.info(_id + ":getCatalog() -> " + catalog);
82       
83       return catalog;
84     } catch (SQLException e) {
85       log.info(_id + ":exn-getCatalog(" + e + ")");
86       
87       throw e;
88     }
89   }
90
91   /**
92    * Sets the JDBC catalog.
93    */

94   public void setCatalog(String JavaDoc catalog)
95     throws SQLException
96   {
97     try {
98       log.info(_id + ":setCatalog(" + catalog + ")");
99     
100       _conn.setCatalog(catalog);
101     } catch (SQLException e) {
102       log.info(_id + ":exn-setCatalog(" + e + ")");
103       throw e;
104     }
105   }
106
107   /**
108    * Gets the connection's metadata.
109    */

110   public DatabaseMetaData getMetaData()
111     throws SQLException
112   {
113     try {
114       DatabaseMetaData metaData = _conn.getMetaData();
115       
116       log.info(_id + ":getMetaData() -> " + metaData);
117       
118       return metaData;
119     } catch (SQLException e) {
120       log.info(_id + ":exn-getMetaData(" + e + ")");
121       throw e;
122     }
123   }
124
125   /**
126    * Returns the connection's type map.
127    */

128   public Map JavaDoc getTypeMap()
129     throws SQLException
130   {
131     try {
132       Map JavaDoc map = _conn.getTypeMap();
133
134       log.info(_id + ":getTypeMap() -> " + map);
135       
136       return map;
137     } catch (SQLException e) {
138       log.info(_id + ":exn-getTypeMap(" + e + ")");
139       throw e;
140     }
141   }
142
143   /**
144    * Sets the connection's type map.
145    */

146   public void setTypeMap(Map JavaDoc<String JavaDoc,Class JavaDoc<?>> map)
147     throws SQLException
148   {
149     try {
150       log.info(_id + ":setTypeMap(" + map + ")");
151       
152       _conn.setTypeMap(map);
153     } catch (SQLException e) {
154       log.info(_id + ":exn-setTypeMap(" + e + ")");
155
156       throw e;
157     }
158   }
159
160   /**
161    * Calls the nativeSQL method for the connection.
162    */

163   public String JavaDoc nativeSQL(String JavaDoc sql)
164     throws SQLException
165   {
166     try {
167       String JavaDoc nativeSQL = _conn.nativeSQL(sql);
168
169       log.info(_id + ":nativeSQL() -> " + nativeSQL);
170       
171       return nativeSQL;
172     } catch (SQLException e) {
173       log.info(_id + ":exn-nativeSQL(" + e + ")");
174
175       throw e;
176     }
177   }
178
179   public int getTransactionIsolation()
180     throws SQLException
181   {
182     try {
183       int isolation = _conn.getTransactionIsolation();
184
185       log.info(_id + ":getTransactionIsolation() -> " + isolation);
186       
187       return isolation;
188     } catch (SQLException e) {
189       log.info(_id + ":exn-getTransactionIsolation(" + e + ")");
190
191       throw e;
192     }
193   }
194
195   public void setTransactionIsolation(int isolation)
196     throws SQLException
197   {
198     try {
199       log.info(_id + ":setTransactionIsolation(" + isolation + ")");
200       
201       _conn.setTransactionIsolation(isolation);
202     } catch (SQLException e) {
203       log.info(_id + ":exn-setTransactionIsolation(" + e + ")");
204
205       throw e;
206     }
207   }
208
209   public SQLWarning getWarnings()
210     throws SQLException
211   {
212     try {
213       SQLWarning warning = _conn.getWarnings();
214       
215       log.info(_id + ":getWarnings() -> " + warning);
216       
217       return warning;
218     } catch (SQLException e) {
219       log.info(_id + ":exn-getWarnings(" + e + ")");
220
221       throw e;
222     }
223   }
224
225   public void clearWarnings()
226     throws SQLException
227   {
228     try {
229       log.info(_id + ":clearWarnings()");
230
231       _conn.clearWarnings();
232     } catch (SQLException e) {
233       log.info(_id + ":exn-clearWarnings(" + e + ")");
234
235       throw e;
236     }
237   }
238
239   public void setReadOnly(boolean readOnly)
240     throws SQLException
241   {
242     try {
243       log.info(_id + ":setReadOnly(" + readOnly + ")");
244       
245       _conn.setReadOnly(readOnly);
246     } catch (SQLException e) {
247       log.info(_id + ":exn-setReadOnly(" + e + ")");
248
249       throw e;
250     }
251   }
252
253   public boolean isReadOnly()
254     throws SQLException
255   {
256     try {
257       boolean isReadOnly = _conn.isReadOnly();
258
259       log.info(_id + "isReadOnly() -> " + isReadOnly);
260
261       return isReadOnly;
262
263     } catch (SQLException e) {
264       log.info(_id + ":exn-isReadOnly(" + e + ")");
265
266       throw e;
267     }
268   }
269
270   /**
271    * JDBC api to create a new statement. Any SQL exception thrown here
272    * will make the connection invalid, i.e. it can't be put back into
273    * the pool.
274    *
275    * @return a new JDBC statement.
276    */

277   public Statement createStatement()
278     throws SQLException
279   {
280     try {
281       String JavaDoc stmtId = _id + "." + _stmtCount++;
282       
283       log.info(stmtId + ":createStatement()");
284       
285       Statement stmt;
286       
287       stmt = _conn.createStatement();
288       
289       return new SpyStatement(stmtId, this, stmt);
290     } catch (SQLException e) {
291       log.info(_id + ":exn-createStatement(" + e + ")");
292       throw e;
293     }
294   }
295
296   /**
297    * JDBC api to create a new statement. Any SQL exception thrown here
298    * will make the connection invalid, i.e. it can't be put back into
299    * the pool.
300    *
301    * @return a new JDBC statement.
302    */

303   public Statement createStatement(int resultSetType, int resultSetConcurrency)
304     throws SQLException
305   {
306     try {
307       String JavaDoc stmtId = _id + "." + _stmtCount++;
308       
309       log.info(stmtId + ":createStatement(type=" + resultSetType +
310               ",concurrency=" + resultSetConcurrency + ")");
311       
312       Statement stmt;
313       
314       stmt = _conn.createStatement(resultSetType, resultSetConcurrency);
315       
316       return new SpyStatement(stmtId, this, stmt);
317     } catch (SQLException e) {
318       log.info(_id + ":exn-createStatement(" + e + ")");
319       throw e;
320     }
321   }
322   
323   public Statement createStatement(int resultSetType,
324                                    int resultSetConcurrency,
325                                    int resultSetHoldability)
326     throws SQLException
327   {
328     try {
329       String JavaDoc stmtId = _id + "." + _stmtCount++;
330       
331       log.info(stmtId + ":createStatement(type=" + resultSetType +
332               ",concurrency=" + resultSetConcurrency +
333               ",holdability=" + resultSetHoldability + ")");
334       
335       Statement stmt;
336       
337       stmt = _conn.createStatement(resultSetType,
338                                    resultSetConcurrency,
339                                    resultSetHoldability);
340       
341       return new SpyStatement(stmtId, this, stmt);
342     } catch (SQLException e) {
343       log.info(_id + ":exn-createStatement(" + e + ")");
344       throw e;
345     }
346   }
347
348
349   public PreparedStatement prepareStatement(String JavaDoc sql)
350     throws SQLException
351   {
352     try {
353       String JavaDoc stmtId = _id + "." + _stmtCount++;
354       log.info(stmtId + ":prepareStatement(" + sql + ")");
355       
356       PreparedStatement stmt;
357       
358       stmt = _conn.prepareStatement(sql);
359       
360       return new SpyPreparedStatement(stmtId, this, stmt, sql);
361     } catch (SQLException e) {
362       log.info(_id + ":exn-prepareStatement(" + e + ")");
363
364       throw e;
365     }
366   }
367   
368   public PreparedStatement prepareStatement(String JavaDoc sql,
369                                             int resultSetType)
370     throws SQLException
371   {
372     try {
373       String JavaDoc stmtId = _id + "." + _stmtCount++;
374       log.info(stmtId + ":prepareStatement(" + sql + ",type=" + resultSetType + ")");
375       
376       PreparedStatement stmt;
377       
378       stmt = _conn.prepareStatement(sql, resultSetType);
379       
380       return new SpyPreparedStatement(stmtId, this, stmt, sql);
381     } catch (SQLException e) {
382       log.info(_id + ":exn-prepareStatement(" + e + ")");
383
384       throw e;
385     }
386   }
387
388   public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType,
389                         int resultSetConcurrency)
390     throws SQLException
391   {
392     try {
393       String JavaDoc stmtId = _id + "." + _stmtCount++;
394       log.info(stmtId + ":prepareStatement(" + sql + ",type=" + resultSetType +
395               ",concurrency=" + resultSetConcurrency + ")");
396       
397       PreparedStatement stmt;
398       
399       stmt = _conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
400       
401       return new SpyPreparedStatement(stmtId, this, stmt, sql);
402     } catch (SQLException e) {
403       log.info(_id + ":exn-prepareStatement(" + e + ")");
404
405       throw e;
406     }
407   }
408
409   public PreparedStatement prepareStatement(String JavaDoc sql,
410                                             int resultSetType,
411                                             int resultSetConcurrency,
412                                             int resultSetHoldability)
413     throws SQLException
414   {
415     return null;
416   }
417   
418   public PreparedStatement prepareStatement(String JavaDoc sql,
419                                             int []columnIndexes)
420     throws SQLException
421   {
422     return null;
423   }
424   
425   public PreparedStatement prepareStatement(String JavaDoc sql,
426                                             String JavaDoc []columnNames)
427     throws SQLException
428   {
429     return null;
430   }
431
432   public CallableStatement prepareCall(String JavaDoc sql)
433     throws SQLException
434   {
435     try {
436       String JavaDoc stmtId = _id + "." + _stmtCount++;
437       log.info(stmtId + ":prepareCall(" + sql + ")");
438       
439       CallableStatement stmt;
440       
441       stmt = _conn.prepareCall(sql);
442       
443       return stmt;
444     } catch (SQLException e) {
445       log.info(_id + ":exn-prepareCall(" + e + ")");
446
447       throw e;
448     }
449   }
450
451   public CallableStatement prepareCall(String JavaDoc sql, int resultSetType,
452                        int resultSetConcurrency)
453     throws SQLException
454   {
455     try {
456       String JavaDoc stmtId = _id + "." + _stmtCount++;
457       log.info(stmtId + ":prepareCall(" + sql + ",type=" + resultSetType +
458               ",concurrency=" + resultSetConcurrency + ")");
459       
460       CallableStatement stmt;
461       
462       stmt = _conn.prepareCall(sql);
463       
464       return stmt;
465     } catch (SQLException e) {
466       log.info(_id + ":exn-prepareCall(" + e + ")");
467
468       throw e;
469     }
470   }
471
472   public CallableStatement prepareCall(String JavaDoc sql,
473                                        int resultSetType,
474                                        int resultSetConcurrency,
475                                        int resultSetHoldability)
476     throws SQLException
477   {
478     return null;
479   }
480
481   public boolean getAutoCommit()
482     throws SQLException
483   {
484     try {
485       boolean autoCommit = _conn.getAutoCommit();
486       
487       log.info(_id + ":getAutoCommit() -> " + autoCommit);
488       
489       return autoCommit;
490     } catch (SQLException e) {
491       log.info(_id + ":exn-getAutoCommit(" + e + ")");
492
493       throw e;
494     }
495   }
496
497   public void setAutoCommit(boolean autoCommit)
498     throws SQLException
499   {
500     try {
501       logXA.info(_id + ":setAutoCommit(" + autoCommit + ")");
502       
503       _conn.setAutoCommit(autoCommit);
504     } catch (SQLException e) {
505       logXA.info(_id + ":exn-setAutoCommit(" + e + ")");
506
507       throw e;
508     }
509   }
510
511   public void commit()
512     throws SQLException
513   {
514     try {
515       logXA.info(_id + ":commit()");
516       
517       _conn.commit();
518     } catch (SQLException e) {
519       logXA.info(_id + ":exn-commit(" + e + ")");
520
521       throw e;
522     }
523   }
524
525   public void rollback()
526     throws SQLException
527   {
528     try {
529       logXA.info(_id + ":rollback()");
530       
531       _conn.rollback();
532     } catch (SQLException e) {
533       logXA.info(_id + ":exn-rollback(" + e + ")");
534
535       throw e;
536     }
537   }
538
539   /**
540    * Returns true if the connection is closed.
541    */

542   public boolean isClosed()
543     throws SQLException
544   {
545     try {
546       boolean isClosed = _conn.isClosed();
547
548       log.info(_id + ":isClosed() -> " + isClosed);
549       
550       return isClosed;
551     } catch (SQLException e) {
552       log.info(_id + ":exn-isClosed(" + e + ")");
553
554       throw e;
555     }
556   }
557
558   /**
559    * Reset the connection and return the underlying JDBC connection to
560    * the pool.
561    */

562   public void close() throws SQLException
563   {
564     log.info(_id + ":close()");
565
566     try {
567       _conn.close();
568     } catch (SQLException e) {
569       log.info(_id + ":exn-close(" + e + ")");
570
571       throw e;
572     }
573   }
574
575   public void setHoldability(int hold)
576     throws SQLException
577   {
578     _conn.setHoldability(hold);
579   }
580
581   public int getHoldability()
582     throws SQLException
583   {
584     return _conn.getHoldability();
585   }
586
587   public Savepoint setSavepoint()
588     throws SQLException
589   {
590     return _conn.setSavepoint();
591   }
592
593   public Savepoint setSavepoint(String JavaDoc name)
594     throws SQLException
595   {
596     return _conn.setSavepoint(name);
597   }
598
599   public void releaseSavepoint(Savepoint savepoint)
600     throws SQLException
601   {
602     _conn.releaseSavepoint(savepoint);
603   }
604
605   public void rollback(Savepoint savepoint)
606     throws SQLException
607   {
608     _conn.rollback(savepoint);
609   }
610
611   public String JavaDoc toString()
612   {
613     return "SpyConnection[id=" + _id + ",conn=" + _conn + "]";
614   }
615 }
616
Popular Tags