Example #1
0
        /// <summary>
        /// Connects to the server. Normally there is no need to call this method as
        /// the driver will connect to the server automatically when needed.
        /// </summary>
        /// <param name="timeout">How long to wait before timing out.</param>
        public virtual void Connect(
            TimeSpan timeout
            )
        {
            lock (serverLock) {
                if (state == MongoServerState.Disconnected)
                {
                    switch (settings.ConnectionMode)
                    {
                    case ConnectionMode.Direct:
                        var directConnector = new DirectConnector(this);
                        directConnector.Connect(timeout);
                        break;

                    case ConnectionMode.ReplicaSet:
                        var replicaSetConnector = new ReplicaSetConnector(this);
                        replicaSetConnector.Connect(timeout);
                        break;

                    default:
                        throw new MongoInternalException("Invalid ConnectionMode");
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Connects to the server. Normally there is no need to call this method as
        /// the driver will connect to the server automatically when needed.
        /// </summary>
        /// <param name="timeout">How long to wait before timing out.</param>
        public virtual void Connect(
            TimeSpan timeout
            )
        {
            lock (serverLock) {
                if (state != MongoServerState.Connected)
                {
                    state = MongoServerState.Connecting;
                    try {
                        switch (settings.ConnectionMode)
                        {
                        case ConnectionMode.Direct:
                            var directConnector = new DirectConnector(this);
                            directConnector.Connect(timeout);
                            primaryConnectionPool    = new MongoConnectionPool(this, directConnector.Connection);
                            secondaryConnectionPools = null;
                            replicaSet       = null;
                            maxDocumentSize  = directConnector.MaxDocumentSize;
                            maxMessageLength = directConnector.MaxMessageLength;
                            break;

                        case ConnectionMode.ReplicaSet:
                            var replicaSetConnector = new ReplicaSetConnector(this);
                            replicaSetConnector.Connect(timeout);
                            primaryConnectionPool = new MongoConnectionPool(this, replicaSetConnector.PrimaryConnection);
                            if (settings.SlaveOk && replicaSetConnector.SecondaryConnections.Count > 0)
                            {
                                secondaryConnectionPools = new List <MongoConnectionPool>();
                                foreach (var connection in replicaSetConnector.SecondaryConnections)
                                {
                                    var secondaryConnectionPool = new MongoConnectionPool(this, connection);
                                    secondaryConnectionPools.Add(secondaryConnectionPool);
                                }
                            }
                            else
                            {
                                secondaryConnectionPools = null;
                            }
                            replicaSet       = replicaSetConnector.ReplicaSet;
                            maxDocumentSize  = replicaSetConnector.MaxDocumentSize;
                            maxMessageLength = replicaSetConnector.MaxMessageLength;
                            break;

                        default:
                            throw new MongoInternalException("Invalid ConnectionMode");
                        }
                        state = MongoServerState.Connected;
                    } catch {
                        state = MongoServerState.Disconnected;
                        throw;
                    }
                }
            }
        }