GERONIMO-4786 merge changes from 2.1 branch r780437, r801468, r801849, r803183

git-svn-id: https://svn.apache.org/repos/asf/geronimo/components/txmanager/trunk@803194 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/AbstractSinglePoolConnectionInterceptor.java b/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/AbstractSinglePoolConnectionInterceptor.java
index da85f98..853fa4d 100644
--- a/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/AbstractSinglePoolConnectionInterceptor.java
+++ b/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/AbstractSinglePoolConnectionInterceptor.java
@@ -103,7 +103,7 @@
     public void returnConnection(ConnectionInfo connectionInfo,
                                  ConnectionReturnAction connectionReturnAction) {
         if (log.isTraceEnabled()) {
-            log.trace("returning connection " + connectionInfo.getConnectionHandle() + " for MCI " + connectionInfo.getManagedConnectionInfo() + " and MC " + connectionInfo.getManagedConnectionInfo().getManagedConnection() + " to pool " + this);
+            log.trace("returning connection " + connectionInfo.getConnectionHandle() + " for MCI " + connectionInfo.getManagedConnectionInfo() + " to pool " + this);
         }
 
         // not strictly synchronized with destroy(), but pooled operations in internalReturn() are...
@@ -120,9 +120,7 @@
         try {
             ManagedConnectionInfo mci = connectionInfo.getManagedConnectionInfo();
             if (connectionReturnAction == ConnectionReturnAction.RETURN_HANDLE && mci.hasConnectionHandles()) {
-                if (log.isTraceEnabled()) {
-                    log.trace("Return request at pool with connection handles! " + connectionInfo.getConnectionHandle() + " for MCI " + connectionInfo.getManagedConnectionInfo() + " and MC " + connectionInfo.getManagedConnectionInfo().getManagedConnection() + " to pool " + this, new Exception("Stack trace"));
-                }
+                log.warn("Return request at pool with connection handles! " + connectionInfo.getConnectionHandle() + " for MCI " + connectionInfo.getManagedConnectionInfo() + " and MC " + connectionInfo.getManagedConnectionInfo().getManagedConnection() + " to pool " + this, new Exception("Stack trace"));
                 return;
             }
 
@@ -136,6 +134,14 @@
         }
     }
 
+
+    /**
+     *
+     * @param connectionInfo connection info to return to pool
+     * @param connectionReturnAction whether to return to pool or destroy
+     * @return true if a connection for which a permit was issued was returned (so the permit should be released),
+     * false if no permit was issued (for instance if the connection was already in the pool and we are destroying it).
+     */
     protected boolean internalReturn(ConnectionInfo connectionInfo, ConnectionReturnAction connectionReturnAction) {
         ManagedConnectionInfo mci = connectionInfo.getManagedConnectionInfo();
         ManagedConnection mc = mci.getManagedConnection();
diff --git a/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/ConnectionReturnAction.java b/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/ConnectionReturnAction.java
index 1e713fa..8e6f0d9 100644
--- a/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/ConnectionReturnAction.java
+++ b/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/ConnectionReturnAction.java
@@ -27,12 +27,18 @@
  * @version 1.0
  */
 public class ConnectionReturnAction {
+    private final String name;
     public final static ConnectionReturnAction RETURN_HANDLE =
-            new ConnectionReturnAction();
+            new ConnectionReturnAction("RETURN_HANDLE");
     public final static ConnectionReturnAction DESTROY =
-            new ConnectionReturnAction();
+            new ConnectionReturnAction("DESTROY");
 
-    private ConnectionReturnAction() {
+    private ConnectionReturnAction(String name) {
+        this.name = name;
     }
 
+    @Override
+    public String toString() {
+        return "ConnectionReturnAction{" + name + '}';
+    }
 }
diff --git a/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/ManagedConnectionInfo.java b/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/ManagedConnectionInfo.java
index 81d5f67..5fdf5fe 100644
--- a/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/ManagedConnectionInfo.java
+++ b/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/ManagedConnectionInfo.java
@@ -151,4 +151,9 @@
         return listener.isFirstConnectionInfo(connectionInfo);
     }
 
+    @Override
+    public String toString() {
+        return "ManagedConnectionInfo: " + super.toString() + ". mc: " + managedConnection + "]";
+    }
+    
 }
diff --git a/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/SinglePoolConnectionInterceptor.java b/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/SinglePoolConnectionInterceptor.java
index 4604b1b..9a9257e 100644
--- a/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/SinglePoolConnectionInterceptor.java
+++ b/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/SinglePoolConnectionInterceptor.java
@@ -45,7 +45,6 @@
     private boolean selectOneAssumeMatch;
 
     //pool is mutable but only changed when protected by write lock on resizelock in superclass
-//    private PoolDeque pool;
     private final List<ManagedConnectionInfo> pool;
 
     public SinglePoolConnectionInterceptor(final ConnectionInterceptor next,
@@ -55,7 +54,6 @@
                                            int idleTimeoutMinutes,
                                            boolean selectOneAssumeMatch) {
         super(next, maxSize, minSize, blockingTimeoutMilliseconds, idleTimeoutMinutes);
-//        pool = new PoolDeque(maxSize);
         pool = new ArrayList<ManagedConnectionInfo>(maxSize);
         this.selectOneAssumeMatch = selectOneAssumeMatch;
     }
@@ -71,7 +69,7 @@
                 next.getConnection(connectionInfo);
                 connectionCount++;
                 if (log.isTraceEnabled()) {
-                    log.trace("Supplying new connection MCI: " + connectionInfo.getManagedConnectionInfo() + " MC: " + connectionInfo.getManagedConnectionInfo().getManagedConnection() + " from pool: " + this);
+                    log.trace("Supplying new connection MCI: " + connectionInfo.getManagedConnectionInfo() + " from pool: " + this);
                 }
                 return;
             } else {
@@ -83,27 +81,16 @@
             if (selectOneAssumeMatch) {
                 connectionInfo.setManagedConnectionInfo(newMCI);
                 if (log.isTraceEnabled()) {
-                    log.trace("Supplying pooled connection without checking matching MCI: " + connectionInfo.getManagedConnectionInfo() + " MC: " + connectionInfo.getManagedConnectionInfo().getManagedConnection() + " from pool: " + this);
+                    log.trace("Supplying pooled connection without checking matching MCI: " + connectionInfo.getManagedConnectionInfo() + " from pool: " + this);
                 }
                 return;
             }
+            ManagedConnection matchedMC;
             try {
                 ManagedConnectionInfo mci = connectionInfo.getManagedConnectionInfo();
-                ManagedConnection matchedMC = newMCI.getManagedConnectionFactory().matchManagedConnections(Collections.singleton(newMCI.getManagedConnection()),
+                matchedMC = newMCI.getManagedConnectionFactory().matchManagedConnections(Collections.singleton(newMCI.getManagedConnection()),
                         mci.getSubject(),
                         mci.getConnectionRequestInfo());
-                if (matchedMC != null) {
-                    connectionInfo.setManagedConnectionInfo(newMCI);
-                    if (log.isTraceEnabled()) {
-                        log.trace("Supplying pooled connection  MCI: " + connectionInfo.getManagedConnectionInfo() + " MC: " + connectionInfo.getManagedConnectionInfo().getManagedConnection() + " from pool: " + this);
-                    }
-                } else {
-                    //matching failed.
-                    ConnectionInfo returnCI = new ConnectionInfo();
-                    returnCI.setManagedConnectionInfo(newMCI);
-                    returnConnection(returnCI, ConnectionReturnAction.RETURN_HANDLE);
-                    throw new ResourceException("The pooling strategy does not match the MatchManagedConnections implementation.  Please investigate and reconfigure this pool");
-                }
             } catch (ResourceException e) {
                 //something is wrong: destroy connection, rethrow, release permit
                 ConnectionInfo returnCI = new ConnectionInfo();
@@ -111,6 +98,18 @@
                 returnConnection(returnCI, ConnectionReturnAction.DESTROY);
                 throw e;
             }
+            if (matchedMC != null) {
+                connectionInfo.setManagedConnectionInfo(newMCI);
+                if (log.isTraceEnabled()) {
+                    log.trace("Supplying pooled connection  MCI: " + connectionInfo.getManagedConnectionInfo() + " from pool: " + this);
+                }
+            } else {
+                //matching failed.
+                ConnectionInfo returnCI = new ConnectionInfo();
+                returnCI.setManagedConnectionInfo(newMCI);
+                returnConnection(returnCI, ConnectionReturnAction.RETURN_HANDLE);
+                throw new ResourceException("The pooling strategy does not match the MatchManagedConnections implementation.  Please investigate and reconfigure this pool");
+            }
         }
     }
 
@@ -138,7 +137,12 @@
         pool.add(mci);
     }
 
+    /**
+     * @param mci managedConnectionInfo to remove from pool
+     * @return true if mci was not in pool already, false if mci was in pool already.
+     */
     protected boolean doRemove(ManagedConnectionInfo mci) {
+        log.info("Removing " + mci + " from pool " + this);
         return !pool.remove(mci);
     }
 
diff --git a/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/ThreadLocalCachingConnectionInterceptor.java b/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/ThreadLocalCachingConnectionInterceptor.java
index 4094959..295745b 100644
--- a/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/ThreadLocalCachingConnectionInterceptor.java
+++ b/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/ThreadLocalCachingConnectionInterceptor.java
@@ -72,7 +72,12 @@
     }
 
     public void returnConnection(ConnectionInfo connectionInfo, ConnectionReturnAction connectionReturnAction) {
-        if (connectionReturnAction == ConnectionReturnAction.DESTROY || connectionInfo.isUnshareable()) {
+        if (connectionReturnAction == ConnectionReturnAction.DESTROY
+                || connectionInfo.isUnshareable()
+                || !connectionInfo.getManagedConnectionInfo().hasConnectionHandles()) {
+            if (connections.get() == connectionInfo.getManagedConnectionInfo()) {
+                connections.remove();
+            }
             next.returnConnection(connectionInfo, connectionReturnAction);
         }
     }
diff --git a/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/TransactionCachingInterceptor.java b/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/TransactionCachingInterceptor.java
index 393eb0f..62dbf13 100644
--- a/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/TransactionCachingInterceptor.java
+++ b/geronimo-connector/src/main/java/org/apache/geronimo/connector/outbound/TransactionCachingInterceptor.java
@@ -77,18 +77,25 @@
             } else {
                 ManagedConnectionInfo managedConnectionInfo = managedConnectionInfos.getShared();
                 if (managedConnectionInfo != null) {
-                    connectionInfo.setManagedConnectionInfo(managedConnectionInfo);
-                    //return;
-                    if (log.isTraceEnabled()) {
-                        log.trace(
-                                "supplying connection from tx cache " + connectionInfo.getConnectionHandle() + " for managed connection " + connectionInfo.getManagedConnectionInfo().getManagedConnection() + " to tx caching interceptor " + this);
+                    ManagedConnectionInfo previousMci = connectionInfo.getManagedConnectionInfo();
+                    if (previousMci != null && previousMci != managedConnectionInfo && previousMci.getManagedConnection() != null) {
+                        //This might occur if more than one connection were obtained before a UserTransaction were started.
+                        //enlists connection
+                        next.getConnection(connectionInfo);
+                        managedConnectionInfos.addUnshared(previousMci);
+                    } else {
+                        connectionInfo.setManagedConnectionInfo(managedConnectionInfo);
+
+                        //return;
+                        if (log.isTraceEnabled()) {
+                            log.trace("supplying connection from tx cache " + connectionInfo.getConnectionHandle() + " for managed connection " + connectionInfo.getManagedConnectionInfo().getManagedConnection() + " to tx caching interceptor " + this);
+                        }
                     }
                 } else {
                     next.getConnection(connectionInfo);
                     managedConnectionInfos.setShared(connectionInfo.getManagedConnectionInfo());
                     if (log.isTraceEnabled()) {
-                        log.trace(
-                                "supplying connection from pool " + connectionInfo.getConnectionHandle() + " for managed connection " + connectionInfo.getManagedConnectionInfo().getManagedConnection() + " to tx caching interceptor " + this);
+                        log.trace("supplying connection from pool " + connectionInfo.getConnectionHandle() + " for managed connection " + connectionInfo.getManagedConnectionInfo().getManagedConnection() + " to tx caching interceptor " + this);
                     }
                 }
             }
@@ -112,8 +119,7 @@
             if (transaction != null) {
                 if (TxUtil.isActive(transaction)) {
                     if (log.isTraceEnabled()) {
-                        log.trace(
-                                "tx active, not returning connection" + connectionInfo.getConnectionHandle() + " for managed connection " + connectionInfo.getManagedConnectionInfo().getManagedConnection() + " to tx caching interceptor " + this);
+                        log.trace("tx active, not returning connection" + connectionInfo.getConnectionHandle() + " for managed connection " + connectionInfo.getManagedConnectionInfo().getManagedConnection() + " to tx caching interceptor " + this);
                     }
                     return;
                 }
@@ -137,16 +143,14 @@
     private void internalReturn(ConnectionInfo connectionInfo, ConnectionReturnAction connectionReturnAction) {
         if (connectionInfo.getManagedConnectionInfo().hasConnectionHandles()) {
             if (log.isTraceEnabled()) {
-                log.trace(
-                        "not returning connection from tx cache (has handles) " + connectionInfo.getConnectionHandle() + " for managed connection " + connectionInfo.getManagedConnectionInfo().getManagedConnection() + " to tx caching interceptor " + this);
+                log.trace("not returning connection from tx cache (has handles) " + connectionInfo.getConnectionHandle() + " for managed connection " + connectionInfo.getManagedConnectionInfo().getManagedConnection() + " to tx caching interceptor " + this);
             }
             return;
         }
         //No transaction, no handles, we return it.
         next.returnConnection(connectionInfo, connectionReturnAction);
         if (log.isTraceEnabled()) {
-            log.trace(
-                    "completed return of connection through tx cache " + connectionInfo.getConnectionHandle() + " for MCI: " + connectionInfo.getManagedConnectionInfo() + " and MC " + connectionInfo.getManagedConnectionInfo().getManagedConnection() + " to tx caching interceptor " + this);
+            log.trace("completed return of connection through tx cache " + connectionInfo.getConnectionHandle() + " for MCI: " + connectionInfo.getManagedConnectionInfo() + " and MC " + connectionInfo.getManagedConnectionInfo().getManagedConnection() + " to tx caching interceptor " + this);
         }
     }