Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="connectKey"></param>
        /// <returns></returns>
        public static DbBaseProvider CreateDbProvider(string connectKey)
        {
            DbBaseProvider dbBaseProvider = null;

            if (string.IsNullOrEmpty(connectKey))
            {
                return(dbBaseProvider);
            }
            if (dbProviders.TryGetValue(connectKey, out dbBaseProvider))
            {
                return(dbBaseProvider);
            }

            ConnectionStringSettings connSetting = ConfigurationManager.ConnectionStrings[connectKey];

            if (connSetting != null)
            {
                string connectionString = connSetting.ConnectionString;
                try
                {
                    dbBaseProvider = CreateDbProvider(connSetting.Name, connSetting.ProviderName, connectionString);
                    dbProviders.TryAdd(connectKey, dbBaseProvider);
                }
                catch
                {
                    TraceLog.WriteError("ProviderName:{0} instance failed.", connSetting.ProviderName);
                }
            }

            return(dbBaseProvider);
        }
Пример #2
0
        /// <summary>
        /// 初始化DB连接
        /// </summary>
        public static void Initialize()
        {
            DbBaseProvider dbBaseProvider = null;
            var            er             = ConfigurationManager.ConnectionStrings.GetEnumerator();

            while (er.MoveNext())
            {
                ConnectionStringSettings connSetting = er.Current as ConnectionStringSettings;
                if (connSetting == null)
                {
                    continue;
                }
                var setting = ConnectionSetting.Create(connSetting.Name, connSetting.ProviderName, connSetting.ConnectionString.Trim());
                if (setting.DbLevel == DbLevel.LocalMysql || setting.DbLevel == DbLevel.LocalSql)
                {
                    continue;
                }
                dbBaseProvider = CreateDbProvider(setting);
                try
                {
                    dbBaseProvider.CheckConnect();
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("Not connect to the database server \"{0}\" database \"{1}\".", dbBaseProvider.ConnectionSetting.DataSource, dbBaseProvider.ConnectionSetting.DatabaseName), ex);
                }
                dbProviders.TryAdd(connSetting.Name, dbBaseProvider);
            }
        }
Пример #3
0
 private static void DoProcessSqlSyncQueue(string workingKey, byte[][] bufferBytes)
 {
     try
     {
         bool hasClear = false;
         foreach (var buffer in bufferBytes)
         {
             DbBaseProvider dbProvider = null;
             SqlStatement   statement  = null;
             try
             {
                 statement  = ProtoBufUtils.Deserialize <SqlStatement>(buffer);
                 dbProvider = DbConnectionProvider.CreateDbProvider("", statement.ProviderType, statement.ConnectionString);
                 var paramList = ToSqlParameter(dbProvider, statement.Params);
                 dbProvider.ExecuteQuery(statement.CommandType, statement.CommandText, paramList);
             }
             catch (Exception e)
             {
                 TraceLog.WriteSqlError("Error:{0}\r\nSql>>\r\n{1}", e, statement != null ? statement.CommandText : "");
                 PutError(buffer);
                 if (!hasClear && dbProvider != null)
                 {
                     //modify error: 40 - Could not open a connection to SQL Server
                     hasClear = true;
                     dbProvider.ClearAllPools();
                 }
             }
         }
     }
     catch (Exception ex)
     {
         TraceLog.WriteError("DoProcessSqlSyncQueue error:{0}", ex);
     }
 }
Пример #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="connectKey"></param>
        /// <returns></returns>
        public static DbBaseProvider CreateDbProvider(string connectKey)
        {
            DbBaseProvider dbBaseProvider = null;

            if (string.IsNullOrEmpty(connectKey))
            {
                return(dbBaseProvider);
            }
            if (dbProviders.TryGetValue(connectKey, out dbBaseProvider))
            {
                return(dbBaseProvider);
            }
            var connSection = ConfigManager.Configger.GetConnetion <ConnectionSection>(connectKey);

            if (connSection != null)
            {
                string connectionString = connSection.ConnectionString;
                try
                {
                    dbBaseProvider = CreateDbProvider(connSection.Name, connSection.ProviderName, connectionString);
                    dbProviders.TryAdd(connectKey, dbBaseProvider);
                }
                catch
                {
                    TraceLog.WriteError("ProviderName:{0} instance failed.", connSection.ProviderName);
                }
            }

            return(dbBaseProvider);
        }
Пример #5
0
 private static IDataParameter[] ToSqlParameter(DbBaseProvider dbProvider, SqlParam[] paramList)
 {
     IDataParameter[] list = new IDataParameter[paramList.Length];
     for (int i = 0; i < paramList.Length; i++)
     {
         SqlParam param = paramList[i];
         list[i] = dbProvider.CreateParameter(param.ParamName, param.DbTypeValue, param.Size, param.Value.Value);
     }
     return(list);
 }
Пример #6
0
        /// <summary>
        /// 初始化DB连接
        /// </summary>
        public static void Initialize()
        {
            DbBaseProvider dbBaseProvider = null;
            var            er             = ConfigurationManager.ConnectionStrings.GetEnumerator();

            while (er.MoveNext())
            {
                ConnectionStringSettings connSetting = er.Current as ConnectionStringSettings;
                if (connSetting != null)
                {
                    try
                    {
                        dbBaseProvider = CreateDbProvider(connSetting.Name, connSetting.ProviderName, connSetting.ConnectionString.Trim());
                        dbProviders.TryAdd(connSetting.Name, dbBaseProvider);
                    }
                    catch
                    {
                        TraceLog.WriteError("ProviderName:{0} instance failed.", connSetting.ProviderName);
                    }
                }
            }
        }
Пример #7
0
        private static void DoProcessSqlSyncQueue(string workingKey, byte[][] bufferBytes)
        {
            try
            {
                bool hasClear = false;
                foreach (var buffer in bufferBytes)
                {
                    DbBaseProvider dbProvider = null;
                    SqlStatement   statement  = null;
                    int            result     = 0;
                    try
                    {
                        statement  = ProtoBufUtils.Deserialize <SqlStatement>(buffer);
                        dbProvider = DbConnectionProvider.CreateDbProvider("", statement.ProviderType,
                                                                           statement.ConnectionString);
                        var paramList = ToSqlParameter(dbProvider, statement.Params);
                        result = dbProvider.ExecuteQuery(statement.CommandType, statement.CommandText, paramList);
                    }
                    catch (DbConnectionException connError)
                    {
                        TraceLog.WriteSqlError("SqlSync Error:{0}\r\nSql>>\r\n{1}", connError,
                                               statement != null ? statement.ToString() : "");
                        if (dbProvider != null)
                        {
                            //modify error: 40 - Could not open a connection to SQL Server
                            dbProvider.ClearAllPools();

                            //resend
                            var paramList = ToSqlParameter(dbProvider, statement.Params);
                            result = dbProvider.ExecuteQuery(statement.CommandType, statement.CommandText, paramList);
                        }
                        else
                        {
                            PutError(buffer, SqlSyncConnErrorQueueKey);
                        }
                    }
                    catch (Exception e)
                    {
                        TraceLog.WriteSqlError("SqlSync Error:{0}\r\nSql>>\r\n{1}", e,
                                               statement != null ? statement.ToString() : "");
                        PutError(buffer);
                        if (!hasClear && dbProvider != null)
                        {
                            //modify error: 40 - Could not open a connection to SQL Server
                            hasClear = true;
                            dbProvider.ClearAllPools();
                        }
                    }
                    finally
                    {
                        if (result > 0)
                        {
                            ProfileManager.ProcessSqlOfMessageQueueTimes(statement != null ? statement.Table : null);
                        }
                        else
                        {
                            ProfileManager.ProcessFailSqlOfMessageQueueTimes(statement != null ? statement.Table : null, 1);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("DoProcessSqlSyncQueue error:{0}\r\n", ex.Message, ex.StackTrace);
            }
        }