ConnectionImpl.java

00001 /*
00002  * JOnAS: Java(TM) Open Application Server
00003  * Copyright (C) 1999 Bull S.A.
00004  * Contact: jonas-team@objectweb.org
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA
00020  *
00021  * --------------------------------------------------------------------------
00022  * $Id: ConnectionImpl.java,v 1.11 2004/10/18 11:54:06 durieuxp Exp $
00023  * --------------------------------------------------------------------------
00024  */
00025 
00026 
00027 package org.objectweb.jonas.jdbc_xa;
00028 
00029 import java.sql.*;
00030 import java.util.Map;
00031 
00032 import org.objectweb.jonas.common.Log;
00033 import org.objectweb.util.monolog.api.Logger;
00034 import org.objectweb.util.monolog.api.BasicLevel;
00035 
00044 public class ConnectionImpl implements Connection {
00045         
00046     static private Logger logger = null;
00047         
00048     protected Connection actConn = null;
00049     protected XAConnectionImpl xac = null;
00050     protected boolean autocommit_set = false;
00051     protected boolean autocommit_unset = false;
00052     static private Class implclass = null;
00053         
00054     // -----------------------------------------------------------------
00055     // Constructors
00056     // -----------------------------------------------------------------
00057 
00060     public ConnectionImpl(XAConnectionImpl xac, Connection actual) {
00061         this.xac = xac;
00062         actConn = actual;
00063         logger = Log.getLogger(Log.JONAS_JDBCXA_PREFIX);
00064     }
00065 
00066     
00067 
00068     // -----------------------------------------------------------------
00069     // Accessors
00070     // -----------------------------------------------------------------
00071 
00075     public Connection getConnection() {
00076         logger.log(BasicLevel.DEBUG, "");
00077         return actConn;
00078     }
00079         
00080     // -----------------------------------------------------------------
00081     // Connection implementation
00082     // Most of the methods just forward the call to the actual connection.
00083     // -----------------------------------------------------------------
00084         
00085     public Statement createStatement() 
00086         throws SQLException {
00087         
00088         logger.log(BasicLevel.DEBUG, "");
00089         try {
00090             return actConn.createStatement();
00091         } catch (SQLException e) {
00092             xac.notifyError(e);
00093             throw(e);
00094         }
00095     }
00096 
00097 
00098 
00099     public PreparedStatement prepareStatement(String sql) 
00100         throws SQLException {
00101         
00102         logger.log(BasicLevel.DEBUG, "");
00103         try {
00104             return actConn.prepareStatement(sql);
00105         } catch (SQLException e) {
00106             xac.notifyError(e);
00107             throw(e);
00108         }
00109     }
00110 
00111     public CallableStatement prepareCall(String sql) 
00112         throws SQLException {
00113         
00114         logger.log(BasicLevel.DEBUG, "");
00115         try {
00116             return actConn.prepareCall(sql);
00117         } catch (SQLException e) {
00118             xac.notifyError(e);
00119             throw(e);
00120         }
00121     }
00122         
00123     public String nativeSQL(String sql) 
00124         throws SQLException {
00125         
00126         logger.log(BasicLevel.DEBUG, "");
00127         try {
00128             return actConn.nativeSQL(sql);
00129         } catch (SQLException e) {
00130             xac.notifyError(e);
00131             throw(e);
00132         }
00133     }
00134         
00135     public boolean isPhysicallyClosed() 
00136         throws SQLException {
00137         
00138         logger.log(BasicLevel.DEBUG, "");
00139         return actConn.isClosed();
00140     }
00141 
00142     public boolean isClosed() 
00143         throws SQLException {
00144         
00145         logger.log(BasicLevel.DEBUG, "");
00146 
00147         // TODO : This should return the status of the connection viewed from the user,
00148         // not the physical connection!
00149         try {
00150             return actConn.isClosed();
00151         } catch (SQLException e) {
00152             xac.notifyError(e);
00153             throw(e);
00154         }
00155     }
00156 
00157     public DatabaseMetaData getMetaData() 
00158         throws SQLException {
00159         
00160         logger.log(BasicLevel.DEBUG, "");
00161         try {
00162             return actConn.getMetaData();
00163         } catch (SQLException e) {
00164             xac.notifyError(e);
00165             throw(e);
00166         }
00167     }
00168 
00169     public void setReadOnly(boolean readOnly) 
00170     throws SQLException {
00171         
00172         logger.log(BasicLevel.DEBUG, "");
00173         try {
00174             actConn.setReadOnly(readOnly);
00175         } catch (SQLException e) {
00176             xac.notifyError(e);
00177             throw(e);
00178         }
00179     }
00180 
00181     public boolean isReadOnly() 
00182     throws SQLException {
00183         
00184         logger.log(BasicLevel.DEBUG, "");
00185         try {
00186             return actConn.isReadOnly();
00187         } catch (SQLException e) {
00188             xac.notifyError(e);
00189             throw(e);
00190         }
00191     }
00192     
00193     public void setCatalog(String catalog) 
00194     throws SQLException {
00195         
00196         logger.log(BasicLevel.DEBUG, "");
00197         try {
00198             actConn.setCatalog(catalog);
00199         } catch (SQLException e) {
00200             xac.notifyError(e);
00201             throw(e);
00202         }
00203     }
00204 
00205     public String getCatalog() 
00206     throws SQLException {
00207         
00208         logger.log(BasicLevel.DEBUG, "");
00209         try {
00210             return actConn.getCatalog();
00211         } catch (SQLException e) {
00212             xac.notifyError(e);
00213             throw(e);
00214         }
00215     }
00216         
00220     public void close() 
00221     throws SQLException {
00222         logger.log(BasicLevel.DEBUG, "");
00223         xac.notifyClose();
00224     }
00225         
00226     public void setTransactionIsolation(int level) 
00227     throws SQLException {
00228         
00229         logger.log(BasicLevel.DEBUG, "");
00230         try {
00231             actConn.setTransactionIsolation(level);
00232         } catch (SQLException e) {
00233             xac.notifyError(e);
00234             throw(e);
00235         }
00236     }
00237         
00238     public int getTransactionIsolation() 
00239     throws SQLException {
00240         
00241         logger.log(BasicLevel.DEBUG, "");
00242         try {
00243             return actConn.getTransactionIsolation();
00244         } catch (SQLException e) {
00245             xac.notifyError(e);
00246             throw(e);
00247         }
00248     }
00249         
00250     public SQLWarning getWarnings() 
00251     throws SQLException {
00252         
00253         logger.log(BasicLevel.DEBUG, "");
00254         try {
00255             return actConn.getWarnings();
00256         } catch (SQLException e) {
00257             xac.notifyError(e);
00258             throw(e);
00259         }
00260     }
00261         
00262     public void clearWarnings() 
00263     throws SQLException {
00264         
00265         logger.log(BasicLevel.DEBUG, "");
00266         try {
00267             actConn.clearWarnings();
00268         } catch (SQLException e) {
00269             xac.notifyError(e);
00270             throw(e);
00271         }
00272     }
00273         
00278     public void commit() 
00279     throws SQLException {
00280         
00281         logger.log(BasicLevel.DEBUG, "local transaction");
00282         try {
00283             actConn.commit();
00284         } catch (SQLException e) {
00285             xac.notifyError(e);
00286             throw(e);
00287         }
00288     }
00289     
00294     public void rollback() 
00295     throws SQLException {
00296         
00297         logger.log(BasicLevel.DEBUG, "local transaction");
00298         try {
00299             actConn.rollback();
00300         } catch (SQLException e) {
00301             xac.notifyError(e);
00302             throw(e);
00303         }
00304     }
00305 
00309     public void setAutoCommit(boolean autoCommit) 
00310     throws SQLException {
00311         
00312         logger.log(BasicLevel.DEBUG, " ");
00313         try {
00314             if (autoCommit == false) {
00315                 if (autocommit_unset == false) { // cache for optimization
00316                     actConn.setAutoCommit(false);
00317                     autocommit_set = false;
00318                     autocommit_unset = true;
00319                 }
00320             } else {
00321                 if (autocommit_set == false) { // cache for optimization
00322                     actConn.setAutoCommit(true);
00323                     autocommit_set = true;
00324                     autocommit_unset = false;
00325                 }
00326             }
00327         } catch (SQLException e) {
00328             String s = e.getMessage().toLowerCase();
00329             if (s.indexOf("set chained command not allowed") != -1) {
00330                 logger.log(BasicLevel.DEBUG, "failed...");
00331                 logger.log(BasicLevel.DEBUG, "Committing then retrying");
00332                 try {
00333                     // These lines for Sybase only, hoping they don't broke
00334                     // anything for others DBs.
00335                     actConn.commit();
00336                     actConn.setAutoCommit(autoCommit); // Shouldn't fail now.
00337                     logger.log(BasicLevel.DEBUG, "succeeded after retry");
00338                 } catch (SQLException se) {
00339                     logger.log(BasicLevel.ERROR,"" + autoCommit + ") failed again after retry");
00340                     logger.log(BasicLevel.ERROR,"Exception: "+se);
00341                     xac.notifyError(e);
00342                     throw(e);
00343                 }
00344             } else {
00345                 logger.log(BasicLevel.ERROR,"setAutoCommit(" + autoCommit + ") failed: "+e);
00346                 xac.notifyError(e);
00347                 throw(e);
00348             }
00349         }
00350     }
00351 
00355     public boolean getAutoCommit() 
00356     throws SQLException {
00357         
00358         try {
00359             return actConn.getAutoCommit();
00360         } catch (SQLException e) {
00361             xac.notifyError(e);
00362             throw(e);
00363         }
00364     }
00365 
00366     public Statement createStatement(int resultSetType, int resultSetConcurrency) 
00367         throws SQLException {
00368         
00369         logger.log(BasicLevel.DEBUG, "");
00370         try {
00371             return actConn.createStatement(resultSetType, resultSetConcurrency);
00372         } catch (SQLException e) {
00373             xac.notifyError(e);
00374             throw(e);
00375         }
00376     }
00377     
00378     public Map getTypeMap() 
00379     throws SQLException {
00380         
00381         logger.log(BasicLevel.DEBUG, "");
00382         try {
00383             return actConn.getTypeMap();
00384         } catch (SQLException e) {
00385             xac.notifyError(e);
00386             throw(e);
00387         }
00388     }
00389 
00390     public void setTypeMap(Map map) 
00391     throws SQLException {
00392         
00393         logger.log(BasicLevel.DEBUG, "");
00394         try {
00395             setTypeMap(map);
00396         } catch (SQLException e) {
00397             xac.notifyError(e);
00398             throw(e);
00399         }
00400     }
00401     
00402     public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) 
00403         throws SQLException {
00404         
00405         logger.log(BasicLevel.DEBUG, "");
00406         try {
00407             return actConn.prepareStatement(sql, resultSetType, resultSetConcurrency);
00408         } catch (SQLException e) {
00409             xac.notifyError(e);
00410             throw(e);
00411         }
00412     }
00413 
00414     public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) 
00415         throws SQLException {
00416         
00417         logger.log(BasicLevel.DEBUG, "");
00418         try {
00419             return actConn.prepareCall(sql, resultSetType, resultSetConcurrency);
00420         } catch (SQLException e) {
00421             xac.notifyError(e);
00422             throw(e);
00423         }
00424     }
00425 
00426     public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
00427     throws SQLException {
00428         
00429         logger.log(BasicLevel.DEBUG, "");
00430         try {
00431             return actConn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
00432         } catch (SQLException e) {
00433             xac.notifyError(e);
00434             throw(e);
00435         }
00436     }
00437     
00438     public int getHoldability() throws SQLException {
00439         logger.log(BasicLevel.DEBUG, "");
00440         try {
00441             return actConn.getHoldability();
00442         } catch (SQLException e) {
00443             xac.notifyError(e);
00444             throw(e);
00445         }
00446     }
00447     
00448     public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) 
00449     throws SQLException {
00450         
00451         logger.log(BasicLevel.DEBUG, "");
00452         try {
00453             return actConn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
00454         } catch (SQLException e) {
00455             xac.notifyError(e);
00456             throw(e);
00457         }
00458     }
00459     
00460     public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) 
00461         throws SQLException {
00462         
00463         logger.log(BasicLevel.DEBUG, "");
00464         try {
00465             return actConn.prepareStatement(sql, autoGeneratedKeys);
00466         } catch (SQLException e) {
00467             xac.notifyError(e);
00468             throw(e);
00469         }
00470     }
00471     
00472     public PreparedStatement prepareStatement(String sql,
00473                                               int resultSetType,
00474                                               int resultSetConcurrency,
00475                                               int resultSetHoldability) 
00476         throws SQLException {
00477         
00478         logger.log(BasicLevel.DEBUG, "");
00479         
00480         try {
00481             return actConn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
00482         } catch (SQLException e) {
00483             xac.notifyError(e);
00484             throw(e);
00485         }
00486     }
00487     
00488     public PreparedStatement prepareStatement(String sql,
00489                                               int[] columnIndexes) 
00490     throws SQLException {
00491         logger.log(BasicLevel.DEBUG, "");
00492         
00493         try {
00494             return actConn.prepareStatement(sql, columnIndexes);
00495         } catch (SQLException e) {
00496             xac.notifyError(e);
00497             throw(e);
00498         }
00499     }
00500     
00501     public PreparedStatement prepareStatement(String sql,
00502                                               String[] columnNames) 
00503     throws SQLException {
00504         logger.log(BasicLevel.DEBUG, "");
00505         
00506         try {
00507             return actConn.prepareStatement(sql, columnNames);
00508         } catch (SQLException e) {
00509             xac.notifyError(e);
00510             throw(e);
00511         }
00512     }
00513     
00514     public void releaseSavepoint(java.sql.Savepoint savepoint) 
00515     throws SQLException {
00516         logger.log(BasicLevel.DEBUG, "");
00517         
00518         try {
00519             actConn.releaseSavepoint(savepoint);
00520         } catch (SQLException e) {
00521             xac.notifyError(e);
00522             throw(e);
00523         }
00524     }
00525     
00526     public void rollback(java.sql.Savepoint savepoint) 
00527     throws SQLException {
00528         logger.log(BasicLevel.DEBUG, "");
00529         
00530         try {
00531             actConn.rollback(savepoint);
00532         } catch (SQLException e) {
00533             xac.notifyError(e);
00534             throw(e);
00535         }
00536     }
00537     
00538     public void setHoldability(int holdability) 
00539     throws SQLException {
00540         logger.log(BasicLevel.DEBUG, "");
00541         
00542         try {
00543             actConn.setHoldability(holdability);
00544         } catch (SQLException e) {
00545             xac.notifyError(e);
00546             throw(e);
00547         }
00548     }
00549     
00550     public java.sql.Savepoint setSavepoint() 
00551     throws SQLException {
00552         logger.log(BasicLevel.DEBUG, "");
00553         
00554         try {
00555             return actConn.setSavepoint();
00556         } catch (SQLException e) {
00557             xac.notifyError(e);
00558             throw(e);
00559         }
00560     }
00561     
00562     public java.sql.Savepoint setSavepoint(String name) 
00563     throws SQLException {
00564         logger.log(BasicLevel.DEBUG, "");
00565         
00566         try {
00567             return actConn.setSavepoint(name);
00568         } catch (SQLException e) {
00569             xac.notifyError(e);
00570             throw(e);
00571         }
00572     }
00573 
00574 }
00575 

Generated on Tue Feb 15 15:05:16 2005 for JOnAS by  doxygen 1.3.9.1