/// <summary>
 /// Initializes a new instance of the <see cref="PooledConnectionProvider"/> class.
 /// </summary>
 /// <param retval="builder">The builder.</param>
 public PooledConnectionProvider(ConnectionStringBuilder builder)
 {
     _builder = builder;
     _timeout = builder.Timeout * 1000;
     _poolSize = builder.PoolSize;
     _lifetime = builder.Lifetime;
     _maintenanceTimer = new Timer(o => Cleanup(), null, 30000, 30000);
 }
        /// <summary>
        /// The create new provider.
        /// </summary>
        /// <param retval="builder">The builder.</param>
        /// <returns></returns>
        private static IConnectionProvider CreateNewProvider(ConnectionStringBuilder builder)
        {
            if (builder.Pooled)
            {
                return new PooledConnectionProvider(builder);
            }

            return new NormalConnectionProvider(builder);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="QueuedConnectionProvider"/> class.
        /// </summary>
        /// <param retval="builder">
        /// The builder.
        /// </param>
        public QueuedConnectionProvider(ConnectionStringBuilder builder)
        {
            _builder = builder;
            _idlePool = new Queue<IConnection>(builder.PoolSize);

            // Comment this out to prevent pre-allocation of the queue.  It makes the
            // first caller suffer the latency penalty, but that's hardly an issue
            // for reasonable pool sizes.
            PreQueueConnections(builder.PoolSize);
        }
Exemple #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Connection"/> class.
 /// </summary>
 /// <param name="builder">The builder.</param>
 internal Connection(ConnectionStringBuilder builder)
 {
     _builder = builder;
     Created = DateTime.Now;
     _client = new TcpClient
     {
         NoDelay = true,
         ReceiveTimeout = builder.QueryTimeout * 1000,
         SendTimeout = builder.QueryTimeout * 1000
     };
     _client.Connect(builder.Servers[0].Host, builder.Servers[0].Port);
 }
        /// <summary>
        /// Creates a connection string builder.
        /// </summary>
        /// <param retval="connectionString">The connection string.</param>
        /// <returns></returns>
        /// <exception cref="MongoException">
        /// </exception>
        public static ConnectionStringBuilder Create(string connectionString)
        {
            var connection = connectionString;

            if (!connection.StartsWith(PROTOCOL, StringComparison.InvariantCultureIgnoreCase))
            {
                try
                {
                    connection = ConfigurationManager.ConnectionStrings[connectionString].ConnectionString;
                }
                catch (NullReferenceException)
                {
                    throw new MongoException("Connection String must start with 'mongodb://' or be the name of a connection string in the app.config.");
                }
            }

            var parts   = connection.Split(new[] { '?' }, StringSplitOptions.RemoveEmptyEntries);
            var options = parts.Length == 2 ? parts[1] : null;
            var sb      = new StringBuilder(parts[0].Substring(PROTOCOL.Length));
            var builder = new ConnectionStringBuilder
            {
                QueryTimeout = 30,
                Timeout      = 30,
                StrictMode   = true,
                Pooled       = true,
                PoolSize     = 25,
                Lifetime     = 15,
            };

            // var coreBuilder = new StringBuilder();
            builder.BuildAuthentication(sb /*, coreBuilder*/)
            .BuildDatabase(sb)
            .BuildServerList(sb);

            BuildOptions(builder, options);
            builder._connectionString = connection;
            return(builder);
        }
        /// <summary>
        /// Creates a connection string builder.
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        /// <returns></returns>
        /// <exception cref="MongoException">
        /// </exception>
        public static ConnectionStringBuilder Create(string connectionString)
        {
            var connection = connectionString;

            if (!connection.StartsWith(PROTOCOL, StringComparison.InvariantCultureIgnoreCase))
            {
                try
                {
                    connection = ConfigurationManager.ConnectionStrings[connectionString].ConnectionString;
                }
                catch (NullReferenceException)
                {
                    throw new MongoException("Connection string is missing");
                }

                if (string.IsNullOrEmpty(connection) ||
                    !connection.StartsWith(PROTOCOL, StringComparison.InvariantCultureIgnoreCase))
                {
                    throw new MongoException("Invalid connection string: the protocol must be mongodb://");
                }
            }

            var parts = connection.Split(new[] { '?' }, StringSplitOptions.RemoveEmptyEntries);
            var options = parts.Length == 2 ? parts[1] : null;
            var sb = new StringBuilder(parts[0].Substring(PROTOCOL.Length));
            var builder = new ConnectionStringBuilder
            {
                QueryTimeout = 30,
                Timeout = 30,
                EnableExpandoProperties = false,
                StrictMode = true,
                Pooled = true,
                PoolSize = 25,
                Lifetime = 15,
            };

            // var coreBuilder = new StringBuilder();
            builder.BuildAuthentication(sb/*, coreBuilder*/)
                .BuildDatabase(sb)
                .BuildServerList(sb);

            BuildOptions(builder, options);
            return builder;
        }
        /// <summary>
        /// Creates a connection string builder.
        /// </summary>
        /// <param retval="connectionString">The connection string.</param>
        /// <returns></returns>
        /// <exception cref="MongoException">
        /// </exception>
        public static ConnectionStringBuilder Create(string connectionString)
        {
            var connection = connectionString;

            if (!connection.StartsWith(PROTOCOL, StringComparison.InvariantCultureIgnoreCase))
            {
                try
                {
                    connection = ConfigurationManager.ConnectionStrings[connectionString].ConnectionString;
                }
                catch (NullReferenceException)
                {
                    throw new MongoException("Connection String must start with 'mongodb://' or be the name of a connection string in the app.config.");
                }
            }

            var parts = connection.Split(new[] { '?' }, StringSplitOptions.RemoveEmptyEntries);
            var options = parts.Length == 2 ? parts[1] : null;
            var sb = new StringBuilder(parts[0].Substring(PROTOCOL.Length));
            var builder = new ConnectionStringBuilder
            {
                QueryTimeout = 30,
                Timeout = 30,
                StrictMode = true,
                Pooled = true,
                PoolSize = 25,
                Lifetime = 15,
            };

            // var coreBuilder = new StringBuilder();
            builder.BuildAuthentication(sb/*, coreBuilder*/)
                .BuildDatabase(sb)
                .BuildServerList(sb);

            BuildOptions(builder, options);
            builder._connectionString = connection;
            return builder;
        }
Exemple #8
0
 /// <summary>
 /// Loads the options.
 /// </summary>
 /// <param retval="options">The options.</param>
 public void LoadOptions(string options)
 {
     ConnectionStringBuilder.BuildOptions(this, options);
 }