本文整理汇总了C#中MongoConnection.Open方法的典型用法代码示例。如果您正苦于以下问题:C# MongoConnection.Open方法的具体用法?C# MongoConnection.Open怎么用?C# MongoConnection.Open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MongoConnection
的用法示例。
在下文中一共展示了MongoConnection.Open方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EnsureMinConnectionPoolSizeWorkItem
// private methods
private void EnsureMinConnectionPoolSizeWorkItem(object state)
{
// make sure only one instance of EnsureMinConnectionPoolSizeWorkItem is running at a time
if (_inEnsureMinConnectionPoolSizeWorkItem)
{
return;
}
_inEnsureMinConnectionPoolSizeWorkItem = true;
try
{
// keep creating connections one at a time until MinConnectionPoolSize is reached
var forGenerationId = (int)state;
while (true)
{
lock (_connectionPoolLock)
{
// stop if the connection pool generationId has changed or we have already reached MinConnectionPoolSize
if (_generationId != forGenerationId || _poolSize >= _settings.MinConnectionPoolSize)
{
return;
}
}
var connection = new MongoConnection(this);
try
{
connection.Open();
// compare against MaxConnectionPoolSize instead of MinConnectionPoolSize
// because while we were opening this connection many others may have already been created
// and we don't want to throw this one away unless we would exceed MaxConnectionPoolSize
var added = false;
lock (_connectionPoolLock)
{
if (_generationId == forGenerationId && _poolSize < _settings.MaxConnectionPoolSize)
{
_availableConnections.Add(connection);
_poolSize++;
added = true;
Monitor.Pulse(_connectionPoolLock);
}
}
if (!added)
{
// turns out we couldn't use the connection after all
connection.Close();
}
}
catch
{
// TODO: log exception?
// wait a bit before trying again
Thread.Sleep(TimeSpan.FromSeconds(1));
}
}
}
catch
{
// don't let unhandled exceptions leave EnsureMinConnectionPoolSizeWorkItem
// if the minimum connection pool size was not achieved a new work item will be queued shortly
// TODO: log exception?
}
finally
{
_inEnsureMinConnectionPoolSizeWorkItem = false;
}
}
示例2: CreateInitialConnectionsWorkItem
internal void CreateInitialConnectionsWorkItem(
object state // ignored
) {
// keep creating connections one at a time until MinConnectionPoolSize is reached
while (true) {
lock (connectionPoolLock) {
// stop if connection pool has been closed or we have already reached MinConnectionPoolSize
if (closed || poolSize >= server.Settings.MinConnectionPoolSize) {
return;
}
}
var connection = new MongoConnection(this);
try {
connection.Open();
// compare against MaxConnectionPoolSize instead of MinConnectionPoolSize
// because while we were opening this connection many others may have already been created
// and we don't want to throw this one away unless we would exceed MaxConnectionPoolSize
var added = false;
lock (connectionPoolLock) {
if (poolSize < server.Settings.MaxConnectionPoolSize) {
availableConnections.Add(connection);
poolSize++;
added = true;
}
}
if (!added) {
// turns out we couldn't use the connection after all
connection.Close();
}
} catch {
// TODO: log exception?
// wait a bit before trying again
Thread.Sleep(TimeSpan.FromSeconds(1));
}
}
}