001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2005 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: ConnectionImpl.java 9932 2007-01-18 00:03:16Z ehardesty $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jdbc;
025:
026: import java.io.PrintWriter;
027: import java.util.Map;
028:
029: import java.sql.CallableStatement;
030: import java.sql.Connection;
031: import java.sql.DatabaseMetaData;
032: import java.sql.PreparedStatement;
033: import java.sql.SQLException;
034: import java.sql.SQLWarning;
035: import java.sql.Statement;
036:
037: import javax.resource.ResourceException;
038:
039: import org.objectweb.jonas.resource.MCInfo;
040: import org.objectweb.jonas.resource.SQLManager;
041:
042: import org.objectweb.util.monolog.api.BasicLevel;
043: import org.objectweb.util.monolog.api.Logger;
044:
045: /**
046: * The class <b>Connection</b> provides the Connection implementation for
047: * encapsulating JDBC connections
048: * @author Eric hardesty
049: */
050: public class ConnectionImpl implements Connection {
051:
052: protected boolean autocommit_set = false;
053:
054: protected boolean autocommit_unset = false;
055:
056: public Logger trace = null;
057:
058: ManagedConnectionImpl mc = null;
059:
060: Connection connection = null;
061:
062: PrintWriter pw = null;
063:
064: long key = 0;
065:
066: String user = "";
067:
068: private SQLManager conman = null;
069:
070: private MCInfo mci = null;
071:
072: /**
073: * Traces are enabled ?
074: */
075: private final boolean isDebugging;
076:
077: protected ConnectionImpl(ManagedConnectionImpl _mc,
078: Connection _con, long _key, PrintWriter _pw) {
079: mc = _mc;
080: connection = _con;
081: key = _key;
082: pw = _pw;
083: trace = mc.trace;
084: isDebugging = trace.isLoggable(BasicLevel.DEBUG);
085: }
086:
087: public boolean isPhysicallyClosed() throws SQLException {
088: if (isDebugging) {
089: trace.log(BasicLevel.DEBUG, "");
090: }
091: return (connection.isClosed());
092: }
093:
094: public void setJonasInfo(MCInfo _mci, SQLManager _conman) {
095: mci = _mci;
096: conman = _conman;
097: }
098:
099: public void setUser() {
100: try {
101: user = mc.getMetaData().getUserName();
102: } catch (Exception ex) {
103: }
104: }
105:
106: // IMPLEMENTATION OF METHODS FROM THE java.sql.Connection INTERFACE //
107:
108: public void clearWarnings() throws SQLException {
109: if (isDebugging) {
110: trace.log(BasicLevel.DEBUG, "");
111: }
112: try {
113: checkContext();
114: } catch (Exception e) {
115: trace.log(BasicLevel.ERROR, "checkContext error", e);
116: throw new SQLException("JOnAS JDBC: " + e.getMessage());
117: }
118: connection.clearWarnings();
119: }
120:
121: public void close() throws SQLException {
122: if (isDebugging) {
123: trace.log(BasicLevel.DEBUG, "");
124: }
125: try {
126: closeConnectionImpl();
127: } catch (Exception e) {
128: trace.log(BasicLevel.ERROR, "error", e);
129: e.printStackTrace();
130: throw new SQLException("JOnAS JDBC: " + e.getMessage());
131: }
132: }
133:
134: public void commit() throws SQLException {
135: if (isDebugging) {
136: trace.log(BasicLevel.DEBUG, "");
137: }
138: try {
139: checkContext();
140: connection.commit();
141: } catch (Exception e) {
142: trace.log(BasicLevel.ERROR, "error", e);
143: throw new SQLException("JOnAS JDBC: " + e.getMessage());
144: }
145: }
146:
147: public Statement createStatement() throws SQLException {
148: if (isDebugging) {
149: trace.log(BasicLevel.DEBUG, "");
150: }
151: try {
152: checkContext();
153: } catch (Exception e) {
154: trace.log(BasicLevel.ERROR, "checkContext error", e,
155: "ConnectionImpl", "createStatement");
156: throw new SQLException("JOnAS JDBC: " + e.getMessage());
157: }
158: return connection.createStatement();
159: }
160:
161: public Statement createStatement(int resultSetType,
162: int resultSetConcurrency) throws SQLException {
163: if (isDebugging) {
164: trace.log(BasicLevel.DEBUG, "");
165: }
166: try {
167: checkContext();
168: } catch (Exception e) {
169: trace
170: .log(
171: BasicLevel.ERROR,
172: "ConnectionImpl.createStatement: checkContext error",
173: e);
174: throw new SQLException("JOnAS JDBC: " + e.getMessage());
175: }
176: return connection.createStatement(resultSetType,
177: resultSetConcurrency);
178: }
179:
180: public boolean getAutoCommit() throws SQLException {
181: if (isDebugging) {
182: trace.log(BasicLevel.DEBUG, "");
183: }
184: try {
185: checkContext();
186: return connection.getAutoCommit();
187: } catch (Exception e) {
188: trace
189: .log(BasicLevel.ERROR,
190: "ConnectionImpl.getAC error", e);
191: throw new SQLException("JOnAS JDBC: " + e.getMessage());
192: }
193: }
194:
195: public String getCatalog() throws SQLException {
196: if (isDebugging) {
197: trace.log(BasicLevel.DEBUG, "");
198: }
199: try {
200: checkContext();
201: } catch (Exception e) {
202: trace.log(BasicLevel.ERROR,
203: "ConnectionImpl.getCatalog: checkContext error", e);
204: throw new SQLException("JOnAS JDBC: " + e.getMessage());
205: }
206: return connection.getCatalog();
207: }
208:
209: // JDK 1.4 SQL
210: public int getHoldability() throws SQLException {
211: if (isDebugging) {
212: trace.log(BasicLevel.DEBUG, "");
213: }
214: try {
215: checkContext();
216: } catch (Exception e) {
217: trace
218: .log(
219: BasicLevel.ERROR,
220: "ConnectionImpl.getHoldability: checkContext error",
221: e);
222: throw new SQLException("JOnAS JDBC: " + e.getMessage());
223: }
224: return connection.getHoldability();
225: }
226:
227: public DatabaseMetaData getMetaData() throws SQLException {
228: if (isDebugging) {
229: trace.log(BasicLevel.DEBUG, "");
230: }
231: try {
232: checkContext();
233: } catch (Exception e) {
234: trace
235: .log(
236: BasicLevel.ERROR,
237: "ConnectionImpl.getMetaData: checkContext error",
238: e);
239: throw new SQLException("JOnAS JDBC: " + e.getMessage());
240: }
241: return connection.getMetaData();
242: }
243:
244: public int getTransactionIsolation() throws SQLException {
245: if (isDebugging) {
246: trace.log(BasicLevel.DEBUG, "");
247: }
248: try {
249: checkContext();
250: } catch (Exception e) {
251: trace
252: .log(
253: BasicLevel.ERROR,
254: "ConnectionImpl.getTransactionIsolation: checkContext error",
255: e);
256: throw new SQLException("JOnAS JDBC: " + e.getMessage());
257: }
258: return connection.getTransactionIsolation();
259: }
260:
261: public Map getTypeMap() throws SQLException {
262: if (isDebugging) {
263: trace.log(BasicLevel.DEBUG, "");
264: }
265: try {
266: checkContext();
267: } catch (Exception e) {
268: trace.log(BasicLevel.ERROR,
269: "ConnectionImpl.getTypeMap: checkContext error", e);
270: throw new SQLException("JOnAS JDBC: " + e.getMessage());
271: }
272: return connection.getTypeMap();
273: }
274:
275: public SQLWarning getWarnings() throws SQLException {
276: if (isDebugging) {
277: trace.log(BasicLevel.DEBUG, "");
278: }
279: try {
280: checkContext();
281: } catch (Exception e) {
282: trace
283: .log(
284: BasicLevel.ERROR,
285: "ConnectionImpl.getWarnings: checkContext error",
286: e);
287: throw new SQLException("JOnAS JDBC: " + e.getMessage());
288: }
289: return connection.getWarnings();
290: }
291:
292: public boolean isClosed() throws SQLException {
293: if (isDebugging) {
294: trace.log(BasicLevel.DEBUG, "");
295: }
296: return (key == 0);
297: }
298:
299: public boolean isReadOnly() throws SQLException {
300: if (isDebugging) {
301: trace.log(BasicLevel.DEBUG, "");
302: }
303: try {
304: checkContext();
305: } catch (Exception e) {
306: trace.log(BasicLevel.ERROR,
307: "ConnectionImpl.isReadOnly: checkContext error", e);
308: throw new SQLException("JOnAS JDBC: " + e.getMessage());
309: }
310: return connection.isReadOnly();
311: }
312:
313: public String nativeSQL(String sql) throws SQLException {
314: if (isDebugging) {
315: trace.log(BasicLevel.DEBUG, "");
316: }
317: try {
318: checkContext();
319: } catch (Exception e) {
320: trace.log(BasicLevel.ERROR,
321: "ConnectionImpl.nativeSQL: checkContext error", e);
322: throw new SQLException("JOnAS JDBC: " + e.getMessage());
323: }
324: return connection.nativeSQL(sql);
325: }
326:
327: public CallableStatement prepareCall(String sql)
328: throws SQLException {
329: if (isDebugging) {
330: trace.log(BasicLevel.DEBUG, "");
331: }
332: try {
333: checkContext();
334: } catch (Exception e) {
335: trace
336: .log(
337: BasicLevel.ERROR,
338: "ConnectionImpl.prepareCall: checkContext error",
339: e);
340: throw new SQLException("JOnAS JDBC: " + e.getMessage());
341: }
342: return connection.prepareCall(sql);
343: }
344:
345: public CallableStatement prepareCall(String sql, int resultSetType,
346: int resultSetConcurrency) throws SQLException {
347:
348: if (isDebugging) {
349: trace.log(BasicLevel.DEBUG, "");
350: }
351: try {
352: checkContext();
353: } catch (Exception e) {
354: trace
355: .log(
356: BasicLevel.ERROR,
357: "ConnectionImpl.prepareCall: checkContext error",
358: e);
359: throw new SQLException("JOnAS JDBC: " + e.getMessage());
360: }
361: return connection.prepareCall(sql, resultSetType,
362: resultSetConcurrency);
363: }
364:
365: public PreparedStatement prepareStatement(String sql)
366: throws SQLException {
367: if (isDebugging) {
368: trace.log(BasicLevel.DEBUG, "");
369: }
370: try {
371: checkContext();
372: } catch (Exception e) {
373: trace
374: .log(
375: BasicLevel.ERROR,
376: "ConnectionImpl.prepareStatement: checkContext error",
377: e);
378: throw new SQLException("JOnAS JDBC: " + e.getMessage());
379: }
380: if (conman != null && conman.getMaxPstmtPoolSize() >= 0) {
381: return conman.getPStatement(mci, connection, user, sql);
382: } else {
383: return connection.prepareStatement(sql);
384: }
385: }
386:
387: public PreparedStatement prepareStatement(String sql, int rsettype,
388: int rsetconcurrency) throws SQLException {
389: if (isDebugging) {
390: trace.log(BasicLevel.DEBUG, "");
391: }
392: try {
393: checkContext();
394: } catch (Exception e) {
395: trace
396: .log(
397: BasicLevel.ERROR,
398: "ConnectionImpl.prepareStatement: checkContext error",
399: e);
400: throw new SQLException("JOnAS JDBC: " + e.getMessage());
401: }
402: if (conman != null && conman.getMaxPstmtPoolSize() >= 0) {
403: return conman.getPStatement(mci, connection, user, sql,
404: rsettype, rsetconcurrency);
405: } else {
406: return connection.prepareStatement(sql, rsettype,
407: rsetconcurrency);
408: }
409: }
410:
411: public void rollback() throws SQLException {
412: if (isDebugging) {
413: trace.log(BasicLevel.DEBUG, "");
414: }
415: try {
416: checkContext();
417: connection.rollback();
418: } catch (Exception e) {
419: trace.log(BasicLevel.ERROR, e);
420: throw new SQLException("JOnAS JDBC: " + e.getMessage());
421: }
422: }
423:
424: // JDK 1.4 SQL
425:
426: public PreparedStatement prepareStatement(String sql, int rsettype,
427: int rsetconcurrency, int holdability) throws SQLException {
428: if (isDebugging) {
429: trace.log(BasicLevel.DEBUG, "");
430: }
431: try {
432: checkContext();
433: } catch (Exception e) {
434: trace
435: .log(
436: BasicLevel.ERROR,
437: "ConnectionImpl.prepareStatement: checkContext error",
438: e);
439: throw new SQLException("JOnAS JDBC: " + e.getMessage());
440: }
441: if (conman != null && conman.getMaxPstmtPoolSize() >= 0) {
442: return conman.getPStatement(mci, connection, user, sql,
443: rsettype, rsetconcurrency, holdability);
444: } else {
445: return connection.prepareStatement(sql, rsettype,
446: rsetconcurrency, holdability);
447: }
448: }
449:
450: // JDK 1.4 SQL
451:
452: public PreparedStatement prepareStatement(String sql, int[] t)
453: throws SQLException {
454: if (isDebugging) {
455: trace.log(BasicLevel.DEBUG, "");
456: }
457: try {
458: checkContext();
459: } catch (Exception e) {
460: trace
461: .log(
462: BasicLevel.ERROR,
463: "ConnectionImpl.prepareStatement: checkContext error",
464: e);
465: throw new SQLException("JOnAS JDBC: " + e.getMessage());
466: }
467: if (conman != null && conman.getMaxPstmtPoolSize() >= 0) {
468: return conman.getPStatement(mci, connection, user, sql, t);
469: } else {
470: return connection.prepareStatement(sql, t);
471: }
472: }
473:
474: // JDK 1.4 SQL
475: public PreparedStatement prepareStatement(String sql, String[] t)
476: throws SQLException {
477: if (isDebugging) {
478: trace.log(BasicLevel.DEBUG, "");
479: }
480: try {
481: checkContext();
482: } catch (Exception e) {
483: trace
484: .log(
485: BasicLevel.ERROR,
486: "ConnectionImpl.prepareStatement: checkContext error",
487: e);
488: throw new SQLException("JOnAS JDBC: " + e.getMessage());
489: }
490: if (conman != null && conman.getMaxPstmtPoolSize() >= 0) {
491: return conman.getPStatement(mci, connection, user, sql, t);
492: } else {
493: return connection.prepareStatement(sql, t);
494: }
495: }
496:
497: // JDK 1.4 SQL
498: public PreparedStatement prepareStatement(String sql,
499: int autoGenKeys) throws SQLException {
500: if (isDebugging) {
501: trace.log(BasicLevel.DEBUG, "");
502: }
503: try {
504: checkContext();
505: } catch (Exception e) {
506: trace
507: .log(
508: BasicLevel.ERROR,
509: "ConnectionImpl.prepareStatement: checkContext error",
510: e);
511: throw new SQLException("JOnAS JDBC: " + e.getMessage());
512: }
513: if (conman != null && conman.getMaxPstmtPoolSize() >= 0) {
514: return conman.getPStatement(mci, connection, user, sql,
515: autoGenKeys);
516: } else {
517: return connection.prepareStatement(sql, autoGenKeys);
518: }
519: }
520:
521: // JDK 1.4 SQL
522: public CallableStatement prepareCall(String sql, int rsettype,
523: int rsetconcurrency, int holdability) throws SQLException {
524: if (isDebugging) {
525: trace.log(BasicLevel.DEBUG, "");
526: }
527: try {
528: checkContext();
529: } catch (Exception e) {
530: trace
531: .log(
532: BasicLevel.ERROR,
533: "ConnectionImpl.prepareStatement: checkContext error",
534: e);
535: throw new SQLException("JOnAS JDBC: " + e.getMessage());
536: }
537: return connection.prepareCall(sql, rsettype, rsetconcurrency,
538: holdability);
539: }
540:
541: // JDK 1.4 SQL
542: public Statement createStatement(int rsettype, int rsetconcurrency,
543: int holdability) throws SQLException {
544: if (isDebugging) {
545: trace.log(BasicLevel.DEBUG, "");
546: }
547: try {
548: checkContext();
549: } catch (Exception e) {
550: trace
551: .log(
552: BasicLevel.ERROR,
553: "ConnectionImpl.prepareStatement: checkContext error",
554: e);
555: throw new SQLException("JOnAS JDBC: " + e.getMessage());
556: }
557: return connection.createStatement(rsettype, rsetconcurrency,
558: holdability);
559: }
560:
561: // JDK 1.4 SQL
562: public void releaseSavepoint(java.sql.Savepoint sp)
563: throws SQLException {
564: if (isDebugging) {
565: trace.log(BasicLevel.DEBUG, "");
566: }
567: try {
568: checkContext();
569: connection.releaseSavepoint(sp);
570: } catch (Exception e) {
571: trace.log(BasicLevel.ERROR, e);
572: throw new SQLException("JOnAS JDBC: " + e.getMessage());
573: }
574: }
575:
576: // JDK 1.4 SQL
577: public void rollback(java.sql.Savepoint sp) throws SQLException {
578: if (isDebugging) {
579: trace.log(BasicLevel.DEBUG, "");
580: }
581: try {
582: checkContext();
583: connection.rollback(sp);
584: } catch (Exception e) {
585: trace.log(BasicLevel.ERROR, e);
586: throw new SQLException("JOnAS JDBC: " + e.getMessage());
587: }
588: }
589:
590: // JDK 1.4 SQL
591: public java.sql.Savepoint setSavepoint() throws SQLException {
592: if (isDebugging) {
593: trace.log(BasicLevel.DEBUG, "");
594: }
595: try {
596: checkContext();
597: return connection.setSavepoint();
598: } catch (Exception e) {
599: trace.log(BasicLevel.ERROR, e);
600: throw new SQLException("JOnAS JDBC: " + e.getMessage());
601: }
602: }
603:
604: // JDK 1.4 SQL
605: public java.sql.Savepoint setSavepoint(String name)
606: throws SQLException {
607: if (isDebugging) {
608: trace.log(BasicLevel.DEBUG, "");
609: }
610: try {
611: checkContext();
612: return connection.setSavepoint(name);
613: } catch (Exception e) {
614: trace.log(BasicLevel.ERROR, e);
615: throw new SQLException("JOnAS JDBC: " + e.getMessage());
616: }
617: }
618:
619: public void setAutoCommit(boolean isauto) throws SQLException {
620: if (isDebugging) {
621: trace.log(BasicLevel.DEBUG, "isauto=" + isauto);
622: }
623: try {
624: try {
625: checkContext();
626:
627: if (isauto == false) {
628: if (autocommit_unset == false) { // cache for optimization
629: connection.setAutoCommit(false);
630: autocommit_set = false;
631: autocommit_unset = true;
632: }
633: } else {
634: if (autocommit_set == false) { // cache for optimization
635: connection.setAutoCommit(true);
636: autocommit_set = true;
637: autocommit_unset = false;
638: }
639: }
640: } catch (SQLException e) {
641: String s = e.getMessage().toLowerCase();
642: if (s.indexOf("set chained command not allowed") != -1) {
643: if (trace.isLoggable(BasicLevel.DEBUG)) {
644: trace.log(BasicLevel.DEBUG, "failed...");
645: trace.log(BasicLevel.DEBUG,
646: "Committing then retrying");
647: }
648: // These lines for Sybase only, hoping they don't broke
649: // anything for others DBs.
650: connection.commit();
651: connection.setAutoCommit(isauto); // Shouldn't fail now.
652: if (trace.isLoggable(BasicLevel.DEBUG)) {
653: trace.log(BasicLevel.DEBUG,
654: "succeeded after retry");
655: }
656: } else {
657: // rethrow if not sybase
658: throw e;
659: }
660: }
661: } catch (Exception e) {
662: trace.log(BasicLevel.ERROR, "ConnectionImpl.setAC: error",
663: e);
664: throw new SQLException("JOnAS JDBC: " + e.getMessage());
665: }
666: }
667:
668: public void setCatalog(String catalog) throws SQLException {
669: if (isDebugging) {
670: trace.log(BasicLevel.DEBUG, "");
671: }
672: try {
673: checkContext();
674: } catch (Exception e) {
675: trace.log(BasicLevel.ERROR,
676: "ConnectionImpl.setCatalog: checkContext error", e);
677: throw new SQLException("JOnAS JDBC: " + e.getMessage());
678: }
679: connection.setCatalog(catalog);
680: }
681:
682: // JDK 1.4 SQL
683: public void setHoldability(int holdability) throws SQLException {
684: if (isDebugging) {
685: trace.log(BasicLevel.DEBUG, "");
686: }
687: try {
688: checkContext();
689: } catch (Exception e) {
690: trace.log(BasicLevel.ERROR,
691: "ConnectionImpl.setCatalog: checkContext error", e);
692: throw new SQLException("JOnAS JDBC: " + e.getMessage());
693: }
694: connection.setHoldability(holdability);
695: }
696:
697: public void setReadOnly(boolean readOnly) throws SQLException {
698: if (isDebugging) {
699: trace.log(BasicLevel.DEBUG, "");
700: }
701: try {
702: checkContext();
703: } catch (Exception e) {
704: trace
705: .log(
706: BasicLevel.ERROR,
707: "ConnectionImpl.setReadOnly: checkContext error",
708: e);
709: throw new SQLException("JOnAS JDBC: " + e.getMessage());
710: }
711: connection.setReadOnly(readOnly);
712: }
713:
714: public void setTransactionIsolation(int level) throws SQLException {
715: if (isDebugging) {
716: trace.log(BasicLevel.DEBUG, "");
717: }
718: try {
719: checkContext();
720: } catch (Exception e) {
721: trace
722: .log(
723: BasicLevel.ERROR,
724: "ConnectionImpl.setTransactionIsolation: checkContext error",
725: e);
726: throw new SQLException("JOnAS JDBC: " + e.getMessage());
727: }
728: connection.setTransactionIsolation(level);
729: }
730:
731: public void setTypeMap(Map map) throws SQLException {
732: if (isDebugging) {
733: trace.log(BasicLevel.DEBUG, "");
734: }
735: try {
736: checkContext();
737: } catch (Exception e) {
738: trace.log(BasicLevel.ERROR,
739: "ConnectionImpl.setTypeMap: checkContext error", e);
740: throw new SQLException("JOnAS JDBC: " + e.getMessage());
741: }
742: connection.setTypeMap(map);
743: }
744:
745: private void closeConnectionImpl() throws ResourceException {
746: if (key != 0) {
747: mc.close(this );
748: key = 0;
749: }
750: }
751:
752: private void checkContext() throws Exception {
753: if (key == 0) {
754: throw new Exception("Connection is closed");
755: }
756: if (key != mc.getSignature()) {
757: if (mc.getSignature() == 0) {
758: mc.setSignature(key);
759: } else {
760: throw new Exception("Connection w/sig(" + key
761: + ") is not currect active Connection ("
762: + mc.getSignature() + ") for " + mc);
763: }
764: }
765: }
766:
767: public void setSignature(long sig) {
768: key = sig;
769: }
770: }
|