You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by jo...@apache.org on 2013/07/29 23:42:18 UTC

svn commit: r1508207 - in /cayenne/main/trunk: docs/doc/src/main/resources/ framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/configuration/ framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/configuration/server/ fra...

Author: johnthuss
Date: Mon Jul 29 21:42:17 2013
New Revision: 1508207

URL: http://svn.apache.org/r1508207
Log:
CAY-1844 Configuration for maximum time to wait for an available DB connection

Modified:
    cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
    cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/configuration/Constants.java
    cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java
    cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/configuration/server/XMLPoolingDataSourceFactory.java
    cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/conn/PoolManager.java

Modified: cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt?rev=1508207&r1=1508206&r2=1508207&view=diff
==============================================================================
--- cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt (original)
+++ cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt Mon Jul 29 21:42:17 2013
@@ -77,6 +77,7 @@ CAY-1829 Make ResultIterator implement I
 CAY-1836 Firebird Adapter 
 CAY-1838 Deprecate EntityResolver.indexedByClassProperty
 CAY-1840 Conditionally log slow / long-running queries
+CAY-1844 Configuration for maximum time to wait for an available DB connection
 
 Bug Fixes:
 

Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/configuration/Constants.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/configuration/Constants.java?rev=1508207&r1=1508206&r2=1508207&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/configuration/Constants.java (original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/configuration/Constants.java Mon Jul 29 21:42:17 2013
@@ -139,7 +139,15 @@ public interface Constants {
      * clause size limitations and memory usage efficiency.
      */
     public static final String SERVER_MAX_ID_QUALIFIER_SIZE_PROPERTY = "cayenne.server.max_id_qualifier_size";
-
+    
+    /**
+     * Defines a maximum time in milliseconds that a connection request could wait in the
+     * connection queue. After this period expires, an exception will be thrown in the
+     * calling method. A value of zero will make the thread wait until a connection is
+     * available with no time out. Defaults to 20 seconds.
+     */
+    public static final String SERVER_MAX_QUEUE_WAIT_TIME = "cayenne.jdbc.max_wait";
+    
     /** Defines if database uses case-insensitive collation */
     public final static String CI_PROPERTY = "cayenne.runtime.db.collation.assume.ci";
     

Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java?rev=1508207&r1=1508206&r2=1508207&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java (original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java Mon Jul 29 21:42:17 2013
@@ -82,7 +82,8 @@ public class PropertyDataSourceFactory i
                     maxConnections,
                     username,
                     password,
-                    jdbcEventLogger);
+                    jdbcEventLogger,
+                    properties.getLong(Constants.SERVER_MAX_QUEUE_WAIT_TIME, PoolManager.MAX_QUEUE_WAIT_DEFAULT));
         }
         catch (Exception e) {
             jdbcEventLogger.logConnectFailure(e);

Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/configuration/server/XMLPoolingDataSourceFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/configuration/server/XMLPoolingDataSourceFactory.java?rev=1508207&r1=1508206&r2=1508207&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/configuration/server/XMLPoolingDataSourceFactory.java (original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/configuration/server/XMLPoolingDataSourceFactory.java Mon Jul 29 21:42:17 2013
@@ -21,7 +21,9 @@ package org.apache.cayenne.configuration
 import javax.sql.DataSource;
 
 import org.apache.cayenne.ConfigurationException;
+import org.apache.cayenne.configuration.Constants;
 import org.apache.cayenne.configuration.DataNodeDescriptor;
+import org.apache.cayenne.configuration.RuntimeProperties;
 import org.apache.cayenne.conn.DataSourceInfo;
 import org.apache.cayenne.conn.PoolManager;
 import org.apache.cayenne.di.Inject;
@@ -45,6 +47,9 @@ public class XMLPoolingDataSourceFactory
     @Inject
     protected JdbcEventLogger jdbcEventLogger;
 
+    @Inject 
+    private RuntimeProperties properties;
+    
     @Override
     public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception {
 
@@ -59,7 +64,8 @@ public class XMLPoolingDataSourceFactory
         try {
             return new PoolManager(dataSourceDescriptor.getJdbcDriver(), dataSourceDescriptor.getDataSourceUrl(),
                     dataSourceDescriptor.getMinConnections(), dataSourceDescriptor.getMaxConnections(),
-                    dataSourceDescriptor.getUserName(), dataSourceDescriptor.getPassword(), jdbcEventLogger);
+                    dataSourceDescriptor.getUserName(), dataSourceDescriptor.getPassword(), jdbcEventLogger,
+                    properties.getLong(Constants.SERVER_MAX_QUEUE_WAIT_TIME, PoolManager.MAX_QUEUE_WAIT_DEFAULT));
         } catch (Exception e) {
             jdbcEventLogger.logConnectFailure(e);
             throw e;

Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/conn/PoolManager.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/conn/PoolManager.java?rev=1508207&r1=1508206&r2=1508207&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/conn/PoolManager.java (original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/conn/PoolManager.java Mon Jul 29 21:42:17 2013
@@ -47,10 +47,20 @@ public class PoolManager implements Scop
     /**
      * Defines a maximum time in milliseconds that a connection request could wait in the
      * connection queue. After this period expires, an exception will be thrown in the
-     * calling method. In the future this parameter should be made configurable.
+     * calling method. 
      */
-    public static final int MAX_QUEUE_WAIT = 20000;
+    public static final int MAX_QUEUE_WAIT_DEFAULT = 20000;
 
+    /**
+     * An exception indicating that a connection request waiting in the queue
+     * timed out and was unable to obtain a connection.
+     */
+    public static class ConnectionUnavailableException extends SQLException {
+    	public ConnectionUnavailableException(String message) {
+    		super(message);
+    	}
+    }
+    
     protected ConnectionPoolDataSource poolDataSource;
     protected int minConnections;
     protected int maxConnections;
@@ -65,7 +75,8 @@ public class PoolManager implements Scop
     private PoolMaintenanceThread poolMaintenanceThread;
 
     private boolean shuttingDown;
-
+    private long maxQueueWaitTime;
+    
     /**
      * Creates new PoolManager using org.apache.cayenne.conn.PoolDataSource for an
      * underlying ConnectionPoolDataSource.
@@ -73,11 +84,11 @@ public class PoolManager implements Scop
     public PoolManager(String jdbcDriver, String dataSourceUrl, int minCons, int maxCons,
             String userName, String password) throws SQLException {
 
-        this(jdbcDriver, dataSourceUrl, minCons, maxCons, userName, password, null);
+        this(jdbcDriver, dataSourceUrl, minCons, maxCons, userName, password, null, MAX_QUEUE_WAIT_DEFAULT);
     }
 
     public PoolManager(String jdbcDriver, String dataSourceUrl, int minCons, int maxCons,
-            String userName, String password, JdbcEventLogger logger) throws SQLException {
+            String userName, String password, JdbcEventLogger logger, long maxQueueWaitTime) throws SQLException {
 
         if (logger != null) {
             DataSourceInfo info = new DataSourceInfo();
@@ -95,7 +106,7 @@ public class PoolManager implements Scop
         DriverDataSource driverDS = new DriverDataSource(jdbcDriver, dataSourceUrl);
         driverDS.setLogger(logger);
         PoolDataSource poolDS = new PoolDataSource(driverDS);
-        init(poolDS, minCons, maxCons, userName, password);
+        init(poolDS, minCons, maxCons, userName, password, maxQueueWaitTime);
     }
 
     /**
@@ -111,7 +122,7 @@ public class PoolManager implements Scop
      */
     public PoolManager(ConnectionPoolDataSource poolDataSource, int minCons, int maxCons,
             String userName, String password) throws SQLException {
-        init(poolDataSource, minCons, maxCons, userName, password);
+        init(poolDataSource, minCons, maxCons, userName, password, MAX_QUEUE_WAIT_DEFAULT);
     }
 
     /** Initializes pool. Normally called from constructor. */
@@ -120,7 +131,8 @@ public class PoolManager implements Scop
             int minCons,
             int maxCons,
             String userName,
-            String password) throws SQLException {
+            String password,
+            long maxQueueWaitTime) throws SQLException {
 
         // do sanity checks...
         if (maxConnections < 0) {
@@ -146,7 +158,8 @@ public class PoolManager implements Scop
         this.minConnections = minCons;
         this.maxConnections = maxCons;
         this.poolDataSource = poolDataSource;
-
+        this.maxQueueWaitTime = maxQueueWaitTime;
+        
         // init pool... use linked lists to use the queue in the FIFO manner
         usedPool = new LinkedList<PooledConnection>();
         unusedPool = new LinkedList<PooledConnection>();
@@ -426,20 +439,20 @@ public class PoolManager implements Scop
             // before the full wait period expired, and no connections are
             // available yet, go back to sleep. Otherwise we don't give a maintenance
             // thread a chance to increase pool size
-            long waitTill = System.currentTimeMillis() + MAX_QUEUE_WAIT;
-
+            long waitTill = System.currentTimeMillis() + maxQueueWaitTime;
+        	
             do {
                 try {
-                    wait(MAX_QUEUE_WAIT);
+                    wait(maxQueueWaitTime);
                 }
                 catch (InterruptedException iex) {
                     // ignoring
                 }
 
-            } while (unusedPool.size() == 0 && waitTill > System.currentTimeMillis());
+            } while (unusedPool.size() == 0 && (maxQueueWaitTime == 0 || waitTill > System.currentTimeMillis()));
 
             if (unusedPool.size() == 0) {
-                throw new SQLException(
+                throw new ConnectionUnavailableException(
                         "Can't obtain connection. Request timed out. Total used connections: "
                                 + usedPool.size());
             }