长轮循实现,系统消息提醒,经常数据出错!

超级路灯 超级路灯 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我做的一个“系统消息提示”,用的一个长轮循实现。单独的一个SqlConnection,其它的功能又用的另一个SqlConnettion

系统的使用人数大概在60-100左右。

现在的情况的是:有时用着用着,系统会提示出错。多刷新几次,就又正常了。

出错的原因是:提示得很乱,我初步判断是因为轮循的链接,同时触发太多,影响了正常功能应用的链接。

sql操作类用的网上下的DBHelper。自己看了下代码没问题。

当把消息提醒功能关闭,系统不会出错。

请教下这个问题应该怎么处理。

轮循的主要代码
int SendTime = 0; //最多等待时间
string eid = Common.Const.GetF("eid");//要查询的用户

int.TryParse(Common.Const.GetF("time"), out SendTime);
int i = 0;//计算超时时间(秒)
while (true)
{
Thread.Sleep(5000);//停留五千毫秒(5秒),以用这个等待时间达到长轮询
i++;
if (i < SendTime)
{
if (eid != "")
{
lock (this)
{
string strWhere = " receiverId=" + eid + " and isRead=0";
int newmsg = Common.DBHelper.GetRecordCount_msg("OA_SysMsg", strWhere);
if (newmsg != 0)
{
context.Response.Write(newmsg);
break;
}
}
}
}
if (i == SendTime)
{
context.Response.Write("fail:无数据");
break;
}
}


数据查询方法
public static int GetRecordCount_msg(string TableName, string StrWhere)
{
SqlConnection conn_msg;
conn_msg = new SqlConnection();
conn_msg.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

string strsql = "select Count(*) from " + TableName;
if (StrWhere != "")
{
strsql += " where " + StrWhere;
}

SqlCommand cmd_msg = conn_msg.CreateCommand();
conn_msg.Open();
try
{
cmd_msg.CommandText = strsql;
object obj = cmd_msg.ExecuteScalar();
conn_msg.Close();
if (obj == null)
{
return 0;
}
else
{
return int.Parse(obj.ToString());
}
}
finally {
conn_msg.Close();
}
}
...全文

相关推荐

评论 12
收藏 已收藏
分享
返回顶部
超级路灯
C# 111,129 成员 642,540 内容