db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rhille...@apache.org
Subject svn commit: r1455230 - in /db/derby/code/trunk/java: engine/org/apache/derby/jdbc/Driver20.java testing/org/apache/derbyTesting/functionTests/tests/store/InterruptResilienceTest.java
Date Mon, 11 Mar 2013 16:50:06 GMT
Author: rhillegas
Date: Mon Mar 11 16:50:06 2013
New Revision: 1455230

URL: http://svn.apache.org/r1455230
Log:
DERBY-6094: Make login timeouts recover from interrupts and retry.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/jdbc/Driver20.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/InterruptResilienceTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/jdbc/Driver20.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/jdbc/Driver20.java?rev=1455230&r1=1455229&r2=1455230&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/jdbc/Driver20.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/jdbc/Driver20.java Mon Mar 11 16:50:06
2013
@@ -52,10 +52,13 @@ import java.util.concurrent.ExecutionExc
 import java.util.concurrent.Executors;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Future;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.SynchronousQueue;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.TimeUnit;
+import org.apache.derby.iapi.util.InterruptStatus;
 
 /**
 	This class extends the local JDBC driver in order to determine at JBMS
@@ -66,11 +69,13 @@ import java.util.concurrent.TimeUnit;
 
 public abstract class Driver20 extends InternalDriver implements Driver {
 
-    private static  ExecutorService _executorPool;
+    private static  ThreadPoolExecutor _executorPool;
     static
     {
-        _executorPool = Executors.newCachedThreadPool();
-        ((ThreadPoolExecutor) _executorPool).setThreadFactory( new DaemonThreadFactory()
);
+        _executorPool = new ThreadPoolExecutor(0, Integer.MAX_VALUE,
+                                      60L, TimeUnit.SECONDS,
+                                      new SynchronousQueue<Runnable>());   
+        _executorPool.setThreadFactory( new DaemonThreadFactory() );
     } 
 
 	private static final String[] BOOLEAN_CHOICES = {"false", "true"};
@@ -245,21 +250,37 @@ public abstract class Driver20 extends I
         return connect( url, info, DriverManager.getLoginTimeout() );
     }
     
+    private static final String driver20 = "driver20"; 
     /**
      * Use java.util.concurrent package to enforce login timeouts.
      */
     protected EmbedConnection  timeLogin( String url, Properties info, int loginTimeoutSeconds
)
         throws SQLException
     {
-        LoginCallable callable = new LoginCallable( this, url, info );
-        Future<EmbedConnection>  task = _executorPool.submit( callable );
-
         try {
-            return task.get( loginTimeoutSeconds, TimeUnit.SECONDS );
+            LoginCallable callable = new LoginCallable( this, url, info );
+            Future<EmbedConnection>  task = _executorPool.submit( callable );
+            long startTime = System.currentTimeMillis();
+            long interruptedTime = startTime;
+            
+            while ((startTime - interruptedTime) / 1000.0 < loginTimeoutSeconds) {
+                try {
+                    return task.get( loginTimeoutSeconds, TimeUnit.SECONDS );
+                }
+                catch (InterruptedException ie) {
+                    interruptedTime = System.currentTimeMillis();
+                    InterruptStatus.setInterrupted();
+                    continue;
+                }
+                catch (ExecutionException ee) { throw processException( ee ); }
+                catch (TimeoutException te) { throw Util.generateCsSQLException( SQLState.LOGIN_TIMEOUT
); }
+            }
+            
+            // Timed out due to interrupts, throw.
+            throw Util.generateCsSQLException( SQLState.LOGIN_TIMEOUT );
+        } finally {
+            InterruptStatus.restoreIntrFlagIfSeen();
         }
-        catch (InterruptedException ie) { throw processException( ie ); }
-        catch (ExecutionException ee) { throw processException( ee ); }
-        catch (TimeoutException te) { throw Util.generateCsSQLException( SQLState.LOGIN_TIMEOUT
); }
     }
     /** Process exceptions raised while running a timed login */
     private SQLException    processException( Throwable t )
@@ -270,7 +291,7 @@ public abstract class Driver20 extends I
     }
 
     /** Thread factory to produce daemon threads which don't block VM shutdown */
-    public  static  final   class   DaemonThreadFactory implements ThreadFactory
+    private static  final   class   DaemonThreadFactory implements ThreadFactory
     {
         public  Thread newThread( Runnable r )
         {

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/InterruptResilienceTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/InterruptResilienceTest.java?rev=1455230&r1=1455229&r2=1455230&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/InterruptResilienceTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/InterruptResilienceTest.java
Mon Mar 11 16:50:06 2013
@@ -134,6 +134,10 @@ public class InterruptResilienceTest ext
 
         thisConf = TestConfiguration.getCurrent();
         threadNo = 0;    // counter for multiple threads tests
+
+        // test that we recover from login timeouts
+        DriverManager.setLoginTimeout( 10 );
+        
         allDone = false; // flag for threads to terminate
     }
 
@@ -143,6 +147,8 @@ public class InterruptResilienceTest ext
     protected void tearDown()
             throws java.lang.Exception {
 
+        DriverManager.setLoginTimeout( 0 );
+
         // Forget about uncommitted changes
         rollback();
 



Mime
View raw message