2004-02-29 Francisco Figueiredo Jr. <fxjrlists@yahoo.com.br>
authorFrancisco Figueiredo Jr. <fxjr@mono-cvs.ximian.com>
Sun, 29 Feb 2004 18:22:23 +0000 (18:22 -0000)
committerFrancisco Figueiredo Jr. <fxjr@mono-cvs.ximian.com>
Sun, 29 Feb 2004 18:22:23 +0000 (18:22 -0000)
        * Npgsql/NpgsqlConnection.cs: Better handling of connection encoding. Added support for encoding and connection timeout in connection string.
        * Npgsql/NpgsqlConnectorPool.cs: Added support for timeout and max connection pool.

svn path=/trunk/mcs/; revision=23583

mcs/class/Npgsql/ChangeLog
mcs/class/Npgsql/Npgsql/NpgsqlConnection.cs
mcs/class/Npgsql/Npgsql/NpgsqlConnectorPool.cs

index 013be4f9b022ee207bd45f6e4b6e3968f4bab644..766750681e493a7945e615dd153c502f308dc5f6 100644 (file)
@@ -1,4 +1,8 @@
 
+2004-02-29  Francisco Figueiredo Jr.  <fxjrlists@yahoo.com.br>
+       * Npgsql/NpgsqlConnection.cs: Better handling of connection encoding. Added support for encoding and connection timeout in connection string.
+       * Npgsql/NpgsqlConnectorPool.cs: Added support for timeout and max connection pool.
+
 2004-02-28  Francisco Figueiredo Jr.  <fxjrlists@yahoo.com.br>
 
        * Npgsql/NpgsqlConnector.cs: Added support for connection encoding.
index 599d1aa194c7cb32c9ddaaf4d75aa0f63bd9c851..5bc423c425e85e673b3ec21ab079602dd3d59d07 100755 (executable)
@@ -88,6 +88,11 @@ namespace Npgsql
         // These are for the connection pool
         internal readonly String MIN_POOL_SIZE = "MINPOOLSIZE";
         internal readonly String MAX_POOL_SIZE = "MAXPOOLSIZE";
+        
+        internal readonly String CONN_ENCODING = "ENCODING";
+        
+        internal readonly String CONN_TIMEOUT = "TIMEOUT";
+        
 
         // Values for possible CancelRequest messages.
         private NpgsqlBackEndKeyData backend_keydata;
@@ -115,7 +120,9 @@ namespace Npgsql
 
         private System.Resources.ResourceManager resman;
 
-        private Int32          _backendProtocolVersion;
+        private Int32                   _backendProtocolVersion;
+        
+        private Int32                   _connectionTimeout;
 
 
         /// <summary>
@@ -145,6 +152,9 @@ namespace Npgsql
 
             _mediator = new NpgsqlMediator();
             _oidToNameMapping = new Hashtable();
+            
+            _connectionTimeout = 15;
+            
 
             if (connection_string != String.Empty)
                 ParseConnectionString();
@@ -180,12 +190,11 @@ namespace Npgsql
         /// before terminating the attempt and generating an error.
         /// </summary>
         /// <value>The time (in seconds) to wait for a connection to open. The default value is 15 seconds.</value>
-        /// <remarks>This property currently always returns zero</remarks>
         [NpgsqlSysDescription("Description_ConnectionTimeout", typeof(NpgsqlConnection))]
         public Int32 ConnectionTimeout {
             get
             {
-                return 0;
+                return _connectionTimeout;
             }
         }
 
@@ -358,7 +367,11 @@ namespace Npgsql
                 connection_string_values[MIN_POOL_SIZE] = "1";
             if (connection_string_values[MAX_POOL_SIZE] == null)
                 connection_string_values[MAX_POOL_SIZE] = "20";
-                
+            if (connection_string_values[CONN_ENCODING] == null)
+                connection_string_values[CONN_ENCODING] = "SQL_ASCII";
+            if (connection_string_values[CONN_TIMEOUT] == null)
+                connection_string_values[CONN_TIMEOUT] = "15";                
+            
             try
             {
             
@@ -368,7 +381,10 @@ namespace Npgsql
 
                 lock(ConnectorPool.ConnectorPoolMgr)
                 {
-                    Connector = ConnectorPool.ConnectorPoolMgr.RequestConnector(ConnectionString, false);
+                    Connector = ConnectorPool.ConnectorPoolMgr.RequestConnector(ConnectionString, 
+                                                                                Int32.Parse((String)connection_string_values[MAX_POOL_SIZE]),
+                                                                                Int32.Parse((String)connection_string_values[CONN_TIMEOUT]),
+                                                                                false);
                     Connector.InUse = true;
                 }
                 
@@ -428,11 +444,11 @@ namespace Npgsql
                     
                     // Adjust client encoding. 
                 
-                    NpgsqlCommand commandEncoding = new NpgsqlCommand("show client_encoding", this);
-                    String clientEncoding = (String)commandEncoding.ExecuteScalar();
+                    //NpgsqlCommand commandEncoding = new NpgsqlCommand("show client_encoding", this);
+                    //String clientEncoding = (String)commandEncoding.ExecuteScalar();
     
-                    if (clientEncoding.Equals("UNICODE"))
-                      connection_encoding = Encoding.UTF8;
+                    if (connection_string_values[CONN_ENCODING].Equals("UNICODE"))
+                        connection_encoding = Encoding.UTF8;
                   
                     
                     Connector.ServerVersion = ServerVersion;
@@ -554,6 +570,8 @@ namespace Npgsql
         /// Password   - Password for clear text authentication
         /// MinPoolSize - Min size of connection pool
         /// MaxPoolSize - Max size of connection pool
+        /// Encoding    - Encoding to be used
+        /// Timeout     - Time to wait for connection open. In seconds.
         /// </summary>
         private void ParseConnectionString()
         {
index 4eb20332ad365b750160e446cbdbe6e49fd41f6f..a7c90a4aecad3dae5db11b920a5ba9366cf4ec69 100755 (executable)
@@ -24,6 +24,7 @@
 using System;
 using System.Collections;
 using Npgsql;
+using System.Threading;
 
 namespace Npgsql
 {
@@ -128,13 +129,15 @@ namespace Npgsql
         /// <param name="Shared">Allows multiple connections
         /// on a single connector. </param>
         /// <returns>A pooled connector object.</returns>
-        internal Npgsql.Connector RequestConnector ( String connectionString,
-                bool Shared )
+        internal Npgsql.Connector RequestConnector (String connectionString,
+                                                    Int32 maxPoolSize,
+                                                    Int32 timeout,
+                                                    Boolean shared )
         {
             Connector connector;
             ArrayList connectorPool = null;
 
-            if ( Shared )
+            if ( shared )
             {
                 // if a shared connector is requested then the
                 // Shared Connector List is searched first
@@ -170,27 +173,56 @@ namespace Npgsql
                 
                 // Now look for an available connector.
                 
-                
-                foreach (Connector c in connectorPool)
-                {
-                    if (!c.InUse)
-                        return c;
-                }
+                Connector freeConnector = FindFreeConnector(connectorPool);
+                if (freeConnector != null)
+                    return freeConnector;
                 
                 // No suitable connector could be found, so create new one
-                connector = new Npgsql.Connector( connectionString, Shared );
+                // if there is room available.
+                
+                if (connectorPool.Count < maxPoolSize)
+                {
+                    connector = new Npgsql.Connector(connectionString, shared);
 
-            connectorPool.Add(connector);
+                    connectorPool.Add(connector);
             
 
-            // and then returned to the caller
-            return connector;
-                
-                
+                    // and then returned to the caller
+                    return connector;                
+                }
+                else
+                {
+                    // keep checking in the pool until some connector is available or
+                    // a timeout occurs.
+                    Int32 timeoutMilliseconds = timeout * 1000;
+                    
+                    while (timeoutMilliseconds > 0)
+                    {
+                        Connector freeConnector2 = FindFreeConnector(connectorPool);
+                        if (freeConnector2 != null)
+                            return freeConnector2;
+                        else
+                            Thread.Sleep(timeoutMilliseconds % 900);
+                        timeoutMilliseconds -= 900;
+                    }
+                    
+                    throw new NpgsqlException("Timeout while getting a connection from pool.");
+                    
+                }
                 
             }
 
+        }
+        
+        private Connector FindFreeConnector(ArrayList connectorPool)
+        {
+            foreach (Connector c in connectorPool)
+            {
+                if (!c.InUse)
+                    return c;
+            }
             
+            return null;
         }
     }
 }