Exemplo n.º 1
0
        /// <summary>
        /// 注册依赖
        /// </summary>
        /// <param name="container"></param>
        /// <param name="store"></param>
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            var localConfigResolver = IocManager.Instance.Resolve <IConfigResolver>();
            var config = localConfigResolver.Get <DbConfig>();

            if (config.Items.Count == 0)
            {
                return;
            }
            config.Items.ForEach(m =>
            {
                // 注册Db连接
                var dbConnstring = AbsDbProvider.CreateInstance(m.DataType, m.DataVer).CreateDbConnstring(m.Server, m.Port, m.UserID, m.PassWord, m.Catalog, m.DataVer, m.Additional, m.ConnectTimeout, m.PoolMinSize, m.PoolMaxSize);
                container.Register(
                    Component.For <IContextConnection>()
                    .Named(m.Name)
                    .ImplementedBy <ContextConnection>()
                    .DependsOn(
                        Dependency.OnValue(dbConnstring.GetType(), dbConnstring),
                        Dependency.OnValue(m.DataType.GetType(), m.DataType),
                        Dependency.OnValue(m.CommandTimeout.GetType(), m.CommandTimeout),
                        Dependency.OnValue(m.DataType.GetType(), m.DataType)).LifestyleSingleton());
            });

            // 代理异常记录
            if (config.IsWriteSqlErrorLog)
            {
                container.Register(Component.For <ISqlMonitor>().ImplementedBy <MonitorSqlExceptionLog>().LifestyleTransient());
            }
            // 代理SQL记录
            if (config.IsWriteSqlRunLog)
            {
                container.Register(Component.For <ISqlMonitor>().ImplementedBy <MonitorSqlLog>().LifestyleTransient());
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///     初始化数据库环境、实例化子类中,所有Set属性
        /// </summary>
        public void Initializer()
        {
            if (IsInitializer)
            {
                return;
            }

            // 数据库提供者
            DbProvider = AbsDbProvider.CreateInstance(_contextConnection.DbType, _contextConnection.DataVer);
            // 默认SQL执行者
            Executeor = new ExecuteSql(new DbExecutor(_contextConnection.ConnectionString, _contextConnection.DbType, _contextConnection.CommandTimeout, IsMergeCommand && DbProvider.IsSupportTransaction ? IsolationLevel.Serializable : IsolationLevel.Unspecified), this);
            // 代理SQL记录
            if (SystemConfigs.ConfigEntity.IsWriteDbLog)
            {
                Executeor = new ExecuteSqlLogProxy(Executeor);
            }
            // 代理异常记录
            if (SystemConfigs.ConfigEntity.IsWriteDbExceptionLog)
            {
                Executeor = new ExecuteSqlExceptionLogProxy(Executeor);
            }

            // 队列管理者
            QueueManger = new QueueManger(this);
            // 手动编写SQL
            ManualSql = new ManualSql(this);
            // 上下文映射关系
            this.ContextMap = new ContextDataMap(ContextType);

            IsInitializer = true;
        }
Exemplo n.º 3
0
        /// <summary>
        ///     初始化数据库环境、实例化子类中,所有Set属性
        /// </summary>
        public void Initializer()
        {
            if (IsInitializer)
            {
                return;
            }

            // 数据库提供者
            DbProvider = AbsDbProvider.CreateInstance(ContextConnection.DbType, ContextConnection.DataVer);

            var dbconfig = IocManager.Instance.Resolve <IConfigResolver>().DbConfig();

            // 默认SQL执行者
            Executeor = new ExecuteSql(new DbExecutor(ContextConnection.ConnectionString, ContextConnection.DbType, ContextConnection.CommandTimeout, !IsUnitOfWork && DbProvider.IsSupportTransaction ? IsolationLevel.RepeatableRead : IsolationLevel.Unspecified), this);

            // 代理SQL监控
            if (IocManager.Instance.IsRegistered <ISqlMonitor>())
            {
                Executeor = new ExecuteSqlMonitorProxy(Executeor);
            }

            // 队列管理者
            QueueManger = new QueueManger(this);
            // 手动编写SQL
            ManualSql = new ManualSql(this);
            // 上下文映射关系
            this.ContextMap = new ContextDataMap(ContextType);

            IsInitializer = true;
        }
Exemplo n.º 4
0
        /// <summary>
        /// 实例化子类中,所有Set属性
        /// </summary>
        private void InstanceProperty()
        {
            IsMergeCommand = true;
            DbProvider     = AbsDbProvider.CreateInstance(DataBase.DataType);
            QueueManger    = new QueueManger(this);
            Executeor      = SystemConfigs.ConfigEntity.IsWriteDbLog ? new ExecuteSqlProxy(this) : new ExecuteSql(this);

            InstanceProperty(this, "TableSet`1");
            InstanceProperty(this, "TableSetCache`1");
            InstanceProperty(this, "ViewSet`1");
            InstanceProperty(this, "ViewSetCache`1");
            InstanceProperty(this, "ProcSet`1");
        }
        protected override string SetCacheLock()
        {
            lock (LockObject)
            {
                if (CacheList.ContainsKey(Key))
                {
                    return(CacheList[Key]);
                }

                DbInfo dbInfo = Key;
                CacheList.Add(Key, AbsDbProvider.CreateInstance(dbInfo.DataType, dbInfo.DataVer).CreateDbConnstring(dbInfo.UserID, dbInfo.PassWord, dbInfo.Server, dbInfo.Catalog, dbInfo.DataVer, dbInfo.ConnectTimeout, dbInfo.PoolMinSize, dbInfo.PoolMaxSize, dbInfo.Port));
            }

            return(CacheList[Key]);
        }
Exemplo n.º 6
0
        /// <summary>
        ///     获取数据库连接对象
        /// </summary>
        /// <param name="dbType">数据库类型</param>
        /// <param name="connectionString">连接字符串</param>
        public static DbConnection GetDbConnection(eumDbType dbType, string connectionString)
        {
            DbConnection conn;

            switch (dbType)
            {
            case eumDbType.SqlServer:
                conn = new SqlConnection(connectionString);
                break;

            default:
                conn = AbsDbProvider.CreateInstance(dbType).DbProviderFactory.CreateConnection();
                break;
            }
            conn.ConnectionString = connectionString;
            return(conn);
        }
Exemplo n.º 7
0
        /// <summary>
        ///     打开数据库连接
        /// </summary>
        private void Open()
        {
            if (_conn == null)
            {
                _factory             = AbsDbProvider.CreateInstance(DataType).GetDbProviderFactory;
                _comm                = _factory.CreateCommand();
                _comm.CommandTimeout = _commandTimeout;

                _conn = _factory.CreateConnection();
                _conn.ConnectionString = _connectionString;
                _comm.Connection       = _conn;
            }
            if (_conn.State == ConnectionState.Closed)
            {
                _conn.Open();
                _comm.Parameters.Clear();
            }
        }
Exemplo n.º 8
0
        /// <summary>
        ///     创建数据库连接字符串
        /// </summary>
        /// <param name="dbIndex">数据库配置</param>
        public static string CreateConnString(int dbIndex = 0)
        {
            if (ConnList.ContainsKey(dbIndex))
            {
                return(ConnList[dbIndex]);
            }
            lock (LockObject)
            {
                if (ConnList.ContainsKey(dbIndex))
                {
                    return(ConnList[dbIndex]);
                }

                DbInfo dbInfo = dbIndex;
                ConnList.Add(dbIndex, AbsDbProvider.CreateInstance(dbInfo.DataType).CreateDbConnstring(dbInfo.UserID, dbInfo.PassWord, dbInfo.Server, dbInfo.Catalog, dbInfo.DataVer, dbInfo.ConnectTimeout, dbInfo.PoolMinSize, dbInfo.PoolMaxSize, dbInfo.Port));
            }

            return(ConnList[dbIndex]);
        }
Exemplo n.º 9
0
        protected override string SetCacheLock()
        {
            lock (LockObject)
            {
                if (CacheList.ContainsKey(Key))
                {
                    return(CacheList[Key]);
                }

                DbInfo dbInfo = Key;
                if (dbInfo == null)
                {
                    Log.LogManger.Log.Error("未设置数据库配置文件"); return(null);
                }
                CacheList.Add(Key, AbsDbProvider.CreateInstance(dbInfo.DataType, dbInfo.DataVer).CreateDbConnstring(dbInfo.Server, dbInfo.Port, dbInfo.UserID, dbInfo.PassWord, dbInfo.Catalog, dbInfo.DataVer, dbInfo.Additional, dbInfo.ConnectTimeout, dbInfo.PoolMinSize, dbInfo.PoolMaxSize));
            }

            return(CacheList[Key]);
        }
Exemplo n.º 10
0
        /// <summary>
        ///     打开数据库连接
        /// </summary>
        private void Open()
        {
            if (_conn == null)
            {
                _factory             = AbsDbProvider.CreateInstance(DataType).DbProviderFactory;
                _comm                = _factory.CreateCommand();
                _comm.CommandTimeout = _commandTimeout;

                _conn = _factory.CreateConnection();
                _conn.ConnectionString = _connectionString;
                _comm.Connection       = _conn;
            }
            if (_conn.State == ConnectionState.Closed)
            {
                _conn.Open();
                _comm.Parameters.Clear();
                // 是否开启事务
                if (IsTransaction)
                {
                    _comm.Transaction = _conn.BeginTransaction(TranLevel);
                }
            }
        }
Exemplo n.º 11
0
        /// <summary>
        ///     打开数据库连接
        /// </summary>
        private void Open()
        {
            if (_comm == null || _comm.Connection == null)
            {
                _factory = AbsDbProvider.CreateInstance(DataType).DbProviderFactory;
                _comm    = _factory.CreateCommand();
                // ReSharper disable once PossibleNullReferenceException
                _comm.Connection = _factory.CreateConnection();
                _comm.Connection.ConnectionString = _connectionString;
                _comm.CommandTimeout = _commandTimeout;
            }

            if (_comm.Connection.State == ConnectionState.Closed)
            {
                _comm.Parameters.Clear();
                _comm.Connection.Open();

                // 是否开启事务
                if (IsTransaction)
                {
                    _comm.Transaction = _comm.Connection.BeginTransaction(TranLevel);
                }
            }
        }