001: /*
002: * XAPool: Open Source XA JDBC Pool
003: * Copyright (C) 2003 Objectweb.org
004: * Initial Developer: Lutris Technologies Inc.
005: * Contact: xapool-public@lists.debian-sf.objectweb.org
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: */
022: package org.enhydra.jdbc.core;
023:
024: import org.enhydra.jdbc.util.JdbcUtil;
025:
026: import java.sql.CallableStatement;
027: import java.sql.Connection;
028: import java.sql.DatabaseMetaData;
029: import java.sql.PreparedStatement;
030: import java.sql.SQLException;
031: import java.sql.SQLWarning;
032: import java.sql.Statement;
033: import java.util.Map;
034:
035: /**
036: * This is an implementation of java.sql.Connection which simply delegates
037: * everything to an underlying physical implemention of the same interface.
038: */
039: public abstract class CoreConnection extends JdbcUtil implements
040: Connection {
041: public Connection con; // the physical database connection
042:
043: /**
044: * Constructor
045: */
046: public CoreConnection(Connection con) {
047: this .con = con;
048: }
049:
050: public CoreConnection() {
051: }
052:
053: public boolean isClosed() throws SQLException {
054: return con.isClosed();
055: }
056:
057: public void clearWarnings() throws SQLException {
058: preInvoke();
059: try {
060: con.clearWarnings();
061: } catch (SQLException e) {
062: catchInvoke(e);
063: }
064: }
065:
066: public void close() throws SQLException {
067: preInvoke();
068: try {
069: con.close();
070: } catch (SQLException e) {
071: catchInvoke(e);
072: }
073: }
074:
075: public void commit() throws SQLException {
076: preInvoke();
077: try {
078: con.commit();
079: } catch (SQLException e) {
080: catchInvoke(e);
081: }
082: }
083:
084: public Statement createStatement() throws SQLException {
085: preInvoke();
086: try {
087: return con.createStatement();
088: } catch (SQLException e) {
089: catchInvoke(e);
090: }
091: return null;
092: }
093:
094: public Statement createStatement(int resultSetType,
095: int resultSetConcurrency) throws SQLException {
096: preInvoke();
097: try {
098: return con.createStatement(resultSetType,
099: resultSetConcurrency);
100: } catch (SQLException e) {
101: catchInvoke(e);
102: }
103: return null;
104: }
105:
106: public boolean getAutoCommit() throws SQLException {
107: preInvoke();
108: try {
109: return con.getAutoCommit();
110: } catch (SQLException e) {
111: catchInvoke(e);
112: }
113: return false;
114: }
115:
116: public String getCatalog() throws SQLException {
117: preInvoke();
118: try {
119: return con.getCatalog();
120: } catch (SQLException e) {
121: catchInvoke(e);
122: }
123: return null;
124: }
125:
126: public DatabaseMetaData getMetaData() throws SQLException {
127: preInvoke();
128: try {
129: return con.getMetaData();
130: } catch (SQLException e) {
131: catchInvoke(e);
132: }
133: return null;
134: }
135:
136: public int getTransactionIsolation() throws SQLException {
137: preInvoke();
138: try {
139: return con.getTransactionIsolation();
140: } catch (SQLException e) {
141: catchInvoke(e);
142: }
143: return 0;
144: }
145:
146: public Map getTypeMap() throws SQLException {
147: preInvoke();
148: try {
149: return con.getTypeMap();
150: } catch (SQLException e) {
151: catchInvoke(e);
152: }
153: return null;
154: }
155:
156: public SQLWarning getWarnings() throws SQLException {
157: preInvoke();
158: try {
159: return con.getWarnings();
160: } catch (SQLException e) {
161: catchInvoke(e);
162: }
163: return null;
164: }
165:
166: public boolean isReadOnly() throws SQLException {
167: preInvoke();
168: try {
169: return con.isReadOnly();
170: } catch (SQLException e) {
171: catchInvoke(e);
172: }
173: return false;
174: }
175:
176: public String nativeSQL(String sql) throws SQLException {
177: preInvoke();
178: try {
179: return con.nativeSQL(sql);
180: } catch (SQLException e) {
181: catchInvoke(e);
182: }
183: return null;
184: }
185:
186: public CallableStatement prepareCall(String sql)
187: throws SQLException {
188: preInvoke();
189: try {
190: return con.prepareCall(sql);
191: } catch (SQLException e) {
192: catchInvoke(e);
193: }
194: return null;
195: }
196:
197: public PreparedStatement prepareStatement(String sql)
198: throws SQLException {
199: preInvoke();
200: try {
201: return con.prepareStatement(sql);
202: } catch (SQLException e) {
203: catchInvoke(e);
204: }
205: return null;
206: }
207:
208: public PreparedStatement prepareStatement(String sql,
209: int resultSetType, int resultSetConcurrency)
210: throws SQLException {
211: preInvoke();
212: try {
213: return con.prepareStatement(sql, resultSetType,
214: resultSetConcurrency);
215: } catch (SQLException e) {
216: catchInvoke(e);
217: }
218: return null;
219: }
220:
221: public void rollback() throws SQLException {
222: preInvoke();
223: try {
224: con.rollback();
225: } catch (SQLException e) {
226: catchInvoke(e);
227: }
228: }
229:
230: public void setAutoCommit(boolean autoCommit) throws SQLException {
231: boolean con_autocommit = con.getAutoCommit();
232: log.debug("CoreConnection:Setautocommit autoCommit was = "
233: + con_autocommit);
234: log.debug("CoreConnection:Setautocommit = " + autoCommit);
235: preInvoke();
236: try {
237: if (autoCommit != con_autocommit) { // lets not set the same state
238: // again and again
239: con.setAutoCommit(autoCommit);
240: } else {
241: log.debug("CoreConnection:Setautocommit = SKIPPED ");
242: }
243: } catch (SQLException e) {
244: catchInvoke(e);
245: }
246:
247: }
248:
249: public void setCatalog(String catalog) throws SQLException {
250: preInvoke();
251: try {
252: con.setCatalog(catalog);
253: } catch (SQLException e) {
254: catchInvoke(e);
255: }
256: }
257:
258: public void setReadOnly(boolean readOnly) throws SQLException {
259: preInvoke();
260: try {
261: con.setReadOnly(readOnly);
262: } catch (SQLException e) {
263: catchInvoke(e);
264: }
265: }
266:
267: public void setTransactionIsolation(int level) throws SQLException {
268: preInvoke();
269: try {
270: con.setTransactionIsolation(level);
271: } catch (SQLException e) {
272: catchInvoke(e);
273: }
274: }
275:
276: public void setTypeMap(Map map) throws SQLException {
277: preInvoke();
278: try {
279: con.setTypeMap(map);
280: } catch (SQLException e) {
281: catchInvoke(e);
282: }
283: }
284:
285: /*
286: * Add those following methods to compile on JDK 1.4. Instead those methods
287: * are defined in the java.sql.Connection interface only since JDK 1.4.
288: */
289: public Statement createStatement(int resultSetType,
290: int resultSetConcurrency, int resultSetHoldability)
291: throws SQLException {
292: preInvoke();
293: try {
294: return con.createStatement(resultSetType,
295: resultSetConcurrency, resultSetHoldability);
296: } catch (SQLException e) {
297: catchInvoke(e);
298: }
299: return null;
300: }
301:
302: public int getHoldability() throws SQLException {
303: preInvoke();
304: try {
305: return con.getHoldability();
306: } catch (SQLException e) {
307: catchInvoke(e);
308: }
309: return 0;
310: }
311:
312: public CallableStatement prepareCall(String sql, int resultSetType,
313: int resultSetConcurrency, int resultSetHoldability)
314: throws SQLException {
315: preInvoke();
316: try {
317: return con.prepareCall(sql, resultSetType,
318: resultSetConcurrency, resultSetHoldability);
319: } catch (SQLException e) {
320: catchInvoke(e);
321: }
322: return null;
323: }
324:
325: public PreparedStatement prepareStatement(String sql,
326: int autoGeneratedKeys) throws SQLException {
327: preInvoke();
328: try {
329: return con.prepareStatement(sql, autoGeneratedKeys);
330: } catch (SQLException e) {
331: catchInvoke(e);
332: }
333: return null;
334: }
335:
336: public PreparedStatement prepareStatement(String sql,
337: int resultSetType, int resultSetConcurrency,
338: int resultSetHoldability) throws SQLException {
339: preInvoke();
340: try {
341: return prepareStatement(sql, resultSetType,
342: resultSetConcurrency, resultSetHoldability);
343: } catch (SQLException e) {
344: catchInvoke(e);
345: }
346: return null;
347: }
348:
349: public PreparedStatement prepareStatement(String sql,
350: int[] columnIndexes) throws SQLException {
351: preInvoke();
352: try {
353: return con.prepareStatement(sql, columnIndexes);
354: } catch (SQLException e) {
355: catchInvoke(e);
356: }
357: return null;
358: }
359:
360: public PreparedStatement prepareStatement(String sql,
361: String[] columnNames) throws SQLException {
362: preInvoke();
363: try {
364: return con.prepareStatement(sql, columnNames);
365: } catch (SQLException e) {
366: catchInvoke(e);
367: }
368: return null;
369: }
370:
371: public void releaseSavepoint(java.sql.Savepoint savepoint)
372: throws SQLException {
373: preInvoke();
374: try {
375: con.releaseSavepoint(savepoint);
376: } catch (SQLException e) {
377: catchInvoke(e);
378: }
379:
380: }
381:
382: public void rollback(java.sql.Savepoint savepoint)
383: throws SQLException {
384: preInvoke();
385: try {
386: con.rollback(savepoint);
387: } catch (SQLException e) {
388: catchInvoke(e);
389: }
390: }
391:
392: public void setHoldability(int holdability) throws SQLException {
393: preInvoke();
394: try {
395: con.setHoldability(holdability);
396: } catch (SQLException e) {
397: catchInvoke(e);
398: }
399:
400: }
401:
402: public java.sql.Savepoint setSavepoint() throws SQLException {
403: preInvoke();
404: try {
405: return con.setSavepoint();
406: } catch (SQLException e) {
407: catchInvoke(e);
408: }
409: return null;
410: }
411:
412: public java.sql.Savepoint setSavepoint(String name)
413: throws SQLException {
414: preInvoke();
415: try {
416: return con.setSavepoint(name);
417: } catch (SQLException e) {
418: catchInvoke(e);
419: }
420: return null;
421: }
422:
423: /**
424: * Methods used to do some works before and during the catch clause, to
425: * prevent the pool that a connection is broken.
426: */
427: abstract public void preInvoke() throws SQLException;
428:
429: abstract public void catchInvoke(SQLException e)
430: throws SQLException;
431:
432: }
|