001 /*****************************************************************************
002 * Copyright (c) PicoContainer Organization. All rights reserved. *
003 * ------------------------------------------------------------------------- *
004 * The software in this package is published under the terms of the BSD *
005 * style license a copy of which has been included with this distribution in *
006 * the license.html file. *
007 * *
008 * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
009 *****************************************************************************/
010 package org.nanocontainer.persistence.jdbc;
011
012 import java.sql.CallableStatement;
013 import java.sql.Connection;
014 import java.sql.DatabaseMetaData;
015 import java.sql.PreparedStatement;
016 import java.sql.SQLException;
017 import java.sql.SQLWarning;
018 import java.sql.Savepoint;
019 import java.sql.Statement;
020 import java.util.Map;
021
022 import org.nanocontainer.persistence.ExceptionHandler;
023
024 /**
025 * Base classe for Connection components. It delegates all calls to the connection obtained by getDelegatedConnection
026 * method. Error handling is also there.
027 *
028 * @version $Id: AbstractConnection.java 2653 2005-10-16 12:50:07Z juze $
029 * @author Juze Peleteiro <juze -a-t- intelli -dot- biz>
030 */
031 public abstract class AbstractConnection implements Connection {
032
033 private ExceptionHandler jdbcExceptionHandler;
034
035 protected AbstractConnection(ExceptionHandler jdbcExceptionHandler) {
036 this.jdbcExceptionHandler = jdbcExceptionHandler;
037 }
038
039 protected AbstractConnection() {
040 jdbcExceptionHandler = null;
041 }
042
043 protected abstract Connection getDelegatedConnection() throws SQLException;
044
045 protected abstract void invalidateDelegatedConnection();
046
047 /**
048 * Invalidates the connection calling {@link #invalidateDelegatedConnection()} and convert the <code>cause</code>
049 * using a {@link ExceptionHandler}. if it's available otherwise just return the <code>cause</code> back.
050 */
051 protected SQLException handleException(Exception cause) throws RuntimeException {
052 try {
053 invalidateDelegatedConnection();
054 } catch (Exception e) {
055 // Do nothing, only the original exception should be reported.
056 }
057
058 if (jdbcExceptionHandler == null) {
059 if (cause instanceof SQLException) {
060 return (SQLException) cause;
061 }
062
063 throw (RuntimeException) cause;
064 }
065
066 throw jdbcExceptionHandler.handle(cause);
067 }
068
069 /**
070 * @see java.sql.Connection#createStatement()
071 */
072 public Statement createStatement() throws SQLException {
073 try {
074 return getDelegatedConnection().createStatement();
075 } catch (Exception e) {
076 throw handleException(e);
077 }
078 }
079
080 /**
081 * @see java.sql.Connection#prepareStatement(java.lang.String)
082 */
083 public PreparedStatement prepareStatement(String sql) throws SQLException {
084 try {
085 return getDelegatedConnection().prepareStatement(sql);
086 } catch (Exception e) {
087 throw handleException(e);
088 }
089 }
090
091 /**
092 * @see java.sql.Connection#prepareCall(java.lang.String)
093 */
094 public CallableStatement prepareCall(String sql) throws SQLException {
095 try {
096 return getDelegatedConnection().prepareCall(sql);
097 } catch (Exception e) {
098 throw handleException(e);
099 }
100 }
101
102 /**
103 * @see java.sql.Connection#nativeSQL(java.lang.String)
104 */
105 public String nativeSQL(String sql) throws SQLException {
106 try {
107 return getDelegatedConnection().nativeSQL(sql);
108 } catch (Exception e) {
109 throw handleException(e);
110 }
111 }
112
113 /**
114 * @see java.sql.Connection#setAutoCommit(boolean)
115 */
116 public void setAutoCommit(boolean autoCommit) throws SQLException {
117 try {
118 getDelegatedConnection().setAutoCommit(autoCommit);
119 } catch (Exception e) {
120 throw handleException(e);
121 }
122 }
123
124 /**
125 * @see java.sql.Connection#getAutoCommit()
126 */
127 public boolean getAutoCommit() throws SQLException {
128 try {
129 return getDelegatedConnection().getAutoCommit();
130 } catch (Exception e) {
131 throw handleException(e);
132 }
133 }
134
135 /**
136 * @see java.sql.Connection#commit()
137 */
138 public void commit() throws SQLException {
139 try {
140 getDelegatedConnection().commit();
141 } catch (Exception e) {
142 throw handleException(e);
143 }
144 }
145
146 /**
147 * @see java.sql.Connection#rollback()
148 */
149 public void rollback() throws SQLException {
150 try {
151 getDelegatedConnection().rollback();
152 } catch (Exception e) {
153 throw handleException(e);
154 }
155 }
156
157 /**
158 * @see java.sql.Connection#close()
159 */
160 public void close() throws SQLException {
161 try {
162 getDelegatedConnection().close();
163 } catch (Exception e) {
164 throw handleException(e);
165 }
166 }
167
168 /**
169 * @see java.sql.Connection#isClosed()
170 */
171 public boolean isClosed() throws SQLException {
172 try {
173 return getDelegatedConnection().isClosed();
174 } catch (Exception e) {
175 throw handleException(e);
176 }
177 }
178
179 /**
180 * @see java.sql.Connection#getMetaData()
181 */
182 public DatabaseMetaData getMetaData() throws SQLException {
183 try {
184 return getDelegatedConnection().getMetaData();
185 } catch (Exception e) {
186 throw handleException(e);
187 }
188 }
189
190 /**
191 * @see java.sql.Connection#setReadOnly(boolean)
192 */
193 public void setReadOnly(boolean readOnly) throws SQLException {
194 try {
195 getDelegatedConnection().setReadOnly(readOnly);
196 } catch (Exception e) {
197 throw handleException(e);
198 }
199 }
200
201 /**
202 * @see java.sql.Connection#isReadOnly()
203 */
204 public boolean isReadOnly() throws SQLException {
205 try {
206 return getDelegatedConnection().isReadOnly();
207 } catch (Exception e) {
208 throw handleException(e);
209 }
210 }
211
212 /**
213 * @see java.sql.Connection#setCatalog(java.lang.String)
214 */
215 public void setCatalog(String catalog) throws SQLException {
216 try {
217 getDelegatedConnection().setCatalog(catalog);
218 } catch (Exception e) {
219 throw handleException(e);
220 }
221 }
222
223 /**
224 * @see java.sql.Connection#getCatalog()
225 */
226 public String getCatalog() throws SQLException {
227 try {
228
229 return getDelegatedConnection().getCatalog();
230 } catch (Exception e) {
231 throw handleException(e);
232 }
233 }
234
235 /**
236 * @see java.sql.Connection#setTransactionIsolation(int)
237 */
238 public void setTransactionIsolation(int level) throws SQLException {
239 try {
240 getDelegatedConnection().setTransactionIsolation(level);
241 } catch (Exception e) {
242 throw handleException(e);
243 }
244 }
245
246 /**
247 * @see java.sql.Connection#getTransactionIsolation()
248 */
249 public int getTransactionIsolation() throws SQLException {
250 try {
251 return getDelegatedConnection().getTransactionIsolation();
252 } catch (Exception e) {
253 throw handleException(e);
254 }
255 }
256
257 /**
258 * @see java.sql.Connection#getWarnings()
259 */
260 public SQLWarning getWarnings() throws SQLException {
261 try {
262 return getDelegatedConnection().getWarnings();
263 } catch (Exception e) {
264 throw handleException(e);
265 }
266 }
267
268 /**
269 * @see java.sql.Connection#clearWarnings()
270 */
271 public void clearWarnings() throws SQLException {
272 try {
273 getDelegatedConnection().clearWarnings();
274 } catch (Exception e) {
275 throw handleException(e);
276 }
277 }
278
279 /**
280 * @see java.sql.Connection#createStatement(int, int)
281 */
282 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
283 try {
284 return getDelegatedConnection().createStatement(resultSetType, resultSetConcurrency);
285 } catch (Exception e) {
286 throw handleException(e);
287 }
288 }
289
290 /**
291 * @see java.sql.Connection#prepareStatement(java.lang.String, int, int)
292 */
293 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
294 try {
295 return getDelegatedConnection().prepareStatement(sql, resultSetType, resultSetConcurrency);
296 } catch (Exception e) {
297 throw handleException(e);
298 }
299 }
300
301 /**
302 * @see java.sql.Connection#prepareCall(java.lang.String, int, int)
303 */
304 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
305 try {
306 return getDelegatedConnection().prepareCall(sql, resultSetType, resultSetConcurrency);
307 } catch (Exception e) {
308 throw handleException(e);
309 }
310 }
311
312 /**
313 * @see java.sql.Connection#getTypeMap()
314 */
315 public Map getTypeMap() throws SQLException {
316 try {
317 return getDelegatedConnection().getTypeMap();
318 } catch (Exception e) {
319 throw handleException(e);
320 }
321 }
322
323 /**
324 * @see java.sql.Connection#setTypeMap(java.util.Map)
325 */
326 public void setTypeMap(Map map) throws SQLException {
327 try {
328 getDelegatedConnection().setTypeMap(map);
329 } catch (Exception e) {
330 throw handleException(e);
331 }
332 }
333
334 /**
335 * @see java.sql.Connection#setHoldability(int)
336 */
337 public void setHoldability(int holdability) throws SQLException {
338 try {
339 getDelegatedConnection().setHoldability(holdability);
340 } catch (Exception e) {
341 throw handleException(e);
342 }
343 }
344
345 /**
346 * @see java.sql.Connection#getHoldability()
347 */
348 public int getHoldability() throws SQLException {
349 try {
350 return getDelegatedConnection().getHoldability();
351 } catch (Exception e) {
352 throw handleException(e);
353 }
354 }
355
356 /**
357 * @see java.sql.Connection#setSavepoint()
358 */
359 public Savepoint setSavepoint() throws SQLException {
360 try {
361 return getDelegatedConnection().setSavepoint();
362 } catch (Exception e) {
363 throw handleException(e);
364 }
365 }
366
367 /**
368 * @see java.sql.Connection#setSavepoint(java.lang.String)
369 */
370 public Savepoint setSavepoint(String name) throws SQLException {
371 try {
372 return getDelegatedConnection().setSavepoint(name);
373 } catch (Exception e) {
374 throw handleException(e);
375 }
376 }
377
378 /**
379 * @see java.sql.Connection#rollback(java.sql.Savepoint)
380 */
381 public void rollback(Savepoint savepoint) throws SQLException {
382 try {
383 getDelegatedConnection().rollback(savepoint);
384 } catch (Exception e) {
385 throw handleException(e);
386 }
387 }
388
389 /**
390 * @see java.sql.Connection#releaseSavepoint(java.sql.Savepoint)
391 */
392 public void releaseSavepoint(Savepoint savepoint) throws SQLException {
393 try {
394 getDelegatedConnection().releaseSavepoint(savepoint);
395 } catch (Exception e) {
396 throw handleException(e);
397 }
398 }
399
400 /**
401 * @see java.sql.Connection#createStatement(int, int, int)
402 */
403 public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
404 try {
405 return getDelegatedConnection().createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
406 } catch (Exception e) {
407 throw handleException(e);
408 }
409 }
410
411 /**
412 * @see java.sql.Connection#prepareStatement(java.lang.String, int, int, int)
413 */
414 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
415 try {
416 return getDelegatedConnection().prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
417 } catch (Exception e) {
418 throw handleException(e);
419 }
420 }
421
422 /**
423 * @see java.sql.Connection#prepareCall(java.lang.String, int, int, int)
424 */
425 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
426 try {
427 return getDelegatedConnection().prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
428 } catch (Exception e) {
429 throw handleException(e);
430 }
431 }
432
433 /**
434 * @see java.sql.Connection#prepareStatement(java.lang.String, int)
435 */
436 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
437 try {
438 return getDelegatedConnection().prepareStatement(sql, autoGeneratedKeys);
439 } catch (Exception e) {
440 throw handleException(e);
441 }
442 }
443
444 /**
445 * @see java.sql.Connection#prepareStatement(java.lang.String, int[])
446 */
447 public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
448 try {
449 return getDelegatedConnection().prepareStatement(sql, columnIndexes);
450 } catch (Exception e) {
451 throw handleException(e);
452 }
453 }
454
455 /**
456 * @see java.sql.Connection#prepareStatement(java.lang.String, java.lang.String[])
457 */
458 public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
459 try {
460 return getDelegatedConnection().prepareStatement(sql, columnNames);
461 } catch (Exception e) {
462 throw handleException(e);
463 }
464 }
465
466 }