Coverage Report - org.apache.commons.dbcp.cpdsadapter.ConnectionImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ConnectionImpl
52%
24/46
50%
3/6
2.545
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.commons.dbcp.cpdsadapter;
 19  
 
 20  
 import java.sql.Connection;
 21  
 import java.sql.PreparedStatement;
 22  
 import java.sql.SQLException;
 23  
 
 24  
 import org.apache.commons.dbcp.DelegatingConnection;
 25  
 import org.apache.commons.dbcp.DelegatingPreparedStatement;
 26  
 
 27  
 /**
 28  
  * This class is the <code>Connection</code> that will be returned
 29  
  * from <code>PooledConnectionImpl.getConnection()</code>.  
 30  
  * Most methods are wrappers around the jdbc 1.x <code>Connection</code>.  
 31  
  * A few exceptions include preparedStatement and close.
 32  
  * In accordance with the jdbc specification this Connection cannot
 33  
  * be used after closed() is called.  Any further usage will result in an
 34  
  * SQLException.
 35  
  * 
 36  
  * ConnectionImpl extends DelegatingConnection to enable access to the
 37  
  * underlying connection.
 38  
  *
 39  
  * @author John D. McNally
 40  
  * @version $Revision: 1023401 $ $Date: 2010-10-16 21:54:24 -0400 (Sat, 16 Oct 2010) $
 41  
  */
 42  
 class ConnectionImpl extends DelegatingConnection {
 43  
 
 44  
     private final boolean accessToUnderlyingConnectionAllowed;
 45  
 
 46  
     /** The object that instantiated this object */
 47  
      private final PooledConnectionImpl pooledConnection;
 48  
 
 49  
     /**
 50  
      * Creates a <code>ConnectionImpl</code>. 
 51  
      *
 52  
      * @param pooledConnection The PooledConnection that is calling the ctor.
 53  
      * @param connection The JDBC 1.x Connection to wrap.
 54  
      * @param accessToUnderlyingConnectionAllowed if true, then access is allowed to the underlying connectiion
 55  
      */
 56  
     ConnectionImpl(PooledConnectionImpl pooledConnection, 
 57  
             Connection connection,
 58  
             boolean accessToUnderlyingConnectionAllowed) {
 59  7725
         super(connection);
 60  7725
         this.pooledConnection = pooledConnection;
 61  7725
         this.accessToUnderlyingConnectionAllowed =
 62  
             accessToUnderlyingConnectionAllowed;
 63  7725
     }
 64  
 
 65  
     /**
 66  
      * Marks the Connection as closed, and notifies the pool that the
 67  
      * pooled connection is available.
 68  
      * In accordance with the jdbc specification this Connection cannot
 69  
      * be used after closed() is called.  Any further usage will result in an
 70  
      * SQLException.
 71  
      *
 72  
      * @exception SQLException The database connection couldn't be closed.
 73  
      */
 74  
     public void close() throws SQLException {
 75  10481
         if (!_closed) {
 76  7725
             _closed = true;
 77  7725
             passivate();
 78  7725
             pooledConnection.notifyListeners();
 79  
         }
 80  10481
     }
 81  
 
 82  
     /**
 83  
      * If pooling of <code>PreparedStatement</code>s is turned on in the
 84  
      * {@link DriverAdapterCPDS}, a pooled object may be returned, otherwise
 85  
      * delegate to the wrapped jdbc 1.x {@link java.sql.Connection}.
 86  
      *
 87  
      * @param sql SQL statement to be prepared
 88  
      * @return the prepared statement
 89  
      * @exception SQLException if this connection is closed or an error occurs
 90  
      * in the wrapped connection.
 91  
      */
 92  
     public PreparedStatement prepareStatement(String sql) throws SQLException {
 93  7067
         checkOpen();
 94  
         try {
 95  7067
             return new DelegatingPreparedStatement
 96  
                 (this, pooledConnection.prepareStatement(sql));
 97  
         }
 98  0
         catch (SQLException e) {
 99  0
             handleException(e); // Does not return
 100  0
             return null;
 101  
         }
 102  
     }
 103  
 
 104  
     /**
 105  
      * If pooling of <code>PreparedStatement</code>s is turned on in the
 106  
      * {@link DriverAdapterCPDS}, a pooled object may be returned, otherwise
 107  
      * delegate to the wrapped jdbc 1.x {@link java.sql.Connection}.
 108  
      *
 109  
      * @exception SQLException if this connection is closed or an error occurs
 110  
      * in the wrapped connection.
 111  
      */
 112  
     public PreparedStatement prepareStatement(String sql, int resultSetType, 
 113  
                                               int resultSetConcurrency) 
 114  
             throws SQLException {
 115  12
         checkOpen();
 116  
         try {
 117  12
             return new DelegatingPreparedStatement
 118  
                 (this, pooledConnection.prepareStatement
 119  
                     (sql,resultSetType,resultSetConcurrency));
 120  
         }
 121  0
         catch (SQLException e) {
 122  0
             handleException(e);
 123  0
             return null;
 124  
         }
 125  
     }
 126  
 
 127  
     public PreparedStatement prepareStatement(String sql, int resultSetType,
 128  
                                               int resultSetConcurrency,
 129  
                                               int resultSetHoldability)
 130  
             throws SQLException {
 131  8
         checkOpen();
 132  
         try {
 133  8
             return new DelegatingPreparedStatement(this,
 134  
                     pooledConnection.prepareStatement(sql, resultSetType,
 135  
                             resultSetConcurrency, resultSetHoldability));
 136  
         }
 137  0
         catch (SQLException e) {
 138  0
             handleException(e);
 139  0
             return null;
 140  
         }
 141  
     }
 142  
 
 143  
     public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
 144  
             throws SQLException {
 145  8
         checkOpen();
 146  
         try {
 147  8
             return new DelegatingPreparedStatement(this,
 148  
                     pooledConnection.prepareStatement(sql, autoGeneratedKeys));
 149  
         }
 150  0
         catch (SQLException e) {
 151  0
             handleException(e);
 152  0
             return null;
 153  
         }
 154  
     }
 155  
 
 156  
     public PreparedStatement prepareStatement(String sql, int columnIndexes[])
 157  
             throws SQLException {
 158  8
         checkOpen();
 159  
         try {
 160  8
             return new DelegatingPreparedStatement(this,
 161  
                     pooledConnection.prepareStatement(sql, columnIndexes));
 162  
         }
 163  0
         catch (SQLException e) {
 164  0
             handleException(e);
 165  0
             return null;
 166  
         }
 167  
     }
 168  
 
 169  
     public PreparedStatement prepareStatement(String sql, String columnNames[])
 170  
             throws SQLException {
 171  8
         checkOpen();
 172  
         try {
 173  8
             return new DelegatingPreparedStatement(this,
 174  
                     pooledConnection.prepareStatement(sql, columnNames));
 175  
         }
 176  0
         catch (SQLException e) {
 177  0
             handleException(e);
 178  0
             return null;
 179  
         }
 180  
     }
 181  
 
 182  
     //
 183  
     // Methods for accessing the delegate connection
 184  
     //
 185  
 
 186  
     /**
 187  
      * If false, getDelegate() and getInnermostDelegate() will return null.
 188  
      * @return true if access is allowed to the underlying connection
 189  
      * @see ConnectionImpl
 190  
      */
 191  
     public boolean isAccessToUnderlyingConnectionAllowed() {
 192  4649
         return accessToUnderlyingConnectionAllowed;
 193  
     }
 194  
 
 195  
     /**
 196  
      * Get the delegated connection, if allowed.
 197  
      * @return the internal connection, or null if access is not allowed.
 198  
      * @see #isAccessToUnderlyingConnectionAllowed()
 199  
      */
 200  
     public Connection getDelegate() {
 201  0
         if (isAccessToUnderlyingConnectionAllowed()) {
 202  0
             return getDelegateInternal();
 203  
         } else {
 204  0
             return null;
 205  
         }
 206  
     }
 207  
 
 208  
     /**
 209  
      * Get the innermost connection, if allowed.
 210  
      * @return the innermost internal connection, or null if access is not allowed.
 211  
      * @see #isAccessToUnderlyingConnectionAllowed()
 212  
      */
 213  
     public Connection getInnermostDelegate() {
 214  4649
         if (isAccessToUnderlyingConnectionAllowed()) {
 215  4649
             return super.getInnermostDelegateInternal();
 216  
         } else {
 217  0
             return null;
 218  
         }
 219  
     }
 220  
 
 221  
 }