tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fha...@apache.org
Subject svn commit: r722506 - in /tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool: ConnectionPool.java FairBlockingQueue.java
Date Tue, 02 Dec 2008 16:02:17 GMT
Author: fhanik
Date: Tue Dec  2 08:02:16 2008
New Revision: 722506

URL: http://svn.apache.org/viewvc?rev=722506&view=rev
Log:
implement cancellation ability for future

Modified:
    tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java
    tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/FairBlockingQueue.java

Modified: tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java?rev=722506&r1=722505&r2=722506&view=diff
==============================================================================
--- tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java (original)
+++ tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ConnectionPool.java Tue
Dec  2 08:02:16 2008
@@ -31,6 +31,8 @@
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
@@ -100,6 +102,11 @@
      */
     protected Constructor proxyClassConstructor;
 
+    /**
+     * Executor service used to cancel Futures
+     */
+    protected ThreadPoolExecutor cancellator = new ThreadPoolExecutor(0,1,1000,TimeUnit.MILLISECONDS,new
LinkedBlockingQueue<Runnable>());
+
 
     //===============================================================================
     //         PUBLIC METHODS
@@ -741,22 +748,27 @@
      * This one retrieves the pooled connection object
      * and performs the initialization according to 
      * interceptors and validation rules.
-     * This class is thread safe.
+     * This class is thread safe and is cancellable
      * @author fhanik
      *
      */
-    protected class ConnectionFuture implements Future<Connection> {
+    protected class ConnectionFuture implements Future<Connection>, Runnable {
         Future<PooledConnection> pcFuture = null;
         AtomicBoolean configured = new AtomicBoolean(false);
         CountDownLatch latch = new CountDownLatch(1);
         Connection result = null;
         SQLException cause = null;
+        AtomicBoolean cancelled = new AtomicBoolean(false);
         public ConnectionFuture(Future<PooledConnection> pcf) {
             this.pcFuture = pcf;
         }
         
         public boolean cancel(boolean mayInterruptIfRunning) {
-            return pcFuture.cancel(mayInterruptIfRunning);
+            if ((!cancelled.get()) && cancelled.compareAndSet(false, true)) {
+                //cancel by retrieving the connection and returning it to the pool
+                ConnectionPool.this.cancellator.execute(this);
+            }
+            return true;
         }
 
         public Connection get() throws InterruptedException, ExecutionException {
@@ -792,13 +804,24 @@
         }
 
         public boolean isCancelled() {
-            return pcFuture.isCancelled();
+            return pcFuture.isCancelled() || cancelled.get();
         }
 
         public boolean isDone() {
             return pcFuture.isDone();
         }
         
+        public void run() {
+            try {
+                Connection con = get(); //complete this future
+                con.close(); //return to the pool
+            }catch (ExecutionException ex) {
+                //we can ignore this
+            }catch (Exception x) {
+                ConnectionPool.log.error("Unable to cancel ConnectionFuture.",x);
+            }
+        }
+        
     }
 
     protected class PoolCleaner extends Thread {

Modified: tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/FairBlockingQueue.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/FairBlockingQueue.java?rev=722506&r1=722505&r2=722506&view=diff
==============================================================================
--- tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/FairBlockingQueue.java
(original)
+++ tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/FairBlockingQueue.java
Tue Dec  2 08:02:16 2008
@@ -23,6 +23,7 @@
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
+import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.locks.ReentrantLock;
@@ -41,7 +42,7 @@
     LinkedList<E> items = null;
 
     LinkedList<ExchangeCountDownLatch<E>> waiters = null;
-
+    
     public FairBlockingQueue() {
         items = new LinkedList<E>();
         waiters = new LinkedList<ExchangeCountDownLatch<E>>();



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message