KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > conn > ConnectionWrapper


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.conn;
21
22 import java.sql.CallableStatement JavaDoc;
23 import java.sql.Connection JavaDoc;
24 import java.sql.DatabaseMetaData JavaDoc;
25 import java.sql.PreparedStatement 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 /**
33  * ConnectionWrapper is a <code>java.sql.Connection</code> implementation that wraps
34  * another Connection, delegating method calls to this connection. It works in conjunction
35  * with PooledConnectionImpl, to generate pool events, provide limited automated
36  * reconnection functionality, etc.
37  *
38  * @author Andrus Adamchik
39  */

40 public class ConnectionWrapper implements Connection JavaDoc {
41
42     private Connection JavaDoc connection;
43     private PooledConnectionImpl pooledConnection;
44     private long lastReconnected;
45     private int reconnectCount;
46
47     /**
48      * Fixes Sybase problems with autocommit. Used idea from Jonas
49      * org.objectweb.jonas.jdbc_xa.ConnectionImpl (http://www.objectweb.org/jonas/).
50      * <p>
51      * If problem is not the one that can be fixed by this patch, original exception is
52      * rethrown. If exception occurs when fixing the problem, new exception is thrown.
53      * </p>
54      */

55     static void sybaseAutoCommitPatch(Connection JavaDoc c, SQLException JavaDoc e, boolean autoCommit)
56             throws SQLException JavaDoc {
57
58         String JavaDoc s = e.getMessage().toLowerCase();
59         if (s.indexOf("set chained command not allowed") >= 0) {
60             c.commit();
61             c.setAutoCommit(autoCommit); // Shouldn't fail now.
62
}
63         else {
64             throw e;
65         }
66     }
67
68     /**
69      * Creates new ConnectionWrapper
70      */

71     public ConnectionWrapper(Connection JavaDoc connection, PooledConnectionImpl pooledConnection) {
72         this.connection = connection;
73         this.pooledConnection = pooledConnection;
74     }
75
76     protected void reconnect(SQLException JavaDoc exception) throws SQLException JavaDoc {
77
78         // if there was a relatively recent reconnect, just rethrow an error
79
// and retire itself. THIS WILL PREVENT RECONNECT LOOPS
80
if (reconnectCount > 0 && System.currentTimeMillis() - lastReconnected < 60000) {
81
82             retire(exception);
83             throw exception;
84         }
85
86         pooledConnection.reconnect();
87
88         // Pooled connection will wrap returned connection into
89
// another ConnectionWrapper.... lets get the real connection
90
// underneath...
91
Connection JavaDoc connection = pooledConnection.getConnection();
92         if (connection instanceof ConnectionWrapper) {
93             this.connection = ((ConnectionWrapper) connection).connection;
94         }
95         else {
96             this.connection = connection;
97         }
98
99         lastReconnected = System.currentTimeMillis();
100         reconnectCount++;
101     }
102
103     protected void retire(SQLException JavaDoc exception) {
104         // notify all the listeners....
105
pooledConnection.connectionErrorNotification(exception);
106     }
107
108     public void clearWarnings() throws SQLException JavaDoc {
109         try {
110             connection.clearWarnings();
111         }
112         catch (SQLException JavaDoc sqlEx) {
113             retire(sqlEx);
114             throw sqlEx;
115         }
116     }
117
118     public void close() throws SQLException JavaDoc {
119         if (null != pooledConnection) {
120             pooledConnection.returnConnectionToThePool();
121         }
122         connection = null;
123         pooledConnection = null;
124     }
125
126     public void commit() throws SQLException JavaDoc {
127         try {
128             connection.commit();
129         }
130         catch (SQLException JavaDoc sqlEx) {
131             retire(sqlEx);
132             throw sqlEx;
133         }
134     }
135
136     public Statement JavaDoc createStatement() throws SQLException JavaDoc {
137         try {
138             return connection.createStatement();
139         }
140         catch (SQLException JavaDoc sqlEx) {
141
142             // reconnect has code to prevent loops
143
reconnect(sqlEx);
144             return createStatement();
145         }
146     }
147
148     public Statement JavaDoc createStatement(int resultSetType, int resultSetConcurrency)
149             throws SQLException JavaDoc {
150         try {
151             return connection.createStatement(resultSetType, resultSetConcurrency);
152         }
153         catch (SQLException JavaDoc sqlEx) {
154
155             // reconnect has code to prevent loops
156
reconnect(sqlEx);
157             return createStatement(resultSetType, resultSetConcurrency);
158         }
159     }
160
161     public boolean getAutoCommit() throws SQLException JavaDoc {
162         try {
163             return connection.getAutoCommit();
164         }
165         catch (SQLException JavaDoc sqlEx) {
166             retire(sqlEx);
167             throw sqlEx;
168         }
169     }
170
171     public String JavaDoc getCatalog() throws SQLException JavaDoc {
172         try {
173             return connection.getCatalog();
174         }
175         catch (SQLException JavaDoc sqlEx) {
176             retire(sqlEx);
177             throw sqlEx;
178         }
179     }
180
181     public DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc {
182         try {
183             return connection.getMetaData();
184         }
185         catch (SQLException JavaDoc sqlEx) {
186             retire(sqlEx);
187             throw sqlEx;
188         }
189     }
190
191     public int getTransactionIsolation() throws SQLException JavaDoc {
192         try {
193             return connection.getTransactionIsolation();
194         }
195         catch (SQLException JavaDoc sqlEx) {
196             retire(sqlEx);
197             throw sqlEx;
198         }
199     }
200
201     public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc {
202         try {
203             return connection.getWarnings();
204         }
205         catch (SQLException JavaDoc sqlEx) {
206             retire(sqlEx);
207             throw sqlEx;
208         }
209     }
210
211     public boolean isClosed() throws SQLException JavaDoc {
212         if (connection != null) {
213             try {
214                 return connection.isClosed();
215             }
216             catch (SQLException JavaDoc sqlEx) {
217                 retire(sqlEx);
218                 throw sqlEx;
219             }
220         }
221         else
222             return true;
223     }
224
225     public boolean isReadOnly() throws SQLException JavaDoc {
226         try {
227             return connection.isReadOnly();
228         }
229         catch (SQLException JavaDoc sqlEx) {
230             retire(sqlEx);
231             throw sqlEx;
232         }
233     }
234
235     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException JavaDoc {
236         try {
237             return connection.nativeSQL(sql);
238         }
239         catch (SQLException JavaDoc sqlEx) {
240             retire(sqlEx);
241             throw sqlEx;
242         }
243     }
244
245     public CallableStatement JavaDoc prepareCall(String JavaDoc sql) throws SQLException JavaDoc {
246         try {
247             return connection.prepareCall(sql);
248         }
249         catch (SQLException JavaDoc sqlEx) {
250
251             // reconnect has code to prevent loops
252
reconnect(sqlEx);
253             return prepareCall(sql);
254         }
255     }
256
257     public CallableStatement JavaDoc prepareCall(
258             String JavaDoc sql,
259             int resultSetType,
260             int resultSetConcurrency) throws SQLException JavaDoc {
261         try {
262             return connection.prepareCall(sql, resultSetType, resultSetConcurrency);
263         }
264         catch (SQLException JavaDoc sqlEx) {
265
266             // reconnect has code to prevent loops
267
reconnect(sqlEx);
268             return prepareCall(sql, resultSetType, resultSetConcurrency);
269         }
270     }
271
272     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql) throws SQLException JavaDoc {
273         try {
274             return connection.prepareStatement(sql);
275         }
276         catch (SQLException JavaDoc sqlEx) {
277
278             // reconnect has code to prevent loops
279
reconnect(sqlEx);
280             return prepareStatement(sql);
281         }
282     }
283
284     public PreparedStatement JavaDoc prepareStatement(
285             String JavaDoc sql,
286             int resultSetType,
287             int resultSetConcurrency) throws SQLException JavaDoc {
288         try {
289             return connection.prepareStatement(sql, resultSetType, resultSetConcurrency);
290         }
291         catch (SQLException JavaDoc sqlEx) {
292
293             // reconnect has code to prevent loops
294
reconnect(sqlEx);
295             return prepareStatement(sql, resultSetType, resultSetConcurrency);
296         }
297     }
298
299     public void rollback() throws SQLException JavaDoc {
300         try {
301             connection.rollback();
302         }
303         catch (SQLException JavaDoc sqlEx) {
304             retire(sqlEx);
305             throw sqlEx;
306         }
307     }
308
309     public void setAutoCommit(boolean autoCommit) throws SQLException JavaDoc {
310         try {
311             connection.setAutoCommit(autoCommit);
312         }
313         catch (SQLException JavaDoc sqlEx) {
314
315             try {
316                 // apply Sybase patch
317
sybaseAutoCommitPatch(connection, sqlEx, autoCommit);
318             }
319             catch (SQLException JavaDoc patchEx) {
320                 retire(sqlEx);
321                 throw sqlEx;
322             }
323         }
324     }
325
326     public void setCatalog(String JavaDoc catalog) throws SQLException JavaDoc {
327         try {
328             connection.setCatalog(catalog);
329         }
330         catch (SQLException JavaDoc sqlEx) {
331             retire(sqlEx);
332             throw sqlEx;
333         }
334     }
335
336     public void setReadOnly(boolean readOnly) throws SQLException JavaDoc {
337         try {
338             connection.setReadOnly(readOnly);
339         }
340         catch (SQLException JavaDoc sqlEx) {
341             retire(sqlEx);
342             throw sqlEx;
343         }
344     }
345
346     public void setTransactionIsolation(int level) throws SQLException JavaDoc {
347         try {
348             connection.setTransactionIsolation(level);
349         }
350         catch (SQLException JavaDoc sqlEx) {
351             retire(sqlEx);
352             throw sqlEx;
353         }
354     }
355
356     public Map JavaDoc getTypeMap() throws SQLException JavaDoc {
357         try {
358             return connection.getTypeMap();
359         }
360         catch (SQLException JavaDoc sqlEx) {
361             retire(sqlEx);
362             throw sqlEx;
363         }
364     }
365
366     public void setTypeMap(Map JavaDoc map) throws SQLException JavaDoc {
367         try {
368             connection.setTypeMap(map);
369         }
370         catch (SQLException JavaDoc sqlEx) {
371             retire(sqlEx);
372             throw sqlEx;
373         }
374     }
375
376     public void setHoldability(int holdability) throws SQLException JavaDoc {
377         throw new java.lang.UnsupportedOperationException JavaDoc(
378                 "Method setHoldability() not yet implemented.");
379     }
380
381     public int getHoldability() throws SQLException JavaDoc {
382         throw new java.lang.UnsupportedOperationException JavaDoc(
383                 "Method getHoldability() not yet implemented.");
384     }
385
386     public Savepoint JavaDoc setSavepoint() throws SQLException JavaDoc {
387         throw new java.lang.UnsupportedOperationException JavaDoc(
388                 "Method setSavepoint() not yet implemented.");
389     }
390
391     public Savepoint JavaDoc setSavepoint(String JavaDoc name) throws SQLException JavaDoc {
392         throw new java.lang.UnsupportedOperationException JavaDoc(
393                 "Method setSavepoint() not yet implemented.");
394     }
395
396     public void rollback(Savepoint JavaDoc savepoint) throws SQLException JavaDoc {
397         throw new java.lang.UnsupportedOperationException JavaDoc(
398                 "Method rollback() not yet implemented.");
399     }
400
401     public void releaseSavepoint(Savepoint JavaDoc savepoint) throws SQLException JavaDoc {
402         throw new java.lang.UnsupportedOperationException JavaDoc(
403                 "Method releaseSavepoint() not yet implemented.");
404     }
405
406     public Statement JavaDoc createStatement(
407             int resultSetType,
408             int resultSetConcurrency,
409             int resultSetHoldability) throws SQLException JavaDoc {
410         throw new java.lang.UnsupportedOperationException JavaDoc(
411                 "Method createStatement() not yet implemented.");
412     }
413
414     public PreparedStatement JavaDoc prepareStatement(
415             String JavaDoc sql,
416             int resultSetType,
417             int resultSetConcurrency,
418             int resultSetHoldability) throws SQLException JavaDoc {
419         throw new java.lang.UnsupportedOperationException JavaDoc(
420                 "Method prepareStatement() not yet implemented.");
421     }
422
423     public CallableStatement JavaDoc prepareCall(
424             String JavaDoc sql,
425             int resultSetType,
426             int resultSetConcurrency,
427             int resultSetHoldability) throws SQLException JavaDoc {
428         try {
429             return connection.prepareCall(
430                     sql,
431                     resultSetType,
432                     resultSetConcurrency,
433                     resultSetHoldability);
434         }
435         catch (SQLException JavaDoc sqlEx) {
436
437             // reconnect has code to prevent loops
438
reconnect(sqlEx);
439             return prepareCall(
440                     sql,
441                     resultSetType,
442                     resultSetConcurrency,
443                     resultSetHoldability);
444         }
445     }
446
447     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int autoGeneratedKeys)
448             throws SQLException JavaDoc {
449
450         try {
451             return connection.prepareStatement(sql, autoGeneratedKeys);
452         }
453         catch (SQLException JavaDoc sqlEx) {
454
455             // reconnect has code to prevent loops
456
reconnect(sqlEx);
457             return prepareStatement(sql, autoGeneratedKeys);
458         }
459     }
460
461     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int[] columnIndexes)
462             throws SQLException JavaDoc {
463         try {
464             return connection.prepareStatement(sql, columnIndexes);
465         }
466         catch (SQLException JavaDoc sqlEx) {
467
468             // reconnect has code to prevent loops
469
reconnect(sqlEx);
470             return prepareStatement(sql, columnIndexes);
471         }
472     }
473
474     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, String JavaDoc[] columnNames)
475             throws SQLException JavaDoc {
476         try {
477             return connection.prepareStatement(sql, columnNames);
478         }
479         catch (SQLException JavaDoc sqlEx) {
480
481             // reconnect has code to prevent loops
482
reconnect(sqlEx);
483             return prepareStatement(sql, columnNames);
484         }
485     }
486
487 }
488
Popular Tags