Ejemplo n.º 1
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.º 2
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.º 3
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;
                        }
                    }
                }
            }
        }