/// <summary>
        /// 设置连接字符串
        /// </summary>
        /// <param name="operationType"></param>
        /// <returns></returns>
        internal string SetConnectionString(OperationType operationType)
        {
            //写
            if (operationType == OperationType.Write)
            {
                CurrentConnectionString = ConnectionString_Write;
                return(CurrentConnectionString);
            }

            //读
            //先校验下次执行的连接字符串
            if (!string.IsNullOrEmpty(NextConnectionString))
            {
                CurrentConnectionString = NextConnectionString;
                NextConnectionString    = string.Empty;
                return(CurrentConnectionString);
            }

            if (ConnectionStrings_Read == null || !ConnectionStrings_Read.Any())
            {
                CurrentConnectionString = ConnectionString_Write;
            }
            else if (ConnectionStrings_Read.Length == 1)
            {
                CurrentConnectionString = ConnectionStrings_Read[0];
            }
            else
            {
                if (connectionStatuses == null)
                {
                    throw new NullReferenceException("Connection status list is null,please call Init() first!");
                }

                //根据策略选取对应的连接字符串
                switch (ConnectionLoadBalanceStrategy)
                {
                case LoadBalanceStrategy.RoundRobin:
                    CurrentConnectionString = GetByRoundRobin();
                    break;

                case LoadBalanceStrategy.LeastConnection:
                    CurrentConnectionString = LeastConnection();
                    break;

                default:
                    CurrentConnectionString = LeastConnection();
                    break;
                }
            }
            return(CurrentConnectionString);
        }
예제 #2
0
        /// <summary>
        /// set ConnectionString currently be used
        /// </summary>
        /// <param name="operationType"></param>
        internal void SetConnectionString(OperationType operationType)
        {
            //first,Verify the next connection string
            if (!string.IsNullOrEmpty(NextConnectionString))
            {
                CurrentConnectionString = NextConnectionString;
                NextConnectionString    = string.Empty;
                return;
            }

            //write operation
            if (operationType == OperationType.Write)
            {
                CurrentConnectionString = ConnectionString_Write;
                return;
            }

            //read operation
            if (ConnectionStrings_Read == null || !ConnectionStrings_Read.Any())
            {
                CurrentConnectionString = ConnectionString_Write;
            }
            else if (ConnectionStrings_Read.Length == 1)
            {
                CurrentConnectionString = ConnectionStrings_Read[0];
            }
            else
            {
                if (connectionStatuses == null)
                {
                    throw new NullReferenceException("Connection status list is null,please call Init() first!");
                }

                switch (ConnectionLoadBalanceStrategy)
                {
                case LoadBalanceStrategy.RoundRobin:
                    CurrentConnectionString = GetByRoundRobin();
                    break;

                case LoadBalanceStrategy.LeastConnection:
                    CurrentConnectionString = LeastConnection();
                    break;

                default:
                    CurrentConnectionString = LeastConnection();
                    break;
                }
            }
        }
        internal ConnectionManager(string connectionString_Write, string[] connectionStrings_Read)
        {
            this.ConnectionString_Write = connectionString_Write;
            this.ConnectionStrings_Read = connectionStrings_Read?.Distinct()?.ToArray();
            //默认当前连接字符串是写
            this.CurrentConnectionString = connectionString_Write;

            //初始化
            //如果写字符串不需要负载策略,则直接返回
            if (ConnectionStrings_Read == null || !ConnectionStrings_Read.Any() || ConnectionStrings_Read.Length == 1)
                return;

            connectionStatuses = new List<ConnectionStatus>();

            //初始化连接使用情况集合
            ConnectionStrings_Read.Distinct().ToArray().Foreach(item => connectionStatuses.Add(new ConnectionStatus { HashKey = item.GetHashCode(), ConnectionString = item, Count = 0 }));

        }
예제 #4
0
        /// <summary>
        /// constructed function
        /// </summary>
        /// <param name="connectionString_Write">the master Database ConnectionString (Only one)</param>
        /// <param name="connectionStrings_Read">the slave Database ConnectionStrings</param>
        internal ConnectionManager(string connectionString_Write, string[] connectionStrings_Read)
        {
            this.ConnectionString_Write = connectionString_Write.ToFormativeConnectionString();
            this.ConnectionStrings_Read = connectionStrings_Read?.Select(c => c.ToFormativeConnectionString()).Distinct()?.ToArray();

            this.CurrentConnectionString = this.ConnectionString_Write;

            //don't need Load Balance Strategy
            if (ConnectionStrings_Read == null || !ConnectionStrings_Read.Any() || ConnectionStrings_Read.Length == 1)
            {
                return;
            }

            this.connectionStatuses = new List <ConnectionStatus>();

            //Initialize connection usage collection
            ConnectionStrings_Read.Distinct().ToArray().Foreach(item => connectionStatuses.Add(new ConnectionStatus {
                HashKey = item.GetHashCode(), ConnectionString = item, Count = 0
            }));
        }