KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jdbc_xa > ConnectionImpl


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ConnectionImpl.java,v 1.12 2005/04/28 08:43:25 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26
27 package org.objectweb.jonas.jdbc_xa;
28
29 import java.sql.*;
30 import java.util.Map JavaDoc;
31
32 import org.objectweb.jonas.common.Log;
33 import org.objectweb.util.monolog.api.Logger;
34 import org.objectweb.util.monolog.api.BasicLevel;
35
36 /**
37  * This class is a wrapper class on a standard java.sql.Connection class.
38  * Its goal is to prevent explicit close of a connection and transaction
39  * management (should be done via XAResource)
40  * @author Philippe Durieux
41  * Contributor(s):
42  * oe Gittings/Dmitry Melekhov for Sybase pb.
43  */

44 public class ConnectionImpl implements Connection {
45
46     static private Logger logger = null;
47
48     protected Connection actConn = null;
49     protected XAConnectionImpl xac = null;
50     protected boolean autocommit_set = false;
51     protected boolean autocommit_unset = false;
52     static private Class JavaDoc implclass = null;
53
54     // -----------------------------------------------------------------
55
// Constructors
56
// -----------------------------------------------------------------
57

58     /**
59      */

60     public ConnectionImpl(XAConnectionImpl xac, Connection actual) {
61         this.xac = xac;
62         actConn = actual;
63         logger = Log.getLogger(Log.JONAS_JDBCXA_PREFIX);
64     }
65
66
67
68     // -----------------------------------------------------------------
69
// Accessors
70
// -----------------------------------------------------------------
71

72     /**
73      * Get the actual connection on database
74      */

75     public Connection getConnection() {
76         if (logger.isLoggable(BasicLevel.DEBUG)) {
77             logger.log(BasicLevel.DEBUG, "");
78         }
79         return actConn;
80     }
81
82     // -----------------------------------------------------------------
83
// Connection implementation
84
// Most of the methods just forward the call to the actual connection.
85
// -----------------------------------------------------------------
86

87     public Statement createStatement()
88         throws SQLException {
89         if (logger.isLoggable(BasicLevel.DEBUG)) {
90             logger.log(BasicLevel.DEBUG, "");
91         }
92         try {
93             return actConn.createStatement();
94         } catch (SQLException e) {
95             xac.notifyError(e);
96             throw(e);
97         }
98     }
99
100
101
102     public PreparedStatement prepareStatement(String JavaDoc sql)
103         throws SQLException {
104         if (logger.isLoggable(BasicLevel.DEBUG)) {
105             logger.log(BasicLevel.DEBUG, "");
106         }
107         try {
108             return actConn.prepareStatement(sql);
109         } catch (SQLException e) {
110             xac.notifyError(e);
111             throw(e);
112         }
113     }
114
115     public CallableStatement prepareCall(String JavaDoc sql)
116         throws SQLException {
117         if (logger.isLoggable(BasicLevel.DEBUG)) {
118             logger.log(BasicLevel.DEBUG, "");
119         }
120         try {
121             return actConn.prepareCall(sql);
122         } catch (SQLException e) {
123             xac.notifyError(e);
124             throw(e);
125         }
126     }
127
128     public String JavaDoc nativeSQL(String JavaDoc sql)
129         throws SQLException {
130         if (logger.isLoggable(BasicLevel.DEBUG)) {
131             logger.log(BasicLevel.DEBUG, "");
132         }
133         try {
134             return actConn.nativeSQL(sql);
135         } catch (SQLException e) {
136             xac.notifyError(e);
137             throw(e);
138         }
139     }
140
141     public boolean isPhysicallyClosed()
142         throws SQLException {
143         if (logger.isLoggable(BasicLevel.DEBUG)) {
144             logger.log(BasicLevel.DEBUG, "");
145         }
146         return actConn.isClosed();
147     }
148
149     public boolean isClosed()
150         throws SQLException {
151         if (logger.isLoggable(BasicLevel.DEBUG)) {
152             logger.log(BasicLevel.DEBUG, "");
153         }
154
155         // TODO : This should return the status of the connection viewed from the user,
156
// not the physical connection!
157
try {
158             return actConn.isClosed();
159         } catch (SQLException e) {
160             xac.notifyError(e);
161             throw(e);
162         }
163     }
164
165     public DatabaseMetaData getMetaData()
166         throws SQLException {
167         if (logger.isLoggable(BasicLevel.DEBUG)) {
168             logger.log(BasicLevel.DEBUG, "");
169         }
170         try {
171             return actConn.getMetaData();
172         } catch (SQLException e) {
173             xac.notifyError(e);
174             throw(e);
175         }
176     }
177
178     public void setReadOnly(boolean readOnly)
179     throws SQLException {
180         if (logger.isLoggable(BasicLevel.DEBUG)) {
181             logger.log(BasicLevel.DEBUG, "");
182         }
183         try {
184             actConn.setReadOnly(readOnly);
185         } catch (SQLException e) {
186             xac.notifyError(e);
187             throw(e);
188         }
189     }
190
191     public boolean isReadOnly()
192     throws SQLException {
193         if (logger.isLoggable(BasicLevel.DEBUG)) {
194             logger.log(BasicLevel.DEBUG, "");
195         }
196         try {
197             return actConn.isReadOnly();
198         } catch (SQLException e) {
199             xac.notifyError(e);
200             throw(e);
201         }
202     }
203
204     public void setCatalog(String JavaDoc catalog)
205     throws SQLException {
206         if (logger.isLoggable(BasicLevel.DEBUG)) {
207             logger.log(BasicLevel.DEBUG, "");
208         }
209         try {
210             actConn.setCatalog(catalog);
211         } catch (SQLException e) {
212             xac.notifyError(e);
213             throw(e);
214         }
215     }
216
217     public String JavaDoc getCatalog()
218     throws SQLException {
219         if (logger.isLoggable(BasicLevel.DEBUG)) {
220             logger.log(BasicLevel.DEBUG, "");
221         }
222         try {
223             return actConn.getCatalog();
224         } catch (SQLException e) {
225             xac.notifyError(e);
226             throw(e);
227         }
228     }
229
230     /**
231      * Trigger an event to the listener.
232      */

233     public void close()
234     throws SQLException {
235         if (logger.isLoggable(BasicLevel.DEBUG)) {
236             logger.log(BasicLevel.DEBUG, "");
237         }
238         xac.notifyClose();
239     }
240
241     public void setTransactionIsolation(int level)
242     throws SQLException {
243         if (logger.isLoggable(BasicLevel.DEBUG)) {
244             logger.log(BasicLevel.DEBUG, "");
245         }
246         try {
247             actConn.setTransactionIsolation(level);
248         } catch (SQLException e) {
249             xac.notifyError(e);
250             throw(e);
251         }
252     }
253
254     public int getTransactionIsolation()
255     throws SQLException {
256         if (logger.isLoggable(BasicLevel.DEBUG)) {
257             logger.log(BasicLevel.DEBUG, "");
258         }
259         try {
260             return actConn.getTransactionIsolation();
261         } catch (SQLException e) {
262             xac.notifyError(e);
263             throw(e);
264         }
265     }
266
267     public SQLWarning getWarnings()
268     throws SQLException {
269         if (logger.isLoggable(BasicLevel.DEBUG)) {
270             logger.log(BasicLevel.DEBUG, "");
271         }
272         try {
273             return actConn.getWarnings();
274         } catch (SQLException e) {
275             xac.notifyError(e);
276             throw(e);
277         }
278     }
279
280     public void clearWarnings()
281     throws SQLException {
282         if (logger.isLoggable(BasicLevel.DEBUG)) {
283             logger.log(BasicLevel.DEBUG, "");
284         }
285         try {
286             actConn.clearWarnings();
287         } catch (SQLException e) {
288             xac.notifyError(e);
289             throw(e);
290         }
291     }
292
293     /**
294      * In a JDBC-XA driver, Connection.commit is only called if we
295      * are outside a global transaction.
296      */

297     public void commit()
298     throws SQLException {
299         if (logger.isLoggable(BasicLevel.DEBUG)) {
300             logger.log(BasicLevel.DEBUG, "local transaction");
301         }
302         try {
303             actConn.commit();
304         } catch (SQLException e) {
305             xac.notifyError(e);
306             throw(e);
307         }
308     }
309
310     /**
311      * In a JDBC-XA driver, Connection.rollback is only called if we
312      * are outside a global transaction.
313      */

314     public void rollback()
315     throws SQLException {
316         if (logger.isLoggable(BasicLevel.DEBUG)) {
317             logger.log(BasicLevel.DEBUG, "local transaction");
318         }
319         try {
320             actConn.rollback();
321         } catch (SQLException e) {
322             xac.notifyError(e);
323             throw(e);
324         }
325     }
326
327     /**
328      * In a JDBC-XA driver, autocommit is false if we are in a global Tx
329      */

330     public void setAutoCommit(boolean autoCommit)
331     throws SQLException {
332
333         if (logger.isLoggable(BasicLevel.DEBUG)) {
334             logger.log(BasicLevel.DEBUG, " ");
335         }
336         try {
337             if (autoCommit == false) {
338                 if (autocommit_unset == false) { // cache for optimization
339
actConn.setAutoCommit(false);
340                     autocommit_set = false;
341                     autocommit_unset = true;
342                 }
343             } else {
344                 if (autocommit_set == false) { // cache for optimization
345
actConn.setAutoCommit(true);
346                     autocommit_set = true;
347                     autocommit_unset = false;
348                 }
349             }
350         } catch (SQLException e) {
351             String JavaDoc s = e.getMessage().toLowerCase();
352             if (s.indexOf("set chained command not allowed") != -1) {
353                 if (logger.isLoggable(BasicLevel.DEBUG)) {
354                     logger.log(BasicLevel.DEBUG, "failed...");
355                     logger.log(BasicLevel.DEBUG, "Committing then retrying");
356                 }
357                 try {
358                     // These lines for Sybase only, hoping they don't broke
359
// anything for others DBs.
360
actConn.commit();
361                     actConn.setAutoCommit(autoCommit); // Shouldn't fail now.
362
if (logger.isLoggable(BasicLevel.DEBUG)) {
363                         logger.log(BasicLevel.DEBUG, "succeeded after retry");
364                     }
365                 } catch (SQLException se) {
366                     logger.log(BasicLevel.ERROR,"" + autoCommit + ") failed again after retry", se);
367                     xac.notifyError(e);
368                     throw(e);
369                 }
370             } else {
371                 logger.log(BasicLevel.ERROR,"setAutoCommit(" + autoCommit + ") failed: "+e);
372                 xac.notifyError(e);
373                 throw(e);
374             }
375         }
376     }
377
378     /**
379      * In a JDBC-XA driver, autocommit is false if we are in a global Tx
380      */

381     public boolean getAutoCommit()
382     throws SQLException {
383
384         try {
385             return actConn.getAutoCommit();
386         } catch (SQLException e) {
387             xac.notifyError(e);
388             throw(e);
389         }
390     }
391
392     public Statement createStatement(int resultSetType, int resultSetConcurrency)
393         throws SQLException {
394
395         if (logger.isLoggable(BasicLevel.DEBUG)) {
396             logger.log(BasicLevel.DEBUG, "");
397         }
398         try {
399             return actConn.createStatement(resultSetType, resultSetConcurrency);
400         } catch (SQLException e) {
401             xac.notifyError(e);
402             throw(e);
403         }
404     }
405
406     public Map JavaDoc getTypeMap()
407     throws SQLException {
408
409         if (logger.isLoggable(BasicLevel.DEBUG)) {
410             logger.log(BasicLevel.DEBUG, "");
411         }
412         try {
413             return actConn.getTypeMap();
414         } catch (SQLException e) {
415             xac.notifyError(e);
416             throw(e);
417         }
418     }
419
420     public void setTypeMap(Map JavaDoc map)
421     throws SQLException {
422
423         if (logger.isLoggable(BasicLevel.DEBUG)) {
424             logger.log(BasicLevel.DEBUG, "");
425         }
426         try {
427             setTypeMap(map);
428         } catch (SQLException e) {
429             xac.notifyError(e);
430             throw(e);
431         }
432     }
433
434     public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency)
435         throws SQLException {
436
437         if (logger.isLoggable(BasicLevel.DEBUG)) {
438             logger.log(BasicLevel.DEBUG, "");
439         }
440         try {
441             return actConn.prepareStatement(sql, resultSetType, resultSetConcurrency);
442         } catch (SQLException e) {
443             xac.notifyError(e);
444             throw(e);
445         }
446     }
447
448     public CallableStatement prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency)
449         throws SQLException {
450
451         if (logger.isLoggable(BasicLevel.DEBUG)) {
452             logger.log(BasicLevel.DEBUG, "");
453         }
454         try {
455             return actConn.prepareCall(sql, resultSetType, resultSetConcurrency);
456         } catch (SQLException e) {
457             xac.notifyError(e);
458             throw(e);
459         }
460     }
461
462     public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
463     throws SQLException {
464         if (logger.isLoggable(BasicLevel.DEBUG)) {
465             logger.log(BasicLevel.DEBUG, "");
466         }
467         try {
468             return actConn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
469         } catch (SQLException e) {
470             xac.notifyError(e);
471             throw(e);
472         }
473     }
474
475     public int getHoldability() throws SQLException {
476         if (logger.isLoggable(BasicLevel.DEBUG)) {
477             logger.log(BasicLevel.DEBUG, "");
478         }
479         try {
480             return actConn.getHoldability();
481         } catch (SQLException e) {
482             xac.notifyError(e);
483             throw(e);
484         }
485     }
486
487     public CallableStatement prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
488     throws SQLException {
489
490         if (logger.isLoggable(BasicLevel.DEBUG)) {
491             logger.log(BasicLevel.DEBUG, "");
492         }
493         try {
494             return actConn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
495         } catch (SQLException e) {
496             xac.notifyError(e);
497             throw(e);
498         }
499     }
500
501     public PreparedStatement prepareStatement(String JavaDoc sql, int autoGeneratedKeys)
502         throws SQLException {
503
504         if (logger.isLoggable(BasicLevel.DEBUG)) {
505             logger.log(BasicLevel.DEBUG, "");
506         }
507         try {
508             return actConn.prepareStatement(sql, autoGeneratedKeys);
509         } catch (SQLException e) {
510             xac.notifyError(e);
511             throw(e);
512         }
513     }
514
515     public PreparedStatement prepareStatement(String JavaDoc sql,
516                                               int resultSetType,
517                                               int resultSetConcurrency,
518                                               int resultSetHoldability)
519         throws SQLException {
520
521         if (logger.isLoggable(BasicLevel.DEBUG)) {
522             logger.log(BasicLevel.DEBUG, "");
523         }
524
525         try {
526             return actConn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
527         } catch (SQLException e) {
528             xac.notifyError(e);
529             throw(e);
530         }
531     }
532
533     public PreparedStatement prepareStatement(String JavaDoc sql,
534                                               int[] columnIndexes)
535     throws SQLException {
536         if (logger.isLoggable(BasicLevel.DEBUG)) {
537             logger.log(BasicLevel.DEBUG, "");
538         }
539
540         try {
541             return actConn.prepareStatement(sql, columnIndexes);
542         } catch (SQLException e) {
543             xac.notifyError(e);
544             throw(e);
545         }
546     }
547
548     public PreparedStatement prepareStatement(String JavaDoc sql,
549                                               String JavaDoc[] columnNames)
550     throws SQLException {
551         if (logger.isLoggable(BasicLevel.DEBUG)) {
552             logger.log(BasicLevel.DEBUG, "");
553         }
554
555         try {
556             return actConn.prepareStatement(sql, columnNames);
557         } catch (SQLException e) {
558             xac.notifyError(e);
559             throw(e);
560         }
561     }
562
563     public void releaseSavepoint(java.sql.Savepoint JavaDoc savepoint)
564     throws SQLException {
565         if (logger.isLoggable(BasicLevel.DEBUG)) {
566             logger.log(BasicLevel.DEBUG, "");
567         }
568
569         try {
570             actConn.releaseSavepoint(savepoint);
571         } catch (SQLException e) {
572             xac.notifyError(e);
573             throw(e);
574         }
575     }
576
577     public void rollback(java.sql.Savepoint JavaDoc savepoint)
578     throws SQLException {
579         if (logger.isLoggable(BasicLevel.DEBUG)) {
580             logger.log(BasicLevel.DEBUG, "");
581         }
582
583         try {
584             actConn.rollback(savepoint);
585         } catch (SQLException e) {
586             xac.notifyError(e);
587             throw(e);
588         }
589     }
590
591     public void setHoldability(int holdability)
592     throws SQLException {
593         if (logger.isLoggable(BasicLevel.DEBUG)) {
594             logger.log(BasicLevel.DEBUG, "");
595         }
596
597         try {
598             actConn.setHoldability(holdability);
599         } catch (SQLException e) {
600             xac.notifyError(e);
601             throw(e);
602         }
603     }
604
605     public java.sql.Savepoint JavaDoc setSavepoint()
606     throws SQLException {
607         if (logger.isLoggable(BasicLevel.DEBUG)) {
608             logger.log(BasicLevel.DEBUG, "");
609         }
610
611         try {
612             return actConn.setSavepoint();
613         } catch (SQLException e) {
614             xac.notifyError(e);
615             throw(e);
616         }
617     }
618
619     public java.sql.Savepoint JavaDoc setSavepoint(String JavaDoc name)
620     throws SQLException {
621         if (logger.isLoggable(BasicLevel.DEBUG)) {
622             logger.log(BasicLevel.DEBUG, "");
623         }
624
625         try {
626             return actConn.setSavepoint(name);
627         } catch (SQLException e) {
628             xac.notifyError(e);
629             throw(e);
630         }
631     }
632
633 }
634
635
Popular Tags