[jboss-cvs] JBossAS SVN: r111804 - in branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector: src/main/org/jboss/resource/adapter/jdbc and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Jul 20 12:20:44 EDT 2011


Author: jbertram at redhat.com
Date: 2011-07-20 12:20:43 -0400 (Wed, 20 Jul 2011)
New Revision: 111804

Modified:
   branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/build.xml
   branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/adapter/jdbc/BaseWrapperManagedConnection.java
   branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/adapter/jdbc/WrappedConnection.java
   branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/adapter/jms/JmsManagedConnection.java
   branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/adapter/jms/JmsSession.java
   branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/connectionmanager/JBossManagedConnectionPool.java
Log:
JBPAPP-6867

Modified: branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/build.xml
===================================================================
--- branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/build.xml	2011-07-20 16:11:35 UTC (rev 111803)
+++ branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/build.xml	2011-07-20 16:20:43 UTC (rev 111804)
@@ -354,9 +354,10 @@
         <include name="org/jboss/resource/adapter/jdbc/CachedPreparedStatement.class"/>
         <include name="org/jboss/resource/adapter/jdbc/PreparedStatementCache*.class"/>
         <include name="org/jboss/resource/adapter/jdbc/StatementAccess.class"/>
+        <include name="org/jboss/resource/adapter/jdbc/ReentrantLock.class"/>
         <include name="org/jboss/resource/adapter/jdbc/jdk5/*.class"/>
         <include name="org/jboss/resource/adapter/jdbc/jdk6/*.class"/>
-		<include name="org/jboss/resource/adapter/jdbc/URLSelectorStrategy.class"/>
+        <include name="org/jboss/resource/adapter/jdbc/URLSelectorStrategy.class"/>
         <include name="org/jboss/resource/adapter/jdbc/remote/*"/>
         <!-- JBAS-2250 hack -->
         <include name="org/jboss/ejb/plugins/cmp/jdbc/*" />

Modified: branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/adapter/jdbc/BaseWrapperManagedConnection.java
===================================================================
--- branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/adapter/jdbc/BaseWrapperManagedConnection.java	2011-07-20 16:11:35 UTC (rev 111803)
+++ branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/adapter/jdbc/BaseWrapperManagedConnection.java	2011-07-20 16:20:43 UTC (rev 111804)
@@ -36,7 +36,6 @@
 import java.util.Set;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.concurrent.locks.ReentrantLock;
 
 import javax.resource.ResourceException;
 import javax.resource.spi.ConnectionEvent;
@@ -214,6 +213,32 @@
 
    public void cleanup() throws ResourceException
    {
+      boolean isActive = false;
+
+      if (lock.hasQueuedThreads())
+      {
+         Collection<Thread> threads = lock.getQueuedThreads();
+         for (Thread thread : threads)
+         {
+            Throwable t = new Throwable("Thread waiting for lock during cleanup");
+            t.setStackTrace(thread.getStackTrace());
+
+            mcf.log.warn(t.getMessage(), t);
+         }
+
+         isActive = true;
+      }
+
+      if (lock.isLocked())
+      {
+         Throwable t = new Throwable("Lock owned during cleanup");
+         t.setStackTrace(lock.getOwner().getStackTrace());
+
+         mcf.log.warn(t.getMessage(), t);
+         
+         isActive = true;
+      }
+
       synchronized (handles)
       {
          for (Iterator i = handles.iterator(); i.hasNext();)
@@ -221,8 +246,10 @@
             WrappedConnection lc = (WrappedConnection) i.next();
             lc.setManagedConnection(null);
          }
+
          handles.clear();
       }
+
       //reset all the properties we know about to defaults.
       synchronized (stateLock)
       {
@@ -241,11 +268,14 @@
             }
          }
       }
-      // I'm recreating the lock object when we return to the pool
-      // because it looks too nasty to expect the connection handle
-      // to unlock properly in certain race conditions
-      // where the dissociation of the managed connection is "random".
-      lock = new ReentrantLock(true);
+
+      if (isActive)
+      {
+         // There are active lock - make sure that the JCA container kills
+         // this handle by throwing an exception
+
+         throw new ResourceException("Still active locks for " + this);
+      }
    }
 
    protected void lock()
@@ -275,8 +305,19 @@
    
    protected void unlock()
    {
-      if (lock.isHeldByCurrentThread())
+      if (lock.isLocked())
+      {
          lock.unlock();
+      }
+      else
+      {
+         mcf.log.warn("Owner is null");            
+         
+         Throwable t = new Throwable("Thread trying to unlock");
+         t.setStackTrace(Thread.currentThread().getStackTrace());
+
+         mcf.log.warn(t.getMessage(), t);
+      }
    }
 
    public Object getConnection(Subject subject, ConnectionRequestInfo cri) throws ResourceException
@@ -369,16 +410,19 @@
    {
       if(t instanceof SQLException)
       {
-         if(mcf.isStaleConnection((SQLException)t))
+         boolean stale = mcf.isStaleConnection((SQLException)t);
+
+         if(stale)
          {
             t = new StaleConnectionException((SQLException)t);           
          
          }else
          {
-            if(mcf.isExceptionFatal((SQLException)t))
+            boolean fatalException = mcf.isExceptionFatal((SQLException)t);
+
+            if(fatalException)
             {
                broadcastConnectionError(t);
-               
             }
          }
       }
@@ -404,6 +448,11 @@
          }
       }
 
+      // We need to unlock() before sending the connection error to the
+      // event listeners. Otherwise the lock won't be in sync once
+      // cleanup() is called
+      unlock();
+
       Exception ex = null;
       if (e instanceof Exception)
          ex = (Exception) e;

