001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JConnection.java 10112 2007-03-28 12:28:53Z durieuxp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.dbm;
025:
026: import java.util.Map;
027:
028: import java.sql.CallableStatement;
029: import java.sql.Connection;
030: import java.sql.DatabaseMetaData;
031: import java.sql.PreparedStatement;
032: import java.sql.SQLException;
033: import java.sql.SQLWarning;
034: import java.sql.Statement;
035:
036: import org.objectweb.jonas.common.Log;
037: import org.objectweb.util.monolog.api.BasicLevel;
038: import org.objectweb.util.monolog.api.Logger;
039:
040: public class JConnection implements Connection {
041:
042: static private Logger logger = Log.getLogger(Log.JONAS_DBM_PREFIX
043: + ".con");
044:
045: protected Connection actConn = null;
046:
047: protected JManagedConnection xac = null;
048:
049: protected boolean autocommit_set = false;
050:
051: protected boolean autocommit_unset = false;
052:
053: static private Class implclass = null;
054:
055: // -----------------------------------------------------------------
056: // Constructors
057: // -----------------------------------------------------------------
058:
059: /**
060: */
061: public JConnection(JManagedConnection xac, Connection actual) {
062: this .xac = xac;
063: actConn = actual;
064: }
065:
066: // -----------------------------------------------------------------
067: // Accessors
068: // -----------------------------------------------------------------
069:
070: /**
071: * Get the actual connection on database
072: */
073: public Connection getConnection() {
074: if (logger.isLoggable(BasicLevel.DEBUG)) {
075: logger.log(BasicLevel.DEBUG, "");
076: }
077: return actConn;
078: }
079:
080: // -----------------------------------------------------------------
081: // Connection implementation
082: // Most of the methods just forward the call to the actual connection.
083: // -----------------------------------------------------------------
084:
085: public Statement createStatement() throws SQLException {
086: logger.log(BasicLevel.DEBUG, "");
087: try {
088: return actConn.createStatement();
089: } catch (SQLException e) {
090: xac.notifyError(e);
091: throw (e);
092: }
093: }
094:
095: public PreparedStatement prepareStatement(String sql)
096: throws SQLException {
097: logger.log(BasicLevel.DEBUG, sql);
098: try {
099: // Ask the Managed Connection to find one in the pool, if possible.
100: return xac.prepareStatement(sql);
101: } catch (SQLException e) {
102: xac.notifyError(e);
103: throw (e);
104: }
105: }
106:
107: public CallableStatement prepareCall(String sql)
108: throws SQLException {
109: logger.log(BasicLevel.DEBUG, "");
110: try {
111: return actConn.prepareCall(sql);
112: } catch (SQLException e) {
113: xac.notifyError(e);
114: throw (e);
115: }
116: }
117:
118: public String nativeSQL(String sql) throws SQLException {
119: logger.log(BasicLevel.DEBUG, "");
120: try {
121: return actConn.nativeSQL(sql);
122: } catch (SQLException e) {
123: xac.notifyError(e);
124: throw (e);
125: }
126: }
127:
128: public boolean isPhysicallyClosed() throws SQLException {
129: logger.log(BasicLevel.DEBUG, "");
130: return actConn.isClosed();
131: }
132:
133: public boolean isClosed() throws SQLException {
134: logger.log(BasicLevel.DEBUG, "");
135:
136: return xac.isClosed();
137: }
138:
139: public DatabaseMetaData getMetaData() throws SQLException {
140: logger.log(BasicLevel.DEBUG, "");
141: try {
142: return actConn.getMetaData();
143: } catch (SQLException e) {
144: xac.notifyError(e);
145: throw (e);
146: }
147: }
148:
149: public void setReadOnly(boolean readOnly) throws SQLException {
150: logger.log(BasicLevel.DEBUG, "");
151: try {
152: actConn.setReadOnly(readOnly);
153: } catch (SQLException e) {
154: xac.notifyError(e);
155: throw (e);
156: }
157: }
158:
159: public boolean isReadOnly() throws SQLException {
160: logger.log(BasicLevel.DEBUG, "");
161: try {
162: return actConn.isReadOnly();
163: } catch (SQLException e) {
164: xac.notifyError(e);
165: throw (e);
166: }
167: }
168:
169: public void setCatalog(String catalog) throws SQLException {
170: logger.log(BasicLevel.DEBUG, "");
171: try {
172: actConn.setCatalog(catalog);
173: } catch (SQLException e) {
174: xac.notifyError(e);
175: throw (e);
176: }
177: }
178:
179: public String getCatalog() throws SQLException {
180: logger.log(BasicLevel.DEBUG, "");
181: try {
182: return actConn.getCatalog();
183: } catch (SQLException e) {
184: xac.notifyError(e);
185: throw (e);
186: }
187: }
188:
189: /**
190: * Trigger an event to the listener.
191: */
192: public void close() throws SQLException {
193: logger.log(BasicLevel.DEBUG, "");
194: xac.notifyClose();
195: }
196:
197: public void setTransactionIsolation(int level) throws SQLException {
198: logger.log(BasicLevel.DEBUG, "");
199: try {
200: actConn.setTransactionIsolation(level);
201: } catch (SQLException e) {
202: xac.notifyError(e);
203: throw (e);
204: }
205: }
206:
207: public int getTransactionIsolation() throws SQLException {
208: logger.log(BasicLevel.DEBUG, "");
209: try {
210: return actConn.getTransactionIsolation();
211: } catch (SQLException e) {
212: xac.notifyError(e);
213: throw (e);
214: }
215: }
216:
217: public SQLWarning getWarnings() throws SQLException {
218: logger.log(BasicLevel.DEBUG, "");
219: try {
220: return actConn.getWarnings();
221: } catch (SQLException e) {
222: xac.notifyError(e);
223: throw (e);
224: }
225: }
226:
227: public void clearWarnings() throws SQLException {
228: logger.log(BasicLevel.DEBUG, "");
229: try {
230: actConn.clearWarnings();
231: } catch (SQLException e) {
232: xac.notifyError(e);
233: throw (e);
234: }
235: }
236:
237: /**
238: * In a JDBC-XA driver, Connection.commit is only called if we are outside a
239: * global transaction.
240: */
241: public void commit() throws SQLException {
242: logger.log(BasicLevel.DEBUG, "local transaction");
243: try {
244: actConn.commit();
245: } catch (SQLException e) {
246: xac.notifyError(e);
247: throw (e);
248: }
249: }
250:
251: /**
252: * In a JDBC-XA driver, Connection.rollback is only called if we are outside
253: * a global transaction.
254: */
255: public void rollback() throws SQLException {
256: logger.log(BasicLevel.DEBUG, "local transaction");
257: try {
258: actConn.rollback();
259: } catch (SQLException e) {
260: xac.notifyError(e);
261: throw (e);
262: }
263: }
264:
265: /**
266: * In a JDBC-XA driver, autocommit is false if we are in a global Tx
267: */
268: public void setAutoCommit(boolean autoCommit) throws SQLException {
269: logger.log(BasicLevel.DEBUG, "");
270: try {
271: if (autoCommit == false) {
272: if (autocommit_unset == false) { // cache for optimization
273: actConn.setAutoCommit(false);
274: autocommit_set = false;
275: autocommit_unset = true;
276: }
277: } else {
278: if (autocommit_set == false) { // cache for optimization
279: actConn.setAutoCommit(true);
280: autocommit_set = true;
281: autocommit_unset = false;
282: }
283: }
284: } catch (SQLException e) {
285: String s = e.getMessage().toLowerCase();
286: if (s.indexOf("set chained command not allowed") != -1) {
287: if (logger.isLoggable(BasicLevel.DEBUG)) {
288: logger.log(BasicLevel.DEBUG, "failed...");
289: logger.log(BasicLevel.DEBUG,
290: "Committing then retrying");
291: }
292: try {
293: // These lines for Sybase only, hoping they don't broke
294: // anything for others DBs.
295: actConn.commit();
296: actConn.setAutoCommit(autoCommit); // Shouldn't fail now.
297: if (logger.isLoggable(BasicLevel.DEBUG)) {
298: logger.log(BasicLevel.DEBUG,
299: "succeeded after retry");
300: }
301: } catch (SQLException se) {
302: logger.log(BasicLevel.ERROR, "" + autoCommit
303: + ") failed again after retry", se);
304: xac.notifyError(e);
305: throw (e);
306: }
307: } else {
308: logger.log(BasicLevel.ERROR, "setAutoCommit("
309: + autoCommit + ") failed: " + e);
310: xac.notifyError(e);
311: throw (e);
312: }
313: }
314: }
315:
316: /**
317: * In a JDBC-XA driver, autocommit is false if we are in a global Tx
318: */
319: public boolean getAutoCommit() throws SQLException {
320: try {
321: return actConn.getAutoCommit();
322: } catch (SQLException e) {
323: xac.notifyError(e);
324: throw (e);
325: }
326: }
327:
328: public Statement createStatement(int resultSetType,
329: int resultSetConcurrency) throws SQLException {
330: logger.log(BasicLevel.DEBUG, "");
331: try {
332: return actConn.createStatement(resultSetType,
333: resultSetConcurrency);
334: } catch (SQLException e) {
335: xac.notifyError(e);
336: throw (e);
337: }
338: }
339:
340: public Map getTypeMap() throws SQLException {
341: logger.log(BasicLevel.DEBUG, "");
342: try {
343: return actConn.getTypeMap();
344: } catch (SQLException e) {
345: xac.notifyError(e);
346: throw (e);
347: }
348: }
349:
350: public void setTypeMap(Map map) throws SQLException {
351: logger.log(BasicLevel.DEBUG, "");
352: try {
353: setTypeMap(map);
354: } catch (SQLException e) {
355: xac.notifyError(e);
356: throw (e);
357: }
358: }
359:
360: public PreparedStatement prepareStatement(String sql,
361: int resultSetType, int resultSetConcurrency)
362: throws SQLException {
363: logger.log(BasicLevel.DEBUG, sql);
364: try {
365: return xac.prepareStatement(sql, resultSetType,
366: resultSetConcurrency);
367: } catch (SQLException e) {
368: xac.notifyError(e);
369: throw (e);
370: }
371: }
372:
373: public CallableStatement prepareCall(String sql, int resultSetType,
374: int resultSetConcurrency) throws SQLException {
375: logger.log(BasicLevel.DEBUG, "");
376: try {
377: return actConn.prepareCall(sql, resultSetType,
378: resultSetConcurrency);
379: } catch (SQLException e) {
380: xac.notifyError(e);
381: throw (e);
382: }
383: }
384:
385: public Statement createStatement(int resultSetType,
386: int resultSetConcurrency, int resultSetHoldability)
387: throws SQLException {
388: logger.log(BasicLevel.DEBUG, "");
389: try {
390: return actConn.createStatement(resultSetType,
391: resultSetConcurrency, resultSetHoldability);
392: } catch (SQLException e) {
393: xac.notifyError(e);
394: throw (e);
395: }
396: }
397:
398: public int getHoldability() throws SQLException {
399: logger.log(BasicLevel.DEBUG, "");
400: try {
401: return actConn.getHoldability();
402: } catch (SQLException e) {
403: xac.notifyError(e);
404: throw (e);
405: }
406: }
407:
408: public CallableStatement prepareCall(String sql, int resultSetType,
409: int resultSetConcurrency, int resultSetHoldability)
410: throws SQLException {
411: logger.log(BasicLevel.DEBUG, "");
412: try {
413: return actConn.prepareCall(sql, resultSetType,
414: resultSetConcurrency, resultSetHoldability);
415: } catch (SQLException e) {
416: xac.notifyError(e);
417: throw (e);
418: }
419: }
420:
421: public PreparedStatement prepareStatement(String sql,
422: int autoGeneratedKeys) throws SQLException {
423: logger.log(BasicLevel.DEBUG, sql);
424: try {
425: return actConn.prepareStatement(sql, autoGeneratedKeys);
426: } catch (SQLException e) {
427: xac.notifyError(e);
428: throw (e);
429: }
430: }
431:
432: public PreparedStatement prepareStatement(String sql,
433: int resultSetType, int resultSetConcurrency,
434: int resultSetHoldability) throws SQLException {
435: logger.log(BasicLevel.DEBUG, sql);
436:
437: try {
438: return actConn.prepareStatement(sql, resultSetType,
439: resultSetConcurrency, resultSetHoldability);
440: } catch (SQLException e) {
441: xac.notifyError(e);
442: throw (e);
443: }
444: }
445:
446: public PreparedStatement prepareStatement(String sql,
447: int[] columnIndexes) throws SQLException {
448: logger.log(BasicLevel.DEBUG, sql);
449: try {
450: return actConn.prepareStatement(sql, columnIndexes);
451: } catch (SQLException e) {
452: xac.notifyError(e);
453: throw (e);
454: }
455: }
456:
457: public PreparedStatement prepareStatement(String sql,
458: String[] columnNames) throws SQLException {
459: logger.log(BasicLevel.DEBUG, sql);
460: try {
461: return actConn.prepareStatement(sql, columnNames);
462: } catch (SQLException e) {
463: xac.notifyError(e);
464: throw (e);
465: }
466: }
467:
468: public void releaseSavepoint(java.sql.Savepoint savepoint)
469: throws SQLException {
470: logger.log(BasicLevel.DEBUG, "");
471: try {
472: actConn.releaseSavepoint(savepoint);
473: } catch (SQLException e) {
474: xac.notifyError(e);
475: throw (e);
476: }
477: }
478:
479: public void rollback(java.sql.Savepoint savepoint)
480: throws SQLException {
481: logger.log(BasicLevel.DEBUG, "");
482: try {
483: actConn.rollback(savepoint);
484: } catch (SQLException e) {
485: xac.notifyError(e);
486: throw (e);
487: }
488: }
489:
490: public void setHoldability(int holdability) throws SQLException {
491: logger.log(BasicLevel.DEBUG, "");
492: try {
493: actConn.setHoldability(holdability);
494: } catch (SQLException e) {
495: xac.notifyError(e);
496: throw (e);
497: }
498: }
499:
500: public java.sql.Savepoint setSavepoint() throws SQLException {
501: logger.log(BasicLevel.DEBUG, "");
502: try {
503: return actConn.setSavepoint();
504: } catch (SQLException e) {
505: xac.notifyError(e);
506: throw (e);
507: }
508: }
509:
510: public java.sql.Savepoint setSavepoint(String name)
511: throws SQLException {
512: logger.log(BasicLevel.DEBUG, "");
513: try {
514: return actConn.setSavepoint(name);
515: } catch (SQLException e) {
516: xac.notifyError(e);
517: throw (e);
518: }
519: }
520:
521: }
|