public bool Create(DbConnectionParams parameters)
        {
            try
            {
                if ((dateTimeHelper.Value.GetDateTimeNow() - lastConnect).TotalSeconds < DbSetting.PERIOD)
                {
                    Thread.Sleep((int)Math.Ceiling(DbSetting.PERIOD - (dateTimeHelper.Value.GetDateTimeNow() - lastConnect).TotalSeconds) * 1000);
                }

                var connectionString = new SqlConnectionStringBuilder()
                {
                    DataSource     = parameters.Source,
                    InitialCatalog = parameters.Database,
                    UserID         = parameters.Login,
                    Password       = parameters.Password
                };

                if (connectionString.LoadBalanceTimeout <= 0)
                {
                    connectionString.LoadBalanceTimeout = parameters.Timeout != null ? (int)parameters.Timeout : DbSetting.TIMEOUT;
                }

                connectionString.ConnectTimeout       = parameters.Timeout != null ? (int)parameters.Timeout : DbSetting.TIMEOUT;
                connectionString.ConnectRetryCount    = parameters.ConnectRetryCount;
                connectionString.ConnectRetryInterval = parameters.ConnectRetryInterval;

                Log.Logger().LogInformation($"Connection has parameters: {Database.Helpers.Message.CreateMessage(connectionString.ToString())}");

                var connect = _provider.Value.Create(connectionString.ToString());

                lastConnect = dateTimeHelper.Value.GetDateTimeNow();

                return(connect);
            }
            catch (ConnectSqlException ex)
            {
                Log.Logger().LogError($"Connection failed.{ex.Message}");
                throw new ConnectSqlException($"Connection failed. {ex.Message}");
            }
            catch (System.Exception ex)
            {
                Log.Logger().LogError($"Connection failed: {ex.Message}");
                throw new ConnectSqlException($"Connection failed: {ex.Message}");
            }
        }
Exemple #2
0
        public bool Create(DbConnectionParams parameters)
        {
            try
            {
                var connectionString = new SqlConnectionStringBuilder()
                {
                    DataSource     = parameters.Source,
                    InitialCatalog = parameters.Database,
                    UserID         = parameters.Login,
                    Password       = parameters.Password
                };

                if (connectionString.LoadBalanceTimeout <= 0)
                {
                    connectionString.LoadBalanceTimeout = parameters.Timeout != null ? (int)parameters.Timeout : DbSetting.TIMEOUT;
                }

                connectionString.ConnectTimeout       = parameters.Timeout != null ? (int)parameters.Timeout : DbSetting.TIMEOUT;
                connectionString.ConnectRetryCount    = parameters.ConnectRetryCount;
                connectionString.ConnectRetryInterval = parameters.ConnectRetryInterval;

                Log.Logger().LogInformation($"Connection has parameters: {Helpers.Message.CreateMessage(connectionString.ToString())}");

                var connect = _provider.Create(connectionString.ToString());

                return(connect);
            }
            catch (ConnectSqlException ex)
            {
                Log.Logger().LogError($"Connection failed.{ex.Message}");
                throw new ConnectSqlException($"Connection failed. {ex.Message}");
            }
            catch (Exception ex)
            {
                Log.Logger().LogError($"Connection failed: {ex.Message}");
                throw new ConnectSqlException($"Connection failed: {ex.Message}");
            }
        }