Modified: branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/adapter/jdbc/WrappedConnection.java
===================================================================
--- branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/adapter/jdbc/WrappedConnection.java	2011-07-20 16:11:35 UTC (rev 111803)
+++ branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/adapter/jdbc/WrappedConnection.java	2011-07-20 16:20:43 UTC (rev 111804)
@@ -63,16 +63,23 @@
    
    public WrappedConnection(final BaseWrapperManagedConnection mc)
    {
-      this.mc = mc;
-      if (mc != null)
-         trackStatements = mc.getTrackStatements();
+      setManagedConnection(mc);
    }
 
    void setManagedConnection(final BaseWrapperManagedConnection mc)
    {
       this.mc = mc;
+      this.lockCount = 0;
+
       if (mc != null)
+      {
          trackStatements = mc.getTrackStatements();
+      }
+      else
+      {
+         // Reset the lockedMC reference when returned to the pool
+         lockedMC = null;
+      }
    }
 
    protected void lock() throws SQLException
@@ -92,7 +99,7 @@
 
    protected void unlock()
    {
-      BaseWrapperManagedConnection mc = lockedMC;
+      BaseWrapperManagedConnection mc = this.lockedMC;
       if (--lockCount == 0)
          lockedMC = null;
 

Modified: branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/adapter/jms/JmsManagedConnection.java
===================================================================
--- branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/adapter/jms/JmsManagedConnection.java	2011-07-20 16:11:35 UTC (rev 111803)
+++ branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/adapter/jms/JmsManagedConnection.java	2011-07-20 16:20:43 UTC (rev 111804)
@@ -22,13 +22,13 @@
 package org.jboss.resource.adapter.jms;
 
 import java.io.PrintWriter;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Set;
 import java.util.Vector;
 import java.util.concurrent.TimeUnit;
-import java.util.concurrent.locks.ReentrantLock;
 
 import javax.jms.Connection;
 import javax.jms.ExceptionListener;
@@ -355,11 +355,39 @@
       // destory handles
       destroyHandles();
 
-      // I'm recreating the lock object when we return to the pool
-      // because it looks too nasty to expect the connection handle
-      // to unlock properly in certain race conditions
-      // where the dissociation of the managed connection is "random".
-      lock = new ReentrantLock(true);
+      boolean isActive = false;
+
+      if (lock.hasQueuedThreads())
+      {
+         Collection<Thread> threads = lock.getQueuedThreads();
+         for (Thread thread : threads)
+         {
+            Throwable t = new Throwable("Thread waiting for lock during cleanup");
+            t.setStackTrace(thread.getStackTrace());
+
+            log.warn(t.getMessage(), t);
+         }
+
+         isActive = true;
+      }
+
+      if (lock.isLocked())
+      {
+         Throwable t = new Throwable("Lock owned during cleanup");
+         t.setStackTrace(lock.getOwner().getStackTrace());
+
+         log.warn(t.getMessage(), t);
+         
+         isActive = true;
+      }
+
+      if (isActive)
+      {
+         // There are active lock - make sure that the JCA container kills
+         // this handle by throwing an exception
+
+         throw new ResourceException("Still active locks for " + this);
+      }
    }
 
    /**
@@ -414,8 +442,19 @@
    
    protected void unlock()
    {
-      if (lock.isHeldByCurrentThread())
+      if (lock.isLocked())
+      {
          lock.unlock();
+      }
+      else
+      {
+         log.warn("Owner is null");            
+         
+         Throwable t = new Throwable("Thread trying to unlock");
+         t.setStackTrace(Thread.currentThread().getStackTrace());
+
+         log.warn(t.getMessage(), t);
+      }
    }
 
    /**
@@ -546,6 +585,12 @@
 
       log.warn("Handling jms exception failure: " + this, exception);
 
+      // We need to unlock() before sending the connection error to the
+      // event listeners. Otherwise the lock won't be in sync once
+      // cleanup() is called
+      if (lock.isLocked() && Thread.currentThread().equals(lock.getOwner()))
+         unlock();
+
       try
       {
          con.setExceptionListener(null);

Modified: branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/adapter/jms/JmsSession.java
===================================================================
--- branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/adapter/jms/JmsSession.java	2011-07-20 16:11:35 UTC (rev 111803)
+++ branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/adapter/jms/JmsSession.java	2011-07-20 16:20:43 UTC (rev 111804)
@@ -95,6 +95,8 @@
    public JmsSession(final JmsManagedConnection mc, JmsConnectionRequestInfo info)
    {
       this.mc = mc;
+      this.lockedMC = null;
+      this.lockCount = 0;
       this.info = info;
       if (trace)
          log.trace("new JmsSession " + this + " mc=" + mc + " cri=" + info);
@@ -123,7 +125,7 @@
 
    protected void unlock()
    {
-      JmsManagedConnection mc = this.mc;
+      JmsManagedConnection mc = this.lockedMC;
       if (--lockCount == 0)
          lockedMC = null;
 
@@ -746,6 +748,8 @@
    void destroy()
    {
       mc = null;
+      lockedMC = null;
+      lockCount = 0;
    }
 
    void start() throws JMSException

Modified: branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/connectionmanager/JBossManagedConnectionPool.java
===================================================================
--- branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/connectionmanager/JBossManagedConnectionPool.java	2011-07-20 16:11:35 UTC (rev 111803)
+++ branches/JBPAPP_5_1_1_GA_JBPAPP-6867/connector/src/main/org/jboss/resource/connectionmanager/JBossManagedConnectionPool.java	2011-07-20 16:20:43 UTC (rev 111804)
@@ -367,7 +367,6 @@
       return result;
    }
 
-
    /**
     * Test if a connection can be obtained using default values
     * @return True if a connection was obtained; otherwise false



More information about the jboss-cvs-commits mailing list