示例#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);
                }
            }
        }
示例#2
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;
                }
            }
        }
示例#3
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);
        }