KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > conn > LoggingConnection


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.conn;
13
14 import com.versant.core.logging.LogEventStore;
15 import com.versant.core.jdbc.logging.JdbcConnectionEvent;
16 import com.versant.core.jdbc.logging.JdbcPsPoolEvent;
17 import com.versant.core.common.Debug;
18
19 import java.sql.*;
20
21 /**
22  * A JDBC connection wrapped for event logging with a PreparedStatement cache.
23  * These concerns should really be separated but we have not had time to do
24  * that yet.
25  */

26 public class LoggingConnection implements Connection {
27
28     private final Connection con;
29     private final LogEventStore pes;
30     private final PreparedStatementPool psPool;
31     private final boolean clearBatch;
32     private boolean needsValidation; // set for Exception's on us
33

34     public LoggingConnection(Connection con, LogEventStore pes, boolean usePsPool,
35             int psCacheMax, boolean clearBatch) {
36         this.clearBatch = clearBatch;
37         this.con = con;
38         this.pes = pes;
39         if (usePsPool) {
40             psPool = new PreparedStatementPool(this, psCacheMax);
41         } else {
42             psPool = null;
43         }
44     }
45
46     public PreparedStatementPool getPsPool() {
47         return psPool;
48     }
49
50     public void setNeedsValidation(boolean needsValidation) {
51         this.needsValidation = needsValidation;
52     }
53
54     public boolean isNeedsValidation() {
55         return needsValidation;
56     }
57
58     public java.sql.Connection JavaDoc getCon() {
59         return con;
60     }
61
62     /**
63      * Return ps to the pool.
64      *
65      * @see PooledPreparedStatement#close
66      */

67     public void returnPreparedStatement(PooledPreparedStatement ps)
68             throws SQLException {
69         try {
70             if (clearBatch) ps.clearBatch();
71             psPool.returnPS(ps);
72             if (pes.isFiner()) {
73                 logPsPoolEvent(ps.getKey(), JdbcConnectionEvent.PSPOOL_RELEASE, ps);
74             }
75         } catch (Exception JavaDoc e) {
76             throw convertException(e);
77         }
78     }
79
80     /**
81      * This is just going to return the connection to the pool.
82      */

83     public void close() throws SQLException {
84         closeRealConnection();
85     }
86
87     public void setHoldability(int holdability) throws SQLException {
88         con.setHoldability(holdability);
89     }
90
91     public int getHoldability() throws SQLException {
92         return con.getHoldability();
93     }
94
95     public Savepoint setSavepoint() throws SQLException {
96         return con.setSavepoint();
97     }
98
99     public Savepoint setSavepoint(String JavaDoc name) throws SQLException {
100         return con.setSavepoint(name);
101     }
102
103     public void rollback(Savepoint savepoint) throws SQLException {
104         con.rollback(savepoint);
105     }
106
107     public void releaseSavepoint(Savepoint savepoint) throws SQLException {
108         con.releaseSavepoint(savepoint);
109     }
110
111     public Statement createStatement(int resultSetType, int resultSetConcurrency,
112                                      int resultSetHoldability) throws SQLException {
113         return con.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
114     }
115
116     public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType,
117                                               int resultSetConcurrency, int resultSetHoldability)
118             throws SQLException {
119         return con.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
120     }
121
122     public CallableStatement prepareCall(String JavaDoc sql, int resultSetType,
123                                          int resultSetConcurrency,
124                                          int resultSetHoldability) throws SQLException {
125         return con.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
126     }
127
128     public PreparedStatement prepareStatement(String JavaDoc sql, int autoGeneratedKeys)
129             throws SQLException {
130         return con.prepareStatement(sql, autoGeneratedKeys);
131     }
132
133     public PreparedStatement prepareStatement(String JavaDoc sql, int columnIndexes[])
134             throws SQLException {
135         return con.prepareStatement(sql, columnIndexes);
136     }
137
138     public PreparedStatement prepareStatement(String JavaDoc sql, String JavaDoc columnNames[])
139             throws SQLException {
140         return con.prepareStatement(sql, columnNames);
141     }
142
143     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException {
144         return con.nativeSQL(sql);
145     }
146
147     public boolean getAutoCommit() throws SQLException {
148         return con.getAutoCommit();
149     }
150
151     public boolean isClosed() throws SQLException {
152         return con.isClosed();
153     }
154
155     public DatabaseMetaData getMetaData() throws SQLException {
156         return con.getMetaData();
157     }
158
159     public void setReadOnly(boolean readOnly) throws SQLException {
160         con.setReadOnly(readOnly);
161     }
162
163     public boolean isReadOnly() throws SQLException {
164         return con.isReadOnly();
165     }
166
167     public void setCatalog(String JavaDoc catalog) throws SQLException {
168         con.setCatalog(catalog);
169     }
170
171     public String JavaDoc getCatalog() throws SQLException {
172         return con.getCatalog();
173     }
174
175     public void setTransactionIsolation(int level) throws SQLException {
176         JdbcConnectionEvent ev = null;
177         if (pes.isFiner()) {
178             ev = new JdbcConnectionEvent(0, this,
179                     toIsolationStr(level),
180                     JdbcConnectionEvent.CON_ISOLATION);
181             pes.log(ev);
182         }
183         try {
184             con.setTransactionIsolation(level);
185         } catch (SQLException e) {
186             needsValidation = true;
187             if (ev != null) ev.setErrorMsg(e);
188             throw e;
189         } catch (RuntimeException JavaDoc e) {
190             needsValidation = true;
191             if (ev != null) ev.setErrorMsg(e);
192             throw e;
193         } finally {
194             if (ev != null) ev.updateTotalMs();
195         }
196     }
197
198     private static String JavaDoc toIsolationStr(int lvl) {
199         switch (lvl) {
200             case TRANSACTION_NONE:
201                 return "NONE(0)";
202             case TRANSACTION_READ_UNCOMMITTED:
203                 return "READ_UNCOMMITTED(1)";
204             case TRANSACTION_READ_COMMITTED:
205                 return "READ_COMMITTED(2)";
206             case TRANSACTION_REPEATABLE_READ:
207                 return "REPEATABLE_READ(4)";
208             case TRANSACTION_SERIALIZABLE:
209                 return "SERIALIZABLE(8)";
210         }
211         return "UNKNOWN(" + lvl + ")";
212     }
213
214     public int getTransactionIsolation() throws SQLException {
215         return con.getTransactionIsolation();
216     }
217
218
219     public SQLWarning getWarnings() throws SQLException {
220         return con.getWarnings();
221     }
222
223     public void clearWarnings() throws SQLException {
224         con.clearWarnings();
225     }
226
227     public Statement createStatement(int resultSetType, int resultSetConcurrency)
228             throws SQLException {
229         return con.createStatement(resultSetType, resultSetConcurrency);
230     }
231
232     public CallableStatement prepareCall(String JavaDoc sql, int resultSetType,
233                                          int resultSetConcurrency) throws SQLException {
234         return con.prepareCall(sql, resultSetType, resultSetConcurrency);
235     }
236
237     public java.util.Map JavaDoc getTypeMap() throws SQLException {
238         return con.getTypeMap();
239     }
240
241     public void setTypeMap(java.util.Map JavaDoc map) throws SQLException {
242         con.setTypeMap(map);
243     }
244
245     public Statement createStatement() throws SQLException {
246         if (!pes.isFine()) return con.createStatement();
247         JdbcConnectionEvent ev = null;
248         if (pes.isFiner()) {
249             ev = new JdbcConnectionEvent(0, this,
250                     null, JdbcConnectionEvent.CON_CREATE_STATEMENT);
251             pes.log(ev);
252         }
253         try {
254             Statement stat = new LoggingStatement(this, con.createStatement(), pes);
255             if (ev != null) ev.updateStatementID(stat);
256             return stat;
257         } catch (SQLException e) {
258             needsValidation = true;
259             if (ev != null) ev.setErrorMsg(e);
260             throw e;
261         } catch (RuntimeException JavaDoc e) {
262             needsValidation = true;
263             if (ev != null) ev.setErrorMsg(e);
264             throw e;
265         } finally {
266             if (ev != null) ev.updateTotalMs();
267         }
268     }
269
270     public PreparedStatement prepareStatement(String JavaDoc sql)
271             throws SQLException {
272         if (psPool == null) {
273             return prepareStatementImp(sql, 0, 0, null);
274         } else {
275             try {
276                 PreparedStatementPool.Key key = new PreparedStatementPool.Key(sql);
277                 PooledPreparedStatement ps = psPool.borrowPS(key);
278                 if (pes.isFiner()) {
279                     logPsPoolEvent(key, JdbcConnectionEvent.PSPOOL_ALLOC, ps);
280                 }
281                 return ps;
282             } catch (Exception JavaDoc e) {
283                 throw convertException(e);
284             }
285         }
286     }
287
288     private void logPsPoolEvent(PreparedStatementPool.Key key, int type, PreparedStatement ps) {
289         JdbcPsPoolEvent ev = new JdbcPsPoolEvent(0, this,
290                 key.getSql(), type, psPool.getNumActive(), psPool.getNumIdle());
291         ev.zeroTotalMs();
292         ev.setResultSetType(key.getResultSetType());
293         ev.setResultSetConcurrency(key.getResultSetConcurrency());
294         ev.setStatementID(System.identityHashCode(ps));
295         pes.log(ev);
296     }
297
298     private SQLException convertException(Exception JavaDoc x) {
299         if (x instanceof SQLException) return (SQLException) x;
300         if (Debug.DEBUG) x.printStackTrace(System.out);
301         return new SQLException(x.getClass().getName() + ": " + x.getMessage());
302     }
303
304     public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType,
305             int resultSetConcurrency) throws SQLException {
306         if (psPool == null) {
307             return prepareStatementImp(sql, resultSetType, resultSetConcurrency, null);
308         } else {
309             try {
310                 PreparedStatementPool.Key key = new PreparedStatementPool.Key(sql, resultSetType, resultSetConcurrency);
311                 PooledPreparedStatement ps = psPool.borrowPS(key);
312                 if (pes.isFiner()) {
313                     logPsPoolEvent(key, JdbcConnectionEvent.PSPOOL_ALLOC, ps);
314                 }
315                 return ps;
316             } catch (Exception JavaDoc e) {
317                 throw convertException(e);
318             }
319         }
320     }
321
322     /**
323      * Create a new ps. This will not use the PS pool.
324      */

325     public PooledPreparedStatement prepareStatementImp(String JavaDoc sql, int resultSetType,
326             int resultSetConcurrency, PreparedStatementPool.Key key) throws SQLException {
327         JdbcConnectionEvent ev = null;
328         if (pes.isFiner()) {
329             ev = new JdbcConnectionEvent(0, this,
330                     sql, JdbcConnectionEvent.CON_PREPARE_STATEMENT);
331             ev.setResultSetType(resultSetType);
332             ev.setResultSetConcurrency(resultSetConcurrency);
333             pes.log(ev);
334         }
335         try {
336             PreparedStatement stat;
337             if (resultSetType != 0) {
338                 stat = con.prepareStatement(sql, resultSetType, resultSetConcurrency);
339             } else {
340                 stat = con.prepareStatement(sql);
341             }
342             PooledPreparedStatement ps;
343             if (pes.isFiner()) {
344                 ps = new PooledPSWithParamLogging(this, stat, pes, sql, key);
345             } else {
346                 ps = new PooledPreparedStatement(this, stat, pes, sql, key);
347             }
348             if (ev != null) ev.updateStatementID(ps);
349             return ps;
350         } catch (SQLException e) {
351             needsValidation = true;
352             if (ev != null) ev.setErrorMsg(e);
353             throw e;
354         } catch (RuntimeException JavaDoc e) {
355             needsValidation = true;
356             if (ev != null) ev.setErrorMsg(e);
357             throw e;
358         } finally {
359             if (ev != null) ev.updateTotalMs();
360         }
361     }
362
363     public CallableStatement prepareCall(String JavaDoc sql) throws SQLException {
364         if (!pes.isFine()) return con.prepareCall(sql);
365         JdbcConnectionEvent ev = null;
366         if (pes.isFiner()) {
367             ev = new JdbcConnectionEvent(0, this,
368                     sql, JdbcConnectionEvent.CON_PREPARE_CALL);
369             pes.log(ev);
370         }
371         try {
372             CallableStatement stat =
373                     new LoggingCallableStatement(this, con.prepareCall(sql), pes);
374             if (ev != null) ev.updateStatementID(stat);
375             return stat;
376         } catch (SQLException e) {
377             needsValidation = true;
378             if (ev != null) ev.setErrorMsg(e);
379             throw e;
380         } catch (RuntimeException JavaDoc e) {
381             needsValidation = true;
382             if (ev != null) ev.setErrorMsg(e);
383             throw e;
384         } finally {
385             if (ev != null) ev.updateTotalMs();
386         }
387     }
388
389     public void setAutoCommit(boolean autoCommit) throws SQLException {
390         JdbcConnectionEvent ev = null;
391         if (pes.isFiner()) {
392             ev = new JdbcConnectionEvent(0, this,
393                     autoCommit ? "true" : "false",
394                     JdbcConnectionEvent.CON_AUTOCOMMIT);
395             pes.log(ev);
396         }
397         try {
398             con.setAutoCommit(autoCommit);
399         } catch (SQLException e) {
400             needsValidation = true;
401             if (ev != null) ev.setErrorMsg(e);
402             throw e;
403         } catch (RuntimeException JavaDoc e) {
404             needsValidation = true;
405             if (ev != null) ev.setErrorMsg(e);
406             throw e;
407         } finally {
408             if (ev != null) ev.updateTotalMs();
409         }
410     }
411
412     public void commit() throws SQLException {
413         JdbcConnectionEvent ev = null;
414         if (pes.isFine()) {
415             ev = new JdbcConnectionEvent(0, this, null,
416                     JdbcConnectionEvent.CON_COMMIT);
417             pes.log(ev);
418         }
419         try {
420             con.commit();
421         } catch (SQLException e) {
422             needsValidation = true;
423             if (ev != null) ev.setErrorMsg(e);
424             throw e;
425         } catch (RuntimeException JavaDoc e) {
426             needsValidation = true;
427             if (ev != null) ev.setErrorMsg(e);
428             throw e;
429         } finally {
430             if (ev != null) ev.updateTotalMs();
431         }
432     }
433
434     public void rollback() throws SQLException {
435         JdbcConnectionEvent ev = null;
436         if (pes.isFine()) {
437             ev = new JdbcConnectionEvent(0, this, null,
438                     JdbcConnectionEvent.CON_ROLLBACK);
439             pes.log(ev);
440         }
441         try {
442             con.rollback();
443         } catch (SQLException e) {
444             needsValidation = true;
445             if (ev != null) ev.setErrorMsg(e);
446             throw e;
447         } catch (RuntimeException JavaDoc e) {
448             needsValidation = true;
449             if (ev != null) ev.setErrorMsg(e);
450             throw e;
451         } finally {
452             if (ev != null) ev.updateTotalMs();
453         }
454     }
455
456     /**
457      * Realy close this connection i.e. do not return it to the pool.
458      */

459     public void closeRealConnection() throws SQLException {
460         if (!pes.isFine()) {
461             con.close();
462             return;
463         }
464         JdbcConnectionEvent ev = new JdbcConnectionEvent(0, this, null,
465                 JdbcConnectionEvent.CON_CLOSE);
466         pes.log(ev);
467         try {
468             con.close();
469         } catch (SQLException e) {
470             ev.setErrorMsg(e);
471             throw e;
472         } catch (RuntimeException JavaDoc e) {
473             ev.setErrorMsg(e);
474             throw e;
475         } finally {
476             ev.updateTotalMs();
477         }
478     }
479
480 }
481
482
Popular Tags