mysql q4m安装_Mysql Q4M 队列操作封装(二) | 学步园

///

/// Q4M队列操作基类

///

///

/// Author:luyifeng

/// Createday:2013 05 02

public abstract class MyQ4M : IQ4M

{

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(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();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值