【发布时间】:2018-03-11 19:16:17
【问题描述】:
我有一个以异步方式使用 TCP 套接字的 C# 项目。
每个请求都来自客户端并从 SQL Server 存储过程中提出问题,在问题结束后打开和关闭 SQL 连接。
我用过这段代码:
using (var con = new SqlConnection(setting.ConnectionString))
{
try
{
//some codes (edited)
SqlCommand command = new SqlCommand(con);
command.CommandText = "procedurename1";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@name", sb.ToString()));
SqlDataAdapter adapter = new SqlDataAdapter(command);
try
{
adapter.Fill(dataSet);
}
catch (Exception ex)
{
con.Close();
con.Dispose();
throw ex;
}
finally {
con.Close();
con.Dispose();
}
}
catch(Exception ex)
{}
finally
{
con.close();
con.dispose();
}
}
我用过
netstat -a -n | find /c "1433"
统计打开和关闭的 SQL 连接数。
问题是 SQL 连接数增加,很少减少和倒数。
主要问题,当我的程序在大约 30 分钟的大量请求下工作时,我得到了
SqlCommand 超时错误(默认 30 秒过去)
重新启动我的 C# 程序后,SqlCommand 超时将消失。
这是我的程序问题还是 SQL Server 端的问题?
记住它总是调用 SQL Server 中的存储过程,而不是执行查询 直接。
主要方法:
public void main()
{
Task.Factory.StartNew(() =>
{
allDone.Reset();
mySocket.AcceptAsync(e);
allDone.WaitOne();
});
}
public void e_Completed(object sender, SocketAsyncEventArgs e)
{
var socket = (Socket)sender;
ThreadPool.QueueUserWorkItem(HandleTcpRequest, e.AcceptSocket);
e.AcceptSocket = null;
socket.AcceptAsync(e);
}
public void HandleTcpRequest(object state)
{
//do some code and connection to SQL server
DLL.Request httprequest = new DLL.Request(dataSet.Tables[0], fileDt);
DLL.IHttpContext _context = new DLL.HttpContext(httprequest);
_context.GetResults();
}
【问题讨论】:
-
36 小时过去了,没有更新...进展如何?
-
进展如何?我的回答没有有用吗?
-
@Richardissimo 还没有。我已经更改了代码的某些部分并在套接字部分使用 while true 并检查同步套接字。等待结果
-
@Richardissimo 我为无法关闭的收藏找到了另一种解决方案。 sqlconnection.clearallpool()
标签: c# sql-server connection-timeout tcpsocket mmo