コード例 #1
0
 public ConnectionStringPool(string connectionString, ConnectionPool pool)
 {
     ConnectionString = connectionString;
     Pool             = pool;
 }
コード例 #2
0
        public static ConnectionPool?GetPool(string connectionString)
        {
            // check single-entry MRU cache for this exact connection string; most applications have just one
            // connection string and will get a cache hit here
            var cache = s_mruCache;

            if (cache?.ConnectionString == connectionString)
            {
                return(cache.Pool);
            }

            // check if pool has already been created for this exact connection string
            if (s_pools.TryGetValue(connectionString, out var pool))
            {
                s_mruCache = new(connectionString, pool);
                return(pool);
            }

            // parse connection string and check for 'Pooling' setting; return 'null' if pooling is disabled
            var connectionStringBuilder = new MySqlConnectionStringBuilder(connectionString);

            if (!connectionStringBuilder.Pooling)
            {
                s_pools.GetOrAdd(connectionString, default(ConnectionPool));
                s_mruCache = new(connectionString, null);
                return(null);
            }

            // check for pool using normalized form of connection string
            var normalizedConnectionString = connectionStringBuilder.ConnectionString;

            if (normalizedConnectionString != connectionString && s_pools.TryGetValue(normalizedConnectionString, out pool))
            {
                // try to set the pool for the connection string to the canonical pool; if someone else
                // beats us to it, just use the existing value
                pool       = s_pools.GetOrAdd(connectionString, pool) !;
                s_mruCache = new(connectionString, pool);
                return(pool);
            }

            // create a new pool and attempt to insert it; if someone else beats us to it, just use their value
            var connectionSettings = new ConnectionSettings(connectionStringBuilder);
            var newPool            = new ConnectionPool(connectionSettings);

            pool = s_pools.GetOrAdd(normalizedConnectionString, newPool);

            if (pool == newPool)
            {
                s_mruCache = new(connectionString, pool);
                pool.StartReaperTask();

                // if we won the race to create the new pool, also store it under the original connection string
                if (connectionString != normalizedConnectionString)
                {
                    s_pools.GetOrAdd(connectionString, pool);
                }
            }
            else if (pool != newPool && Log.IsInfoEnabled())
            {
                Log.Info("Pool{0} was created but will not be used (due to race)", newPool.m_logArguments);
            }

            return(pool);
        }
コード例 #3
0
 public LeastConnectionsLoadBalancer(ConnectionPool pool) => m_pool = pool;