001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.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 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.component.jdbcpool;
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.Savepoint;
035: import java.sql.Statement;
036:
037: import org.ow2.util.log.Log;
038: import org.ow2.util.log.LogFactory;
039:
040: /**
041: * This class represent a connection linked to the physical and XA connections.
042: * All errors are reported to the managed connection.
043: * This connection is returned to the client.
044: * @author Philippe Durieux
045: * @author Florent Benoit
046: */
047: public class JConnection implements Connection {
048:
049: /**
050: * Logger used for debug.
051: */
052: private static Log logger = LogFactory.getLog(JConnection.class);
053:
054: /**
055: * JDBC connection provided by the DriverManager.
056: */
057: private Connection physicalConnection = null;
058:
059: /**
060: * XA connection which receive events.
061: */
062: private JManagedConnection xaConnection = null;
063:
064: /**
065: * Buils a Connection (viewed by the user) which rely on a Managed
066: * connection and a physical connection.
067: * @param xaConnection the XA connection.
068: * @param physicalConnection the connection to the database.
069: */
070: public JConnection(final JManagedConnection xaConnection,
071: final Connection physicalConnection) {
072: this .xaConnection = xaConnection;
073: this .physicalConnection = physicalConnection;
074: }
075:
076: /**
077: * Gets the physical connection to the database.
078: * @return physical connection to the database
079: */
080: public Connection getConnection() {
081: return physicalConnection;
082: }
083:
084: /**
085: * {@inheritDoc}
086: */
087: public Statement createStatement() throws SQLException {
088: try {
089: return physicalConnection.createStatement();
090: } catch (SQLException e) {
091: xaConnection.notifyError(e);
092: throw e;
093: }
094: }
095:
096: /**
097: * {@inheritDoc}
098: */
099: public PreparedStatement prepareStatement(final String sql)
100: throws SQLException {
101: try {
102: // Ask the Managed Connection to find one in the pool, if possible.
103: return xaConnection.prepareStatement(sql);
104: } catch (SQLException e) {
105: xaConnection.notifyError(e);
106: throw e;
107: }
108: }
109:
110: /**
111: * {@inheritDoc}
112: */
113: public CallableStatement prepareCall(final String sql)
114: throws SQLException {
115: try {
116: return physicalConnection.prepareCall(sql);
117: } catch (SQLException e) {
118: xaConnection.notifyError(e);
119: throw e;
120: }
121: }
122:
123: /**
124: * {@inheritDoc}
125: */
126: public String nativeSQL(final String sql) throws SQLException {
127: try {
128: return physicalConnection.nativeSQL(sql);
129: } catch (SQLException e) {
130: xaConnection.notifyError(e);
131: throw e;
132: }
133: }
134:
135: /**
136: * @return true if the connection to the database is closed or not.
137: * @throws SQLException if a database access error occurs
138: */
139: public boolean isPhysicallyClosed() throws SQLException {
140: return physicalConnection.isClosed();
141: }
142:
143: /**
144: * {@inheritDoc}
145: */
146: public boolean isClosed() throws SQLException {
147:
148: // TODO : This should return the status of the connection viewed from
149: // the user,
150: // not the physical connection!
151: try {
152: return physicalConnection.isClosed();
153: } catch (SQLException e) {
154: xaConnection.notifyError(e);
155: throw e;
156: }
157: }
158:
159: /**
160: * {@inheritDoc}
161: */
162: public DatabaseMetaData getMetaData() throws SQLException {
163: try {
164: return physicalConnection.getMetaData();
165: } catch (SQLException e) {
166: xaConnection.notifyError(e);
167: throw e;
168: }
169: }
170:
171: /**
172: * {@inheritDoc}
173: */
174: public void setReadOnly(final boolean readOnly) throws SQLException {
175: try {
176: physicalConnection.setReadOnly(readOnly);
177: } catch (SQLException e) {
178: xaConnection.notifyError(e);
179: throw e;
180: }
181: }
182:
183: /**
184: * {@inheritDoc}
185: */
186: public boolean isReadOnly() throws SQLException {
187: try {
188: return physicalConnection.isReadOnly();
189: } catch (SQLException e) {
190: xaConnection.notifyError(e);
191: throw e;
192: }
193: }
194:
195: /**
196: * {@inheritDoc}
197: */
198: public void setCatalog(final String catalog) throws SQLException {
199: try {
200: physicalConnection.setCatalog(catalog);
201: } catch (SQLException e) {
202: xaConnection.notifyError(e);
203: throw e;
204: }
205: }
206:
207: /**
208: * {@inheritDoc}
209: */
210: public String getCatalog() throws SQLException {
211: try {
212: return physicalConnection.getCatalog();
213: } catch (SQLException e) {
214: xaConnection.notifyError(e);
215: throw e;
216: }
217: }
218:
219: /**
220: * Trigger an event to the listener.
221: * @exception SQLException if a database access error occurs
222: */
223: public void close() throws SQLException {
224: xaConnection.notifyClose();
225: }
226:
227: /**
228: * {@inheritDoc}
229: */
230: public void setTransactionIsolation(final int level)
231: throws SQLException {
232: try {
233: physicalConnection.setTransactionIsolation(level);
234: } catch (SQLException e) {
235: xaConnection.notifyError(e);
236: throw e;
237: }
238: }
239:
240: /**
241: * {@inheritDoc}
242: */
243: public int getTransactionIsolation() throws SQLException {
244: try {
245: return physicalConnection.getTransactionIsolation();
246: } catch (SQLException e) {
247: xaConnection.notifyError(e);
248: throw e;
249: }
250: }
251:
252: /**
253: * {@inheritDoc}
254: */
255: public SQLWarning getWarnings() throws SQLException {
256: try {
257: return physicalConnection.getWarnings();
258: } catch (SQLException e) {
259: xaConnection.notifyError(e);
260: throw e;
261: }
262: }
263:
264: /**
265: * {@inheritDoc}
266: */
267: public void clearWarnings() throws SQLException {
268: try {
269: physicalConnection.clearWarnings();
270: } catch (SQLException e) {
271: xaConnection.notifyError(e);
272: throw e;
273: }
274: }
275:
276: /**
277: * In a JDBC-XA driver, Connection.commit is only called if we are outside a
278: * global transaction. {@inheritDoc}
279: */
280: public void commit() throws SQLException {
281: try {
282: physicalConnection.commit();
283: } catch (SQLException e) {
284: xaConnection.notifyError(e);
285: throw e;
286: }
287: }
288:
289: /**
290: * In a JDBC-XA driver, Connection.rollback is only called if we are outside
291: * a global transaction. {@inheritDoc}
292: */
293: public void rollback() throws SQLException {
294: try {
295: physicalConnection.rollback();
296: } catch (SQLException e) {
297: xaConnection.notifyError(e);
298: throw e;
299: }
300: }
301:
302: /**
303: * In a JDBC-XA driver, autocommit is false if we are in a global Tx.
304: * {@inheritDoc}
305: */
306: @SuppressWarnings("boxing")
307: public void setAutoCommit(final boolean autoCommit)
308: throws SQLException {
309: try {
310: physicalConnection.setAutoCommit(autoCommit);
311: } catch (SQLException e) {
312: logger
313: .error("setAutoCommit( {0} ) failed: ", autoCommit,
314: e);
315: xaConnection.notifyError(e);
316: throw e;
317: }
318: }
319:
320: /**
321: * In a JDBC-XA driver, autocommit is false if we are in a global Tx.
322: * {@inheritDoc}
323: */
324: public boolean getAutoCommit() throws SQLException {
325: try {
326: return physicalConnection.getAutoCommit();
327: } catch (SQLException e) {
328: xaConnection.notifyError(e);
329: throw e;
330: }
331: }
332:
333: /**
334: * {@inheritDoc}
335: */
336: public Statement createStatement(final int resultSetType,
337: final int resultSetConcurrency) throws SQLException {
338: try {
339: return physicalConnection.createStatement(resultSetType,
340: resultSetConcurrency);
341: } catch (SQLException e) {
342: xaConnection.notifyError(e);
343: throw e;
344: }
345: }
346:
347: /**
348: * {@inheritDoc}
349: */
350: public Map<String, Class<?>> getTypeMap() throws SQLException {
351: try {
352: return physicalConnection.getTypeMap();
353: } catch (SQLException e) {
354: xaConnection.notifyError(e);
355: throw e;
356: }
357: }
358:
359: /**
360: * {@inheritDoc}
361: */
362: public void setTypeMap(final Map<String, Class<?>> map)
363: throws SQLException {
364: try {
365: physicalConnection.setTypeMap(map);
366: } catch (SQLException e) {
367: xaConnection.notifyError(e);
368: throw e;
369: }
370: }
371:
372: /**
373: * {@inheritDoc}
374: */
375: public PreparedStatement prepareStatement(final String sql,
376: final int resultSetType, final int resultSetConcurrency)
377: throws SQLException {
378: try {
379: return xaConnection.prepareStatement(sql, resultSetType,
380: resultSetConcurrency);
381: } catch (SQLException e) {
382: xaConnection.notifyError(e);
383: throw e;
384: }
385: }
386:
387: /**
388: * {@inheritDoc}
389: */
390: public CallableStatement prepareCall(final String sql,
391: final int resultSetType, final int resultSetConcurrency)
392: throws SQLException {
393: try {
394: return physicalConnection.prepareCall(sql, resultSetType,
395: resultSetConcurrency);
396: } catch (SQLException e) {
397: xaConnection.notifyError(e);
398: throw e;
399: }
400: }
401:
402: /**
403: * {@inheritDoc}
404: */
405: public Statement createStatement(final int resultSetType,
406: final int resultSetConcurrency,
407: final int resultSetHoldability) throws SQLException {
408: try {
409: return physicalConnection.createStatement(resultSetType,
410: resultSetConcurrency, resultSetHoldability);
411: } catch (SQLException e) {
412: xaConnection.notifyError(e);
413: throw e;
414: }
415: }
416:
417: /**
418: * {@inheritDoc}
419: */
420: public int getHoldability() throws SQLException {
421: try {
422: return physicalConnection.getHoldability();
423: } catch (SQLException e) {
424: xaConnection.notifyError(e);
425: throw e;
426: }
427: }
428:
429: /**
430: * {@inheritDoc}
431: */
432: public CallableStatement prepareCall(final String sql,
433: final int resultSetType, final int resultSetConcurrency,
434: final int resultSetHoldability) throws SQLException {
435: try {
436: return physicalConnection.prepareCall(sql, resultSetType,
437: resultSetConcurrency, resultSetHoldability);
438: } catch (SQLException e) {
439: xaConnection.notifyError(e);
440: throw e;
441: }
442: }
443:
444: /**
445: * {@inheritDoc}
446: */
447: public PreparedStatement prepareStatement(final String sql,
448: final int autoGeneratedKeys) throws SQLException {
449: try {
450: return physicalConnection.prepareStatement(sql,
451: autoGeneratedKeys);
452: } catch (SQLException e) {
453: xaConnection.notifyError(e);
454: throw e;
455: }
456: }
457:
458: /**
459: * {@inheritDoc}
460: */
461: public PreparedStatement prepareStatement(final String sql,
462: final int resultSetType, final int resultSetConcurrency,
463: final int resultSetHoldability) throws SQLException {
464: try {
465: return physicalConnection.prepareStatement(sql,
466: resultSetType, resultSetConcurrency,
467: resultSetHoldability);
468: } catch (SQLException e) {
469: xaConnection.notifyError(e);
470: throw e;
471: }
472: }
473:
474: /**
475: * {@inheritDoc}
476: */
477: public PreparedStatement prepareStatement(final String sql,
478: final int[] columnIndexes) throws SQLException {
479: try {
480: return physicalConnection.prepareStatement(sql,
481: columnIndexes);
482: } catch (SQLException e) {
483: xaConnection.notifyError(e);
484: throw e;
485: }
486: }
487:
488: /**
489: * {@inheritDoc}
490: */
491: public PreparedStatement prepareStatement(final String sql,
492: final String[] columnNames) throws SQLException {
493: try {
494: return physicalConnection
495: .prepareStatement(sql, columnNames);
496: } catch (SQLException e) {
497: xaConnection.notifyError(e);
498: throw e;
499: }
500: }
501:
502: /**
503: * {@inheritDoc}
504: */
505: public void releaseSavepoint(final Savepoint savepoint)
506: throws SQLException {
507: try {
508: physicalConnection.releaseSavepoint(savepoint);
509: } catch (SQLException e) {
510: xaConnection.notifyError(e);
511: throw e;
512: }
513: }
514:
515: /**
516: * {@inheritDoc}
517: */
518: public void rollback(final Savepoint savepoint) throws SQLException {
519: try {
520: physicalConnection.rollback(savepoint);
521: } catch (SQLException e) {
522: xaConnection.notifyError(e);
523: throw e;
524: }
525: }
526:
527: /**
528: * {@inheritDoc}
529: */
530: public void setHoldability(final int holdability)
531: throws SQLException {
532: try {
533: physicalConnection.setHoldability(holdability);
534: } catch (SQLException e) {
535: xaConnection.notifyError(e);
536: throw e;
537: }
538: }
539:
540: /**
541: * {@inheritDoc}
542: */
543: public Savepoint setSavepoint() throws SQLException {
544: try {
545: return physicalConnection.setSavepoint();
546: } catch (SQLException e) {
547: xaConnection.notifyError(e);
548: throw e;
549: }
550: }
551:
552: /**
553: * {@inheritDoc}
554: */
555: public java.sql.Savepoint setSavepoint(final String name)
556: throws SQLException {
557: try {
558: return physicalConnection.setSavepoint(name);
559: } catch (SQLException e) {
560: xaConnection.notifyError(e);
561: throw e;
562: }
563: }
564:
565: }
|