Mysql Q4M 隊列操作封裝(二)

來源:互聯網
上載者:User

/// <summary>
    /// Q4M隊列操作基類
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// Author:luyifeng
    /// Createday:2013 05 02
    public abstract class MyQ4M<T> : IQ4M<T>
    {
        private bool _isGetData = false;

        private MySqlConnection _conn = null;

        private readonly string _connectionKey = null;

        public T QueueItem { get; set; }

        protected GanjiQ4M(string connectionKey)
        {
            _connectionKey = connectionKey;
        }

        public T First()
        {
            Dispose();

            GetDb();

            QueueItem = GetItem();

            if (QueueItem != null)
            {
                _isGetData = true;
            }

            return QueueItem;
        }

        protected abstract string GetItemSql();

        private T GetItem()
        {
            string strSql = GetItemSql();

            if (string.IsNullOrEmpty(strSql))
            {
                throw new Exception("沒有設定擷取隊列的sql語句");
            }

            var cmd = new MySqlCommand(strSql, _conn);

            using (IDataReader reader = cmd.ExecuteReader())
            {
                if (reader.Read())
                {
                    var que = EntityHelper.TransformPoco<T>(reader);

                    return que;
                }
            }

            return default(T);
        }

        public void Remove()
        {
            //第一條資料出隊
            const string strSql = "select queue_end();";

            ExcuteSql(strSql);

            //狀態恢複
            ClearState();
        }

        public void Rollback()
        {
            //復原資料
            const string strSql = "select queue_abort();";

            ExcuteSql(strSql);

            //狀態恢複
            ClearState();
        }

        private void ExcuteSql(string sql)
        {
            var cmd = new MySqlCommand(sql, _conn);
            cmd.ExecuteNonQuery();
        }

        private void GetDb()
        {
            if (string.IsNullOrEmpty(_connectionKey))
            {
                throw new Exception("沒有配置連接字串key");
            }

            var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[_connectionKey].ConnectionString;

            _conn = new MySqlConnection(connectionString);

            _conn.Open();
        }

        private void ClearState()
        {
            _isGetData = false;
            QueueItem = default(T);

            if (_conn != null)
            {
                if (_conn.State == ConnectionState.Open)
                {
                    _conn.Close();
                }

                _conn.Dispose();
            }
        }

        public void Dispose()
        {
            if (_isGetData)
            {
                Rollback();
            }
        }
       
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.