// internal methods internal MongoConnection AcquireConnection(MongoDatabase database) { MongoConnection connection; lock (_serverInstanceLock) { if (_state != MongoServerState.Connected) { var message = string.Format("Server instance {0} is no longer connected.", _address); throw new InvalidOperationException(message); } } connection = _connectionPool.AcquireConnection(database); // check authentication outside the lock because it might involve a round trip to the server try { connection.CheckAuthentication(database); // will authenticate if necessary } catch (MongoAuthenticationException) { // don't let the connection go to waste just because authentication failed _connectionPool.ReleaseConnection(connection); throw; } return(connection); }
/// <summary> /// Checks whether the server is alive (throws an exception if not). /// </summary> public void Ping() { var connection = connectionPool.AcquireConnection(null); try { var pingCommand = new CommandDocument("ping", 1); connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, pingCommand); } finally { connectionPool.ReleaseConnection(connection); } }
internal void Connect( bool slaveOk ) { if (state != MongoServerState.Disconnected) { var message = string.Format("MongoServerInstance.Connect called when state is: {0}", state); throw new InvalidOperationException(message); } State = MongoServerState.Connecting; connectException = null; try { endPoint = address.ToIPEndPoint(server.Settings.AddressFamily); var connectionPool = new MongoConnectionPool(this); try { var connection = connectionPool.AcquireConnection(null); try { try { var isMasterCommand = new CommandDocument("ismaster", 1); isMasterResult = connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, isMasterCommand); } catch (MongoCommandException ex) { isMasterResult = ex.CommandResult; throw; } isPrimary = isMasterResult.Response["ismaster", false].ToBoolean(); isSecondary = isMasterResult.Response["secondary", false].ToBoolean(); isPassive = isMasterResult.Response["passive", false].ToBoolean(); isArbiter = isMasterResult.Response["arbiterOnly", false].ToBoolean(); if (!isPrimary && !slaveOk) { throw new MongoConnectionException("Server is not a primary and SlaveOk is false"); } maxDocumentSize = isMasterResult.Response["maxBsonObjectSize", MongoDefaults.MaxDocumentSize].ToInt32(); maxMessageLength = Math.Max(MongoDefaults.MaxMessageLength, maxDocumentSize + 1024); // derived from maxDocumentSize } finally { connectionPool.ReleaseConnection(connection); } } catch { connectionPool.Close(); throw; } State = MongoServerState.Connected; this.connectionPool = connectionPool; } catch (Exception ex) { State = MongoServerState.Disconnected; connectException = ex; throw; } }
internal MongoConnection AcquireConnection( MongoDatabase database ) { var connection = connectionPool.AcquireConnection(database); try { connection.CheckAuthentication(database); // will authenticate if necessary } catch (MongoAuthenticationException) { // don't let the connection go to waste just because authentication failed connectionPool.ReleaseConnection(connection); throw; } return(connection); }