Ejemplo n.º 1
0
        protected virtual void PreLoadExecute(IDictionary <string, IStorageContext> storageContexts)
        {
            foreach (KeyValuePair <string, IStorageContext> context in storageContexts)
            {
                IStorageContext storageContext = context.Value;
                string          sConnection    = StorageParser.BuildConnectionString(storageContext.Storage);
                storageContext.Connection =
                    storageContext.Storage.Pooling
                        ?
                    DbConnectionPoolManager.GetConnection(storageContext.StorageName, sConnection)
                        :
                    DatabaseFactory.GetDbConnection(storageContext.Storage.DatabaseStyle, sConnection);

                try
                {
                    if (ConnectionState.Open != storageContext.Connection.State)
                    {
                        storageContext.Connection.Open();
                    }
                }
                catch (Exception exc)
                {
                    IStorageAttribute storageAttr = (IStorageAttribute)StorageCache.Get(storageContext.StorageName);
                    storageAttr.IsHealth = false;
                    UnhealthyStorage.Add(storageAttr.Name);
                    if (null != Logger)
                    {
                        Logger.WarnFormat("Storage:{0} can not open.Set the health is false and it not used until the health set true.", storageAttr.Name);
                    }
                    IConnectionNotify notify = AlbianServiceRouter.GetService <IConnectionNotify>();
                    if (null != notify)
                    {
                        Logger.Info("send message when open database is error.");
                        string msg = string.Format("Server:{0},Database:{1},Exception Message:{2}.", storageContext.Storage.Server, storageContext.Storage.Database, exc.Message);
                        notify.SendMessage(msg);
                    }
                    throw exc;
                }
                if (storageContext.Storage.Transactional)
                {
                    storageContext.Transaction =
                        storageContext.Connection.BeginTransaction(IsolationLevel.ReadUncommitted);
                }
                foreach (IFakeCommandAttribute fc in storageContext.FakeCommand)
                {
                    IDbCommand cmd = storageContext.Connection.CreateCommand();
                    cmd.CommandText = fc.CommandText;
                    cmd.CommandType = CommandType.Text;
                    if (storageContext.Storage.Transactional)
                    {
                        cmd.Transaction = storageContext.Transaction;
                    }
                    foreach (DbParameter para in fc.Paras)
                    {
                        cmd.Parameters.Add(para);
                    }
                    storageContext.Command.Add(cmd);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 释放资源
        /// </summary>
        public static void Release()
        {
            lock (_dicDBAccessLock)
            {
                foreach (var dbAccess in _dbAccessDic.Values)
                {
                    dbAccess.Dispose();
                }
            }

            DbConnectionPoolManager.ReleaseDbConnection();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 移除数据库实例
        /// </summary>
        /// <param name="dbid">数据库编号ID</param>
        /// <returns>已移除的数据库访问实例</returns>
        public static IDBAccess RemoveDBAccessInstance(int dbid)
        {
            IDBAccess dbAccess;

            if (_dbAccessDic.TryRemove(dbid, out dbAccess))
            {
                dbAccess.Dispose();
                DbConnectionPoolManager.RemoveDbConnectionPool(dbid);
            }

            return(dbAccess);
        }
Ejemplo n.º 4
0
        public bool Test(string storageName)
        {
            IStorageAttribute storageAttr = (IStorageAttribute)StorageCache.Get(storageName);
            string            sConnection = StorageParser.BuildConnectionString(storageAttr);
            IDbConnection     conn        =
                storageAttr.Pooling
                    ?
                DbConnectionPoolManager.GetConnection(storageName, sConnection)
                    :
                DatabaseFactory.GetDbConnection(storageAttr.DatabaseStyle, sConnection);

            try
            {
                if (ConnectionState.Open != conn.State)
                {
                    conn.Open();
                }
                IDbCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT 1 AS Row";
                cmd.CommandType = CommandType.Text;
                object oValue = cmd.ExecuteScalar();
                int    value;
                if (int.TryParse(oValue.ToString(), out value) && 1 == value)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception exc)
            {
                return(false);
            }
            finally
            {
                if (ConnectionState.Closed != conn.State)
                {
                    conn.Close();
                }
                if (storageAttr.Pooling)
                {
                    DbConnectionPoolManager.RetutnConnection(storageAttr.Name, conn);
                }
                else
                {
                    conn.Dispose();
                    conn = null;
                }
            }
        }
Ejemplo n.º 5
0
        protected virtual void UnLoadExecute(IDictionary <string, IStorageContext> storageContexts)
        {
            foreach (KeyValuePair <string, IStorageContext> context in storageContexts)
            {
                IStorageContext storageContext = context.Value;
                try
                {
                    if (null != context.Value.Command)
                    {
                        foreach (IDbCommand cmd in context.Value.Command)
                        {
                            cmd.Parameters.Clear();
                            cmd.Dispose();
                        }
                    }
                    if (storageContext.Storage.Transactional && null != storageContext.Transaction)
                    {
                        storageContext.Transaction.Dispose();
                    }
                    storageContext.Transaction = null;
                    storageContext.FakeCommand = null;

                    if (null != storageContext.Connection &&
                        ConnectionState.Closed != storageContext.Connection.State)
                    {
                        storageContext.Connection.Close();

                        if (storageContext.Storage.Pooling)
                        {
                            DbConnectionPoolManager.RetutnConnection(storageContext.StorageName,
                                                                     storageContext.Connection);
                        }
                        else
                        {
                            storageContext.Connection.Dispose();
                        }
                        storageContext.Connection = null;
                    }
                }
                catch
                {
                    if (null != Logger)
                    {
                        Logger.Warn("Clear the database resources is error.but must close the all connections");
                    }
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbInteraction">数据库交互对象</param>
        /// <param name="config">数据库配置</param>
        /// <param name="databaseTypeName">数据库类型名称</param>
        /// <param name="sqlMaxLength">sql语句最大长度</param>
        public DBAccessAbs(IDBInteraction dbInteraction, DatabaseConfig config, string databaseTypeName, long sqlMaxLength)
        {
            this._dbid             = config.DBID;
            this._config           = config;
            this._dbInteraction    = dbInteraction;
            this._databaseTypeName = databaseTypeName;
            if (config.SqlMaxLength == DBConstant.SqlMaxLength)
            {
                this._sqlMaxLength = sqlMaxLength;
            }
            else
            {
                this._sqlMaxLength = config.SqlMaxLength;
            }

            DbConnectionPoolManager.AddDbConnectionPool(config, dbInteraction);
        }
Ejemplo n.º 7
0
        protected IStorageContext PreExecute(ITask task)
        {
            IStorageContext[] storageContexts = new IStorageContext[task.Context.Values.Count];
            task.Context.Values.CopyTo(storageContexts, 0);
            //task.Context.
            //IStorageContext storageContext = task.Context.
            IStorageContext storageContext = storageContexts[0];

            string sConnection = StorageParser.BuildConnectionString(storageContext.Storage);

            storageContext.Connection =
                storageContext.Storage.Pooling
                    ?
                DbConnectionPoolManager.GetConnection(storageContext.StorageName, sConnection)
                    :
                DatabaseFactory.GetDbConnection(storageContext.Storage.DatabaseStyle, sConnection);
            return(storageContext);
        }
Ejemplo n.º 8
0
        public IList <T> QueryObjects <T>(ITask task)
            where T : class, IAlbianObject, new()
        {
            Hashtable reader;
            IDictionary <string, IMemberAttribute> members;
            IList <T>   objects = new List <T>();
            IDataReader dr      = Execute(task);

            try
            {
                PropertyInfo[] properties = AfterExecute <T>(dr, out reader, out members);
                while (dr.Read())
                {
                    T target = AlbianObjectCreater <T>(properties, dr, reader, members);
                    objects.Add(target);
                }
                return(objects);
            }
            finally
            {
                dr.Close();
                dr.Dispose();
                foreach (KeyValuePair <string, IStorageContext> kv in task.Context)
                {
                    if (null != kv.Value.Connection)
                    {
                        if (ConnectionState.Closed != kv.Value.Connection.State)
                        {
                            kv.Value.Connection.Close();
                        }
                        if (kv.Value.Storage.Pooling)
                        {
                            DbConnectionPoolManager.RetutnConnection(kv.Value.StorageName, kv.Value.Connection);
                        }
                        else
                        {
                            //kv.Value.StorageName,
                            kv.Value.Connection.Dispose();
                            //conn = null;
                        }
                    }
                }
            }
        }
Ejemplo n.º 9
0
        public void Init(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath");
            }
            try
            {
                XmlDocument doc   = XmlFileParser.LoadXml(filePath);
                XmlNodeList nodes = XmlFileParser.Analyze(doc, "Storages");
                if (1 != nodes.Count) //root node
                {
                    throw new Exception("Analyze the Storages node is error in the Storage.config");
                }

                IDictionary <string, IStorageAttribute> dic = ParserStorages(nodes[0]);
                if (null == dic)
                {
                    if (null != Logger)
                    {
                        Logger.Error("no storage attribute in the config file.");
                    }
                    throw new Exception("no storage attribute in the config file.");
                }
                foreach (KeyValuePair <string, IStorageAttribute> kv in dic)
                {
                    if (kv.Value.Pooling)
                    {
                        DbConnectionPoolManager.CreatePool(kv.Value.Name, kv.Value.DatabaseStyle, kv.Value.MinPoolSize,
                                                           kv.Value.MaxPoolSize);
                    }
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }