Coverage Report - org.apache.commons.dbcp.managed.ManagedConnection
 
Classes in this File Line Coverage Branch Coverage Complexity
ManagedConnection
79%
69/87
85%
34/40
3.769
ManagedConnection$CompletionListener
100%
4/4
50%
1/2
3.769
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  *  Unless required by applicable law or agreed to in writing, software
 13  
  *  distributed under the License is distributed on an "AS IS" BASIS,
 14  
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  *  See the License for the specific language governing permissions and
 16  
  *  limitations under the License.
 17  
  */
 18  
 package org.apache.commons.dbcp.managed;
 19  
 
 20  
 import org.apache.commons.dbcp.DelegatingConnection;
 21  
 import org.apache.commons.pool.ObjectPool;
 22  
 
 23  
 import java.sql.Connection;
 24  
 import java.sql.SQLException;
 25  
 
 26  
 /**
 27  
  * ManagedConnection is responsible for managing a database connection in a transactional environment
 28  
  * (typically called "Container Managed").  A managed connection operates like any other connection
 29  
  * when no global transaction (a.k.a. XA transaction or JTA Transaction) is in progress.  When a
 30  
  * global transaction is active a single physical connection to the database is used by all
 31  
  * ManagedConnections accessed in the scope of the transaction.  Connection sharing means that all
 32  
  * data access during a transaction has a consistent view of the database.  When the global transaction
 33  
  * is committed or rolled back the enlisted connections are committed or rolled back.  Typically upon
 34  
  * transaction completion, a connection returns to the auto commit setting in effect before being
 35  
  * enlisted in the transaction, but some vendors do not properly implement this.
 36  
  *
 37  
  * When enlisted in a transaction the setAutoCommit(), commit(), rollback(), and setReadOnly() methods
 38  
  * throw a SQLException.  This is necessary to assure that the transaction completes as a single unit.
 39  
  *
 40  
  * @author Dain Sundstrom
 41  
  * @version $Revision$
 42  
  */
 43  496
 public class ManagedConnection extends DelegatingConnection {
 44  
     private final ObjectPool pool;
 45  
     private final TransactionRegistry transactionRegistry;
 46  
     private final boolean accessToUnderlyingConnectionAllowed;
 47  
     private TransactionContext transactionContext;
 48  
     private boolean isSharedConnection;
 49  
 
 50  
     public ManagedConnection(ObjectPool pool, TransactionRegistry transactionRegistry, boolean accessToUnderlyingConnectionAllowed) throws SQLException {
 51  4548
         super(null);
 52  4548
         this.pool = pool;
 53  4548
         this.transactionRegistry = transactionRegistry;
 54  4548
         this.accessToUnderlyingConnectionAllowed = accessToUnderlyingConnectionAllowed;
 55  4548
         updateTransactionStatus();
 56  4540
     }
 57  
 
 58  
     protected void checkOpen() throws SQLException {
 59  4062
         super.checkOpen();
 60  4056
         updateTransactionStatus();
 61  4056
     }
 62  
 
 63  
     private void updateTransactionStatus() throws SQLException {
 64  
         // if there is a is an active transaction context, assure the transaction context hasn't changed
 65  8604
         if (transactionContext != null) {
 66  294
             if (transactionContext.isActive()) {
 67  294
                 if (transactionContext != transactionRegistry.getActiveTransactionContext()) {
 68  0
                     throw new SQLException("Connection can not be used while enlisted in another transaction");
 69  
                 }
 70  294
                 return;
 71  
             } else {
 72  
                 // transaction should have been cleared up by TransactionContextListener, but in
 73  
                 // rare cases another lister could have registered which uses the connection before
 74  
                 // our listener is called.  In that rare case, trigger the transaction complete call now
 75  0
                 transactionComplete();
 76  
             }
 77  
         }
 78  
 
 79  
         // the existing transaction context ended (or we didn't have one), get the active transaction context
 80  8310
         transactionContext = transactionRegistry.getActiveTransactionContext();
 81  
 
 82  
         // if there is an active transaction context and it already has a shared connection, use it
 83  8310
         if (transactionContext != null && transactionContext.getSharedConnection() != null) {
 84  
             // A connection for the connection factory has already been enrolled
 85  
             // in the transaction, replace our delegate with the enrolled connection
 86  
 
 87  
             // return current connection to the pool
 88  410
             Connection connection = getDelegateInternal();
 89  410
             setDelegate(null);
 90  410
             if (connection != null) {
 91  
                 try {
 92  2
                     pool.returnObject(connection);
 93  0
                 } catch (Exception ignored) {
 94  
                     // whatever... try to invalidate the connection
 95  
                     try {
 96  0
                         pool.invalidateObject(connection);
 97  0
                     } catch (Exception ignore) {
 98  
                         // no big deal
 99  0
                     }
 100  2
                 }
 101  
             }
 102  
 
 103  
             // add a listener to the transaction context
 104  410
             transactionContext.addTransactionContextListener(new CompletionListener());
 105  
 
 106  
             // set our delegate to the shared connection
 107  410
             setDelegate(transactionContext.getSharedConnection());
 108  
 
 109  
             // remember that we are using a shared connection so it can be cleared after the
 110  
             // transaction completes
 111  410
             isSharedConnection = true;
 112  410
         } else {
 113  
             // if our delegate is null, create one
 114  7900
             if (getDelegateInternal() == null) {
 115  
                 try {
 116  
                     // borrow a new connection from the pool
 117  4142
                     Connection connection = (Connection) pool.borrowObject();
 118  4134
                     setDelegate(connection);
 119  8
                 } catch (Exception e) {
 120  8
                     throw (SQLException) new SQLException("Unable to acquire a new connection from the pool").initCause(e);
 121  4134
                 }
 122  
             }
 123  
 
 124  
             // if we have a transaction, out delegate becomes the shared delegate
 125  7892
             if (transactionContext != null) {
 126  
                 // add a listener to the transaction context
 127  86
                 transactionContext.addTransactionContextListener(new CompletionListener());
 128  
 
 129  
                 // register our connection as the shared connection
 130  
                 try {
 131  86
                     transactionContext.setSharedConnection(getDelegateInternal());
 132  0
                 } catch (SQLException e) {
 133  
                     // transaction is hosed
 134  0
                     transactionContext = null;
 135  0
                     throw e;
 136  86
                 }
 137  
             }
 138  
         }
 139  8302
     }
 140  
 
 141  
     public void close() throws SQLException {
 142  9014
         if (!_closed) {
 143  
             try {
 144  
                 // don't actually close the connection if in a transaction
 145  
                 // the connection will be closed by the transactionComplete method
 146  4530
                 if (transactionContext == null) {
 147  4048
                     getDelegateInternal().close();
 148  
                 }
 149  
             } finally {
 150  4530
                 _closed = true;
 151  4522
             }
 152  
         }
 153  9006
     }
 154  
 
 155  
     /**
 156  
      * Delegates to {@link ManagedConnection#transactionComplete()} 
 157  
      * for transaction completion events. 
 158  
      */
 159  496
     protected class CompletionListener implements TransactionContextListener {
 160  
         public void afterCompletion(TransactionContext completedContext, boolean commited) {
 161  496
             if (completedContext == transactionContext) {
 162  496
                 transactionComplete();
 163  
             }
 164  496
         }
 165  
     }
 166  
 
 167  
     protected void transactionComplete() {
 168  496
         transactionContext = null;
 169  
 
 170  
         // if we were using a shared connection, clear the reference now that the transaction has completed
 171  496
         if (isSharedConnection) {
 172  
             // for now, just set the delegate to null, it will be created later if needed
 173  410
             setDelegate(null);
 174  410
             isSharedConnection = false;
 175  
         }
 176  
 
 177  
         // if this connection was closed during the transaction and there is still a delegate present close it
 178  496
         Connection delegate = getDelegateInternal();
 179  496
         if (_closed && delegate != null) {
 180  
             try {
 181  80
                 setDelegate(null);
 182  
 
 183  
                 // don't actually close the connection if in a transaction
 184  80
                 if (!delegate.isClosed()) {
 185  
                     // don't use super.close() because it calls passivate() which marks the
 186  
                     // the connection as closed without returning it to the pool
 187  76
                     delegate.close();
 188  
                 }
 189  0
             } catch (SQLException ignored) {
 190  
                 // not a whole lot we can do here as connection is closed
 191  
                 // and this is a transaction callback so there is no
 192  
                 // way to report the error
 193  
             } finally {
 194  80
                 _closed = true;
 195  80
             }
 196  
         }
 197  
 
 198  496
     }
 199  
 
 200  
     //
 201  
     // The following methods can't be used while enlisted in a transaction
 202  
     //
 203  
 
 204  
     public void setAutoCommit(boolean autoCommit) throws SQLException {
 205  6
         if (transactionContext != null) {
 206  2
             throw new SQLException("Auto-commit can not be set while enrolled in a transaction");
 207  
         }
 208  4
         super.setAutoCommit(autoCommit);
 209  4
     }
 210  
 
 211  
 
 212  
     public void commit() throws SQLException {
 213  2
         if (transactionContext != null) {
 214  2
             throw new SQLException("Commit can not be set while enrolled in a transaction");
 215  
         }
 216  0
         super.commit();
 217  0
     }
 218  
 
 219  
     public void rollback() throws SQLException {
 220  0
         if (transactionContext != null) {
 221  0
             throw new SQLException("Commit can not be set while enrolled in a transaction");
 222  
         }
 223  0
         super.rollback();
 224  0
     }
 225  
 
 226  
 
 227  
     public void setReadOnly(boolean readOnly) throws SQLException {
 228  4
         if (transactionContext != null) {
 229  4
             throw new SQLException("Read-only can not be set while enrolled in a transaction");
 230  
         }
 231  0
         super.setReadOnly(readOnly);
 232  0
     }
 233  
 
 234  
     //
 235  
     // Methods for accessing the delegate connection
 236  
     //
 237  
 
 238  
     /**
 239  
      * If false, getDelegate() and getInnermostDelegate() will return null.
 240  
      * @return if false, getDelegate() and getInnermostDelegate() will return null
 241  
      */
 242  
     public boolean isAccessToUnderlyingConnectionAllowed() {
 243  196
         return accessToUnderlyingConnectionAllowed;
 244  
     }
 245  
 
 246  
     public Connection getDelegate() {
 247  20
         if (isAccessToUnderlyingConnectionAllowed()) {
 248  14
             return getDelegateInternal();
 249  
         } else {
 250  6
             return null;
 251  
         }
 252  
     }
 253  
 
 254  
     public Connection getInnermostDelegate() {
 255  168
         if (isAccessToUnderlyingConnectionAllowed()) {
 256  162
             return super.getInnermostDelegateInternal();
 257  
         } else {
 258  6
             return null;
 259  
         }
 260  
     }
 261  
 }