001: // jTDS JDBC Driver for Microsoft SQL Server and Sybase
002: // Copyright (C) 2004 The jTDS Project
003: //
004: // This library is free software; you can redistribute it and/or
005: // modify it under the terms of the GNU Lesser General Public
006: // License as published by the Free Software Foundation; either
007: // version 2.1 of the License, or (at your option) any later version.
008: //
009: // This library is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: // Lesser General Public License for more details.
013: //
014: // You should have received a copy of the GNU Lesser General Public
015: // License along with this library; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: //
018: package net.sourceforge.jtds.jdbcx.proxy;
019:
020: import java.sql.*;
021: import java.util.Map;
022:
023: import net.sourceforge.jtds.jdbc.*;
024: import net.sourceforge.jtds.jdbcx.*;
025:
026: /**
027: * This class would be better implemented as a java.lang.reflect.Proxy. However, this
028: * feature was not added until 1.3 and reflection performance was not improved until 1.4.
029: * Since the driver still needs to be compatible with 1.2 and 1.3 this class is used
030: * to delegate the calls to the connection with minimal overhead.
031: *
032: * @version $Id: ConnectionProxy.java,v 1.7 2005/04/20 16:49:30 alin_sinpalean Exp $
033: */
034: public class ConnectionProxy implements Connection {
035: private PooledConnection _pooledConnection;
036: private ConnectionJDBC2 _connection;
037: private boolean _closed;
038:
039: /**
040: * Constructs a new connection proxy.
041: */
042: public ConnectionProxy(PooledConnection pooledConnection,
043: Connection connection) {
044: _pooledConnection = pooledConnection;
045: _connection = (ConnectionJDBC2) connection;
046: }
047:
048: /**
049: * Delgates calls to the connection; SQLExceptions thrown from the connection
050: * will cause an event to be fired on the connection pool listeners.
051: *
052: * @throws SQLException if an error occurs
053: */
054: public void clearWarnings() throws SQLException {
055: validateConnection();
056:
057: try {
058: _connection.clearWarnings();
059: } catch (SQLException sqlException) {
060: processSQLException(sqlException);
061: }
062: }
063:
064: /**
065: * Delgates calls to the connection; SQLExceptions thrown from the connection
066: * will cause an event to be fired on the connection pool listeners.
067: */
068: public void close() {
069: if (_closed) {
070: return;
071: }
072:
073: _pooledConnection.fireConnectionEvent(true, null);
074: _closed = true;
075: }
076:
077: /**
078: * Delgates calls to the connection; SQLExceptions thrown from the connection
079: * will cause an event to be fired on the connection pool listeners.
080: *
081: * @throws SQLException if an error occurs
082: */
083: public void commit() throws SQLException {
084: validateConnection();
085:
086: try {
087: _connection.commit();
088: } catch (SQLException sqlException) {
089: processSQLException(sqlException);
090: }
091: }
092:
093: /**
094: * Delgates calls to the connection; SQLExceptions thrown from the connection
095: * will cause an event to be fired on the connection pool listeners.
096: *
097: * @throws SQLException if an error occurs
098: */
099: public Statement createStatement() throws SQLException {
100: validateConnection();
101:
102: try {
103: return new StatementProxy(this , (JtdsStatement) _connection
104: .createStatement());
105: } catch (SQLException sqlException) {
106: processSQLException(sqlException);
107: }
108:
109: return null;
110: }
111:
112: /**
113: * Delgates calls to the connection; SQLExceptions thrown from the connection
114: * will cause an event to be fired on the connection pool listeners.
115: *
116: * @throws SQLException if an error occurs
117: */
118: public Statement createStatement(int resultSetType,
119: int resultSetConcurrency) throws SQLException {
120: validateConnection();
121:
122: try {
123: return new StatementProxy(this , (JtdsStatement) _connection
124: .createStatement(resultSetType,
125: resultSetConcurrency));
126: } catch (SQLException sqlException) {
127: processSQLException(sqlException);
128: }
129:
130: return null;
131: }
132:
133: /**
134: * Delgates calls to the connection; SQLExceptions thrown from the connection
135: * will cause an event to be fired on the connection pool listeners.
136: *
137: * @throws SQLException if an error occurs
138: */
139: public Statement createStatement(int resultSetType,
140: int resultSetConcurrency, int resultSetHoldability)
141: throws SQLException {
142: validateConnection();
143:
144: try {
145: return new StatementProxy(this , (JtdsStatement) _connection
146: .createStatement(resultSetType,
147: resultSetConcurrency, resultSetHoldability));
148: } catch (SQLException sqlException) {
149: processSQLException(sqlException);
150: }
151:
152: return null;
153: }
154:
155: /**
156: * Delgates calls to the connection; SQLExceptions thrown from the connection
157: * will cause an event to be fired on the connection pool listeners.
158: *
159: * @throws SQLException if an error occurs
160: */
161: public boolean getAutoCommit() throws SQLException {
162: validateConnection();
163:
164: try {
165: return _connection.getAutoCommit();
166: } catch (SQLException sqlException) {
167: processSQLException(sqlException);
168: }
169:
170: return false;
171: }
172:
173: /**
174: * Delgates calls to the connection; SQLExceptions thrown from the connection
175: * will cause an event to be fired on the connection pool listeners.
176: *
177: * @throws SQLException if an error occurs
178: */
179: public String getCatalog() throws SQLException {
180: validateConnection();
181:
182: try {
183: return _connection.getCatalog();
184: } catch (SQLException sqlException) {
185: processSQLException(sqlException);
186: }
187:
188: return null;
189: }
190:
191: /**
192: * Delgates calls to the connection; SQLExceptions thrown from the connection
193: * will cause an event to be fired on the connection pool listeners.
194: *
195: * @throws SQLException if an error occurs
196: */
197: public int getHoldability() throws SQLException {
198: validateConnection();
199:
200: try {
201: return _connection.getHoldability();
202: } catch (SQLException sqlException) {
203: processSQLException(sqlException);
204: }
205:
206: return Integer.MIN_VALUE;
207: }
208:
209: /**
210: * Delgates calls to the connection; SQLExceptions thrown from the connection
211: * will cause an event to be fired on the connection pool listeners.
212: *
213: * @throws SQLException if an error occurs
214: */
215: public int getTransactionIsolation() throws SQLException {
216: validateConnection();
217:
218: try {
219: return _connection.getTransactionIsolation();
220: } catch (SQLException sqlException) {
221: processSQLException(sqlException);
222: }
223:
224: return Integer.MIN_VALUE;
225: }
226:
227: /**
228: * Delgates calls to the connection; SQLExceptions thrown from the connection
229: * will cause an event to be fired on the connection pool listeners.
230: *
231: * @throws SQLException if an error occurs
232: */
233: public Map getTypeMap() throws SQLException {
234: validateConnection();
235:
236: try {
237: return _connection.getTypeMap();
238: } catch (SQLException sqlException) {
239: processSQLException(sqlException);
240: }
241:
242: return null;
243: }
244:
245: /**
246: * Delgates calls to the connection; SQLExceptions thrown from the connection
247: * will cause an event to be fired on the connection pool listeners.
248: *
249: * @throws SQLException if an error occurs
250: */
251: public SQLWarning getWarnings() throws SQLException {
252: validateConnection();
253:
254: try {
255: return _connection.getWarnings();
256: } catch (SQLException sqlException) {
257: processSQLException(sqlException);
258: }
259:
260: return null;
261: }
262:
263: /**
264: * Delgates calls to the connection; SQLExceptions thrown from the connection
265: * will cause an event to be fired on the connection pool listeners.
266: *
267: * @throws SQLException if an error occurs
268: */
269: public DatabaseMetaData getMetaData() throws SQLException {
270: validateConnection();
271:
272: try {
273: return _connection.getMetaData();
274: } catch (SQLException sqlException) {
275: processSQLException(sqlException);
276: }
277:
278: return null;
279: }
280:
281: /**
282: * Delgates calls to the connection; SQLExceptions thrown from the connection
283: * will cause an event to be fired on the connection pool listeners.
284: *
285: * @throws SQLException if an error occurs
286: */
287: public boolean isClosed() throws SQLException {
288: if (_closed) {
289: return true;
290: }
291:
292: try {
293: return _connection.isClosed();
294: } catch (SQLException sqlException) {
295: processSQLException(sqlException);
296: }
297:
298: return _closed;
299: }
300:
301: /**
302: * Delgates calls to the connection; SQLExceptions thrown from the connection
303: * will cause an event to be fired on the connection pool listeners.
304: *
305: * @throws SQLException if an error occurs
306: */
307: public boolean isReadOnly() throws SQLException {
308: validateConnection();
309:
310: try {
311: return _connection.isReadOnly();
312: } catch (SQLException sqlException) {
313: processSQLException(sqlException);
314: }
315:
316: return false;
317: }
318:
319: /**
320: * Delgates calls to the connection; SQLExceptions thrown from the connection
321: * will cause an event to be fired on the connection pool listeners.
322: *
323: * @throws SQLException if an error occurs
324: */
325: public String nativeSQL(String sql) throws SQLException {
326: validateConnection();
327:
328: try {
329: return _connection.nativeSQL(sql);
330: } catch (SQLException sqlException) {
331: processSQLException(sqlException);
332: }
333:
334: return null;
335: }
336:
337: /**
338: * Delgates calls to the connection; SQLExceptions thrown from the connection
339: * will cause an event to be fired on the connection pool listeners.
340: *
341: * @throws SQLException if an error occurs
342: */
343: public CallableStatement prepareCall(String sql)
344: throws SQLException {
345: validateConnection();
346:
347: try {
348: return new CallableStatementProxy(this ,
349: (JtdsCallableStatement) _connection
350: .prepareCall(sql));
351: } catch (SQLException sqlException) {
352: processSQLException(sqlException);
353: }
354:
355: return null;
356: }
357:
358: /**
359: * Delgates calls to the connection; SQLExceptions thrown from the connection
360: * will cause an event to be fired on the connection pool listeners.
361: *
362: * @throws SQLException if an error occurs
363: */
364: public CallableStatement prepareCall(String sql, int resultSetType,
365: int resultSetConcurrency) throws SQLException {
366: validateConnection();
367:
368: try {
369: return new CallableStatementProxy(this ,
370: (JtdsCallableStatement) _connection.prepareCall(
371: sql, resultSetType, resultSetConcurrency));
372: } catch (SQLException sqlException) {
373: processSQLException(sqlException);
374: }
375:
376: return null;
377: }
378:
379: /**
380: * Delgates calls to the connection; SQLExceptions thrown from the connection
381: * will cause an event to be fired on the connection pool listeners.
382: *
383: * @throws SQLException if an error occurs
384: */
385: public CallableStatement prepareCall(String sql, int resultSetType,
386: int resultSetConcurrency, int resultSetHoldability)
387: throws SQLException {
388: validateConnection();
389:
390: try {
391: return new CallableStatementProxy(this ,
392: (JtdsCallableStatement) _connection.prepareCall(
393: sql, resultSetType, resultSetConcurrency,
394: resultSetHoldability));
395: } catch (SQLException sqlException) {
396: processSQLException(sqlException);
397: }
398:
399: return null;
400: }
401:
402: /**
403: * Delgates calls to the connection; SQLExceptions thrown from the connection
404: * will cause an event to be fired on the connection pool listeners.
405: *
406: * @throws SQLException if an error occurs
407: */
408: public PreparedStatement prepareStatement(String sql)
409: throws SQLException {
410: validateConnection();
411:
412: try {
413: return new PreparedStatementProxy(this ,
414: (JtdsPreparedStatement) _connection
415: .prepareStatement(sql));
416: } catch (SQLException sqlException) {
417: processSQLException(sqlException);
418: }
419:
420: return null;
421: }
422:
423: /**
424: * Delgates calls to the connection; SQLExceptions thrown from the connection
425: * will cause an event to be fired on the connection pool listeners.
426: *
427: * @throws SQLException if an error occurs
428: */
429: public PreparedStatement prepareStatement(String sql,
430: int autoGeneratedKeys) throws SQLException {
431: validateConnection();
432:
433: try {
434: return new PreparedStatementProxy(this ,
435: (JtdsPreparedStatement) _connection
436: .prepareStatement(sql, autoGeneratedKeys));
437: } catch (SQLException sqlException) {
438: processSQLException(sqlException);
439: }
440:
441: return null;
442: }
443:
444: /**
445: * Delgates calls to the connection; SQLExceptions thrown from the connection
446: * will cause an event to be fired on the connection pool listeners.
447: *
448: * @throws SQLException if an error occurs
449: */
450: public PreparedStatement prepareStatement(String sql,
451: int[] columnIndexes) throws SQLException {
452: validateConnection();
453:
454: try {
455: return new PreparedStatementProxy(this ,
456: (JtdsPreparedStatement) _connection
457: .prepareStatement(sql, columnIndexes));
458: } catch (SQLException sqlException) {
459: processSQLException(sqlException);
460: }
461:
462: return null;
463: }
464:
465: /**
466: * Delgates calls to the connection; SQLExceptions thrown from the connection
467: * will cause an event to be fired on the connection pool listeners.
468: *
469: * @throws SQLException if an error occurs
470: */
471: public PreparedStatement prepareStatement(String sql,
472: String[] columnNames) throws SQLException {
473: validateConnection();
474:
475: try {
476: return new PreparedStatementProxy(this ,
477: (JtdsPreparedStatement) _connection
478: .prepareStatement(sql, columnNames));
479: } catch (SQLException sqlException) {
480: processSQLException(sqlException);
481: }
482:
483: return null;
484: }
485:
486: /**
487: * Delgates calls to the connection; SQLExceptions thrown from the connection
488: * will cause an event to be fired on the connection pool listeners.
489: *
490: * @throws SQLException if an error occurs
491: */
492: public PreparedStatement prepareStatement(String sql,
493: int resultSetType, int resultSetConcurrency)
494: throws SQLException {
495: validateConnection();
496:
497: try {
498: return new PreparedStatementProxy(this ,
499: (JtdsPreparedStatement) _connection
500: .prepareStatement(sql, resultSetType,
501: resultSetConcurrency));
502: } catch (SQLException sqlException) {
503: processSQLException(sqlException);
504: }
505:
506: return null;
507: }
508:
509: /**
510: * Delgates calls to the connection; SQLExceptions thrown from the connection
511: * will cause an event to be fired on the connection pool listeners.
512: *
513: * @throws SQLException if an error occurs
514: */
515: public PreparedStatement prepareStatement(String sql,
516: int resultSetType, int resultSetConcurrency,
517: int resultSetHoldability) throws SQLException {
518: validateConnection();
519:
520: try {
521: return new PreparedStatementProxy(this ,
522: (JtdsPreparedStatement) _connection
523: .prepareStatement(sql, resultSetType,
524: resultSetConcurrency,
525: resultSetHoldability));
526: } catch (SQLException sqlException) {
527: processSQLException(sqlException);
528: }
529:
530: return null;
531: }
532:
533: /**
534: * Delgates calls to the connection; SQLExceptions thrown from the connection
535: * will cause an event to be fired on the connection pool listeners.
536: *
537: * @throws SQLException if an error occurs
538: */
539: public void releaseSavepoint(Savepoint savepoint)
540: throws SQLException {
541: validateConnection();
542:
543: try {
544: _connection.releaseSavepoint(savepoint);
545: } catch (SQLException sqlException) {
546: processSQLException(sqlException);
547: }
548: }
549:
550: /**
551: * Delgates calls to the connection; SQLExceptions thrown from the connection
552: * will cause an event to be fired on the connection pool listeners.
553: *
554: * @throws SQLException if an error occurs
555: */
556: public void rollback() throws SQLException {
557: validateConnection();
558:
559: try {
560: _connection.rollback();
561: } catch (SQLException sqlException) {
562: processSQLException(sqlException);
563: }
564: }
565:
566: /**
567: * Delgates calls to the connection; SQLExceptions thrown from the connection
568: * will cause an event to be fired on the connection pool listeners.
569: *
570: * @throws SQLException if an error occurs
571: */
572: public void rollback(Savepoint savepoint) throws SQLException {
573: validateConnection();
574:
575: try {
576: _connection.rollback(savepoint);
577: } catch (SQLException sqlException) {
578: processSQLException(sqlException);
579: }
580: }
581:
582: /**
583: * Delgates calls to the connection; SQLExceptions thrown from the connection
584: * will cause an event to be fired on the connection pool listeners.
585: *
586: * @throws SQLException if an error occurs
587: */
588: public void setAutoCommit(boolean autoCommit) throws SQLException {
589: validateConnection();
590:
591: try {
592: _connection.setAutoCommit(autoCommit);
593: } catch (SQLException sqlException) {
594: processSQLException(sqlException);
595: }
596: }
597:
598: /**
599: * Delgates calls to the connection; SQLExceptions thrown from the connection
600: * will cause an event to be fired on the connection pool listeners.
601: *
602: * @throws SQLException if an error occurs
603: */
604: public void setCatalog(String catalog) throws SQLException {
605: validateConnection();
606:
607: try {
608: _connection.setCatalog(catalog);
609: } catch (SQLException sqlException) {
610: processSQLException(sqlException);
611: }
612: }
613:
614: /**
615: * Delgates calls to the connection; SQLExceptions thrown from the connection
616: * will cause an event to be fired on the connection pool listeners.
617: *
618: * @throws SQLException if an error occurs
619: */
620: public void setHoldability(int holdability) throws SQLException {
621: validateConnection();
622:
623: try {
624: _connection.setHoldability(holdability);
625: } catch (SQLException sqlException) {
626: processSQLException(sqlException);
627: }
628: }
629:
630: /**
631: * Delgates calls to the connection; SQLExceptions thrown from the connection
632: * will cause an event to be fired on the connection pool listeners.
633: *
634: * @throws SQLException if an error occurs
635: */
636: public void setReadOnly(boolean readOnly) throws SQLException {
637: validateConnection();
638:
639: try {
640: _connection.setReadOnly(readOnly);
641: } catch (SQLException sqlException) {
642: processSQLException(sqlException);
643: }
644: }
645:
646: /**
647: * Delgates calls to the connection; SQLExceptions thrown from the connection
648: * will cause an event to be fired on the connection pool listeners.
649: *
650: * @throws SQLException if an error occurs
651: */
652: public Savepoint setSavepoint() throws SQLException {
653: validateConnection();
654:
655: try {
656: return _connection.setSavepoint();
657: } catch (SQLException sqlException) {
658: processSQLException(sqlException);
659: }
660:
661: return null;
662: }
663:
664: /**
665: * Delgates calls to the connection; SQLExceptions thrown from the connection
666: * will cause an event to be fired on the connection pool listeners.
667: *
668: * @throws SQLException if an error occurs
669: */
670: public Savepoint setSavepoint(String name) throws SQLException {
671: validateConnection();
672:
673: try {
674: return _connection.setSavepoint(name);
675: } catch (SQLException sqlException) {
676: processSQLException(sqlException);
677: }
678:
679: return null;
680: }
681:
682: /**
683: * Delgates calls to the connection; SQLExceptions thrown from the connection
684: * will cause an event to be fired on the connection pool listeners.
685: *
686: * @throws SQLException if an error occurs
687: */
688: public void setTransactionIsolation(int level) throws SQLException {
689: validateConnection();
690:
691: try {
692: _connection.setTransactionIsolation(level);
693: } catch (SQLException sqlException) {
694: processSQLException(sqlException);
695: }
696: }
697:
698: /**
699: * Delgates calls to the connection; SQLExceptions thrown from the connection
700: * will cause an event to be fired on the connection pool listeners.
701: *
702: * @throws SQLException if an error occurs
703: */
704: public void setTypeMap(Map map) throws SQLException {
705: validateConnection();
706:
707: try {
708: _connection.setTypeMap(map);
709: } catch (SQLException sqlException) {
710: processSQLException(sqlException);
711: }
712: }
713:
714: /**
715: * Validates the connection state.
716: */
717: private void validateConnection() throws SQLException {
718: if (_closed) {
719: throw new SQLException(Messages
720: .get("error.conproxy.noconn"), "HY010");
721: }
722: }
723:
724: /**
725: * Processes SQLExceptions.
726: */
727: void processSQLException(SQLException sqlException)
728: throws SQLException {
729: _pooledConnection.fireConnectionEvent(false, sqlException);
730:
731: throw sqlException;
732: }
733:
734: /**
735: * Closes the proxy, releasing the connection.
736: */
737: protected void finalize() {
738: close();
739: }
740: }
|