【发布时间】:2014-11-22 10:32:52
【问题描述】:
就我而言,我通过 HttpClient 向服务器端发出 Web 请求。但是每次连接池中都会有一个新的连接。该程序只使用了一个连接字符串。新连接上升太快,然后超过了最大池大小 100。我必须调查有关数据库连接池和 IIS 的问题。
sqlserver 数据库连接字符串:
<connectionStrings>
<add name="WERP2ConnectionString" connectionString="Data Source=localhost;Initial Catalog=WERP2-1108;User ID=sa;Password=sasa;Connect Timeout=15;Encrypt=False;"/>
</connectionStrings>
客户端程序:
static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine("api test: {0}", i);
ApiTest(); //to mimic the 5 different users make the api request.
}
Console.ReadLine();
}
private static void ApiTest()
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost/StressApi/");
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
var response = client.GetAsync("api/order/GetOrder").Result;
if (response.IsSuccessStatusCode)
{
var message = response.Content.ReadAsStringAsync().Result;
if (!string.IsNullOrEmpty(message))
{
Console.WriteLine(message);
}
}
else
{
Console.WriteLine("Error Code" +
response.StatusCode + " : Message - " + response.ReasonPhrase);
}
//Console.ReadLine();
}
}
WebApi 控制器:
public class OrderController : ApiController
{
[HttpGet]
public HttpResponseMessage GetOrder()
{
OrderModel model = new OrderModel();
var entity = model.GetByID();
HttpResponseMessage response = Request.CreateResponse<OrderEntity>(HttpStatusCode.OK, entity);
return response;
}
}
public class OrderEntity
{
public int ID { get; set; }
public string OrderCode { get; set; }
public int OrderType { get; set; }
}
public class OrderModel
{
public OrderEntity GetByID()
{
OrderEntity entity = new OrderEntity();
string sql = @"select * from salorder where id=97";
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WERP2ConnectionString"].ToString()))
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
// don't forget to actually open the connection before using it
conn.Open();
try
{
using (SqlDataReader dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
while (dataReader.Read())
{
// do something
Console.WriteLine(dataReader.GetValue(0) + " - " + dataReader.GetValue(1) + " - " + dataReader.GetValue(2));
entity.ID = int.Parse(dataReader.GetValue(0).ToString());
entity.OrderCode = dataReader.GetValue(1).ToString();
entity.OrderType = int.Parse(dataReader.GetValue(2).ToString());
//dataReader
}
}
}
finally
{
//SqlConnection.ClearPool(conn);
}
}
return entity;
}
}
每个sql查询都会在sqlserver中进行一个处理,我们可以在SQL SERVER Activity Monitor中找到它们,并且有5条记录,因为我重复了5次查询。
如果我们使用SP_WHO命令,我们也可以验证5个进程记录,它们处于sleeping和AWAITTING COMMAND状态。
我很困惑如何避免这种连接泄漏问题。尽管我每次都进行相同的 sql 查询,但仍然会重新生成新的连接。而且我已经测试了这些连接将在大约 6 分钟 20 秒内被回收。当然,我可以重置 IIS 或让应用程序池回收以释放它们。但这在在线系统中是绝对不能接受的。请问有人可以提出任何建议吗?谢谢。
顺便说一句,编程是使用 .NET Framework 4.0、IIS7 和 SQLSERVER2008 R2 运行的。
【问题讨论】:
-
我尝试处理 httpclient 对象,甚至用 using{...} 块重写它,但连接数仍然是 5。
-
我们必须在连接字符串中添加连接lieftime参数,但我认为httpclient中有一些提示,或者我必须使用httpwebrequest而不是我自己重写代码。
标签: asp.net-web-api httpclient