本文整理汇总了C#中Npgsql.NpgsqlConnector.Open方法的典型用法代码示例。如果您正苦于以下问题:C# NpgsqlConnector.Open方法的具体用法?C# NpgsqlConnector.Open怎么用?C# NpgsqlConnector.Open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Npgsql.NpgsqlConnector
的用法示例。
在下文中一共展示了NpgsqlConnector.Open方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Open
/// <summary>
/// Opens a database connection with the property settings specified by the
/// <see cref="Npgsql.NpgsqlConnection.ConnectionString">ConnectionString</see>.
/// </summary>
public override void Open()
{
// If we're postponing a close (see doc on this variable), the connection is already
// open and can be silently reused
if (_postponingClose)
return;
CheckConnectionClosed();
NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Open");
// Check if there is any missing argument.
if (!settings.ContainsKey(Keywords.Host))
{
throw new ArgumentException(resman.GetString("Exception_MissingConnStrArg"),
NpgsqlConnectionStringBuilder.GetKeyName(Keywords.Host));
}
if (!settings.ContainsKey(Keywords.UserName) && !settings.ContainsKey(Keywords.IntegratedSecurity))
{
throw new ArgumentException(resman.GetString("Exception_MissingConnStrArg"),
NpgsqlConnectionStringBuilder.GetKeyName(Keywords.UserName));
}
// Get a Connector, either from the pool or creating one ourselves.
if (Pooling)
{
connector = NpgsqlConnectorPool.ConnectorPoolMgr.RequestConnector(this);
}
else
{
connector = new NpgsqlConnector(this);
connector.ProvideClientCertificatesCallback += ProvideClientCertificatesCallbackDelegate;
connector.CertificateSelectionCallback += CertificateSelectionCallbackDelegate;
connector.CertificateValidationCallback += CertificateValidationCallbackDelegate;
connector.PrivateKeySelectionCallback += PrivateKeySelectionCallbackDelegate;
connector.ValidateRemoteCertificateCallback += ValidateRemoteCertificateCallbackDelegate;
connector.Open();
}
connector.Notice += NoticeDelegate;
connector.Notification += NotificationDelegate;
if (SyncNotification)
{
connector.AddNotificationThread();
}
if (Enlist)
{
Promotable.Enlist(Transaction.Current);
}
this.OnStateChange (new StateChangeEventArgs(ConnectionState.Closed, ConnectionState.Open));
}
示例2: GetPooledConnector
/// <summary>
/// Find an available pooled connector in the pool, or create a new one if none found.
/// </summary>
private NpgsqlConnector GetPooledConnector(NpgsqlConnection Connection)
{
ConnectorQueue Queue = null;
NpgsqlConnector Connector = null;
// We only need to lock all pools when trying to get one pool or create one.
lock (locker)
{
// Try to find a queue.
if (!PooledConnectors.TryGetValue(Connection.ConnectionString, out Queue))
{
Queue = new ConnectorQueue();
Queue.ConnectionLifeTime = Connection.ConnectionLifeTime;
Queue.MinPoolSize = Connection.MinPoolSize;
PooledConnectors[Connection.ConnectionString] = Queue;
}
}
// Now we can simply lock on the pool itself.
lock (Queue)
{
if (Queue.Available.Count > 0)
{
// Found a queue with connectors. Grab the top one.
// Check if the connector is still valid.
Connector = Queue.Available.Dequeue();
Queue.Busy.Add(Connector, null);
}
}
if (Connector != null) return Connector;
lock (Queue)
{
if (Queue.Available.Count + Queue.Busy.Count < Connection.MaxPoolSize)
{
Connector = new NpgsqlConnector(Connection);
Queue.Busy.Add(Connector, null);
}
}
if (Connector != null)
{
Connector.ProvideClientCertificatesCallback = Connection.ProvideClientCertificatesCallback;
Connector.UserCertificateValidationCallback = Connection.UserCertificateValidationCallback;
try
{
Connector.Open();
}
catch
{
Contract.Assert(Connector.IsBroken);
lock (Queue)
{
Queue.Busy.Remove(Connector);
}
throw;
}
// Meet the MinPoolSize requirement if needed.
if (Connection.MinPoolSize > 1)
{
try
{
lock (Queue)
{
while (Queue.Available.Count + Queue.Busy.Count < Connection.MinPoolSize)
{
NpgsqlConnector spare = new NpgsqlConnector(Connection) {
ProvideClientCertificatesCallback = Connection.ProvideClientCertificatesCallback,
UserCertificateValidationCallback = Connection.UserCertificateValidationCallback
};
spare.Open();
spare.ProvideClientCertificatesCallback = null;
spare.UserCertificateValidationCallback = null;
spare.Connection = null;
Queue.Available.Enqueue(spare);
}
}
}
catch (Exception e)
{
Log.Warn("Exception while trying to open spare connectors to meet MinPoolSize", e);
}
}
//.........这里部分代码省略.........
示例3: GetPooledConnector
/// <summary>
/// Find an available pooled connector in the non-shared pool, or create
/// a new one if none found.
/// </summary>
private NpgsqlConnector GetPooledConnector(NpgsqlConnection Connection)
{
ConnectorQueue Queue;
NpgsqlConnector Connector = null;
// We only need to lock all pools when trying to get one pool or create one.
lock (locker)
{
// Try to find a queue.
if (!PooledConnectors.TryGetValue(Connection.ConnectionString, out Queue))
{
Queue = new ConnectorQueue();
Queue.ConnectionLifeTime = Connection.ConnectionLifeTime;
Queue.MinPoolSize = Connection.MinPoolSize;
PooledConnectors[Connection.ConnectionString] = Queue;
}
}
// Now we can simply lock on the pool itself.
lock (Queue)
{
if (Queue.Available.Count > 0)
{
// Found a queue with connectors. Grab the top one.
// Check if the connector is still valid.
Connector = Queue.Available.Dequeue();
Queue.Busy.Add(Connector, null);
}
}
if (Connector != null)
{
if (!Connector.IsValid())
{
lock (Queue)
{
Queue.Busy.Remove(Connector);
}
Connector.Close();
return GetPooledConnector(Connection); //Try again
}
return Connector;
}
lock (Queue)
{
if (Queue.Available.Count + Queue.Busy.Count < Connection.MaxPoolSize)
{
Connector = new NpgsqlConnector(Connection);
Queue.Busy.Add(Connector, null);
}
}
if (Connector != null)
{
Connector.ProvideClientCertificatesCallback += Connection.ProvideClientCertificatesCallbackDelegate;
Connector.CertificateSelectionCallback += Connection.CertificateSelectionCallbackDelegate;
Connector.CertificateValidationCallback += Connection.CertificateValidationCallbackDelegate;
Connector.PrivateKeySelectionCallback += Connection.PrivateKeySelectionCallbackDelegate;
Connector.ValidateRemoteCertificateCallback += Connection.ValidateRemoteCertificateCallbackDelegate;
try
{
Connector.Open();
}
catch
{
lock (Queue)
{
Queue.Busy.Remove(Connector);
}
Connector.Close();
throw;
}
// Meet the MinPoolSize requirement if needed.
if (Connection.MinPoolSize > 1)
{
lock (Queue)
{
//.........这里部分代码省略.........
示例4: Open
/// <summary>
/// Opens a database connection with the property settings specified by the
/// <see cref="NpgsqlConnection.ConnectionString">ConnectionString</see>.
/// </summary>
public override void Open()
{
// If we're postponing a close (see doc on this variable), the connection is already
// open and can be silently reused
if (_postponingClose)
return;
CheckConnectionClosed();
_log.Debug("Opening connnection");
// Check if there is any missing argument.
if (!_settings.ContainsKey(Keywords.Host))
{
throw new ArgumentException(L10N.MissingConnStrArg, Keywords.Host.ToString());
}
if (!_settings.ContainsKey(Keywords.UserName) && !_settings.ContainsKey(Keywords.IntegratedSecurity))
{
throw new ArgumentException(L10N.MissingConnStrArg, Keywords.UserName.ToString());
}
// Get a Connector, either from the pool or creating one ourselves.
if (Pooling)
{
Connector = NpgsqlConnectorPool.ConnectorPoolMgr.RequestConnector(this);
}
else
{
Connector = new NpgsqlConnector(this);
Connector.ProvideClientCertificatesCallback += ProvideClientCertificatesCallbackDelegate;
Connector.CertificateSelectionCallback += CertificateSelectionCallbackDelegate;
Connector.CertificateValidationCallback += CertificateValidationCallbackDelegate;
Connector.PrivateKeySelectionCallback += PrivateKeySelectionCallbackDelegate;
Connector.ValidateRemoteCertificateCallback += ValidateRemoteCertificateCallbackDelegate;
Connector.Open();
}
Connector.Notice += NoticeDelegate;
Connector.Notification += NotificationDelegate;
if (SyncNotification)
{
Connector.AddNotificationThread();
}
if (Enlist)
{
Promotable.Enlist(Transaction.Current);
}
OnStateChange(new StateChangeEventArgs(ConnectionState.Closed, ConnectionState.Open));
}