// 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;
private System.Resources.ResourceManager resman;
- private Int32 _backendProtocolVersion;
+ private Int32 _backendProtocolVersion;
+
+ private Int32 _connectionTimeout;
/// <summary>
_mediator = new NpgsqlMediator();
_oidToNameMapping = new Hashtable();
+
+ _connectionTimeout = 15;
+
if (connection_string != String.Empty)
ParseConnectionString();
/// 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;
}
}
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
{
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;
}
// 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;
/// 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()
{
using System;
using System.Collections;
using Npgsql;
+using System.Threading;
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
// 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;
}
}
}