Esempio n. 1
0
 public MongoConnection AcquireConnection()
 {
     if (_serverInstance == null)
     {
         // first time we need a connection let Server.AcquireConnection pick the server instance
         var connection = _server.AcquireConnection(_readPreference);
         _serverInstance = connection.ServerInstance;
         return(connection);
     }
     else
     {
         // all subsequent requests for the same cursor must go to the same server instance
         return(_server.AcquireConnection(_serverInstance));
     }
 }
Esempio n. 2
0
        #pragma warning restore

        private TCommandResult RunCommandAs <TCommandResult>(
            IMongoCommand command,
            IBsonSerializer <TCommandResult> resultSerializer) where TCommandResult : CommandResult
        {
            var readerSettings = new BsonBinaryReaderSettings
            {
                Encoding           = _settings.ReadEncoding ?? MongoDefaults.ReadEncoding,
                GuidRepresentation = _settings.GuidRepresentation
            };
            var writerSettings = new BsonBinaryWriterSettings
            {
                Encoding           = _settings.WriteEncoding ?? MongoDefaults.WriteEncoding,
                GuidRepresentation = _settings.GuidRepresentation
            };
            var readPreference = _settings.ReadPreference;

            if (readPreference != ReadPreference.Primary)
            {
                if (_server.ProxyType == MongoServerProxyType.Unknown)
                {
                    _server.Connect();
                }
                if (_server.ProxyType == MongoServerProxyType.ReplicaSet && !CanCommandBeSentToSecondary.Delegate(command.ToBsonDocument()))
                {
                    readPreference = ReadPreference.Primary;
                }
            }
            var flags = (readPreference == ReadPreference.Primary) ? QueryFlags.None : QueryFlags.SlaveOk;

            var commandOperation = new CommandOperation <TCommandResult>(
                _name,
                readerSettings,
                writerSettings,
                command,
                flags,
                null, // options
                readPreference,
                resultSerializer);

            var connection = _server.AcquireConnection(readPreference);

            try
            {
                return(commandOperation.Execute(connection));
            }
            finally
            {
                _server.ReleaseConnection(connection);
            }
        }