从连接池的角度来看我做错了什么吗?
我有一个类,它使用 ADO.NET 和 LINQ 来访问一台服务器上的一对数据库。数据库中的表并不广泛,因此实体对象相当轻。我已经尽我所能编写了代码,当然是利用经验和网络文章。例如...
http://dotnetperls.com/sqldataadapter http://www.velocityreviews.com/forums/t71781-set-maximum-pool-size-in-web-config.html http://msdn.microsoft.com/en-us/library/ms971481#adonetbest_topic5
运行我的代码的服务器仅运行我的代码,与数据库主机不是同一台服务器。通过查看从我的 .NET 应用程序服务器到数据库服务器的连接(它是一个 Windows 服务,但我不想详细讨论这一点,因为它现在看起来并不重要),连接数约为 200 个,但它当然应该小于那个;它应该在 10 左右,因为我已在 appSettings.config 中将最大池大小设置为 10。
有人可以看一下我的连接代码并告诉我我是否正在做一些会导致连接突然上升的事情吗?
这是我的 LINQ DB 上下文创建:
private const string ConnectionKey = "SQL2";
protected static string ConnectionString
{
get
{
return _connectionString = (_connectionString == null) ? ConfigurationManager.ConnectionStrings[ConnectionKey].ConnectionString : _connectionString;
}
}
private static string _connectionString = null;
protected static PricingDBDataContext ContextDB
{
get
{
if (_context == null)
{
_context = new PricingDBDataContext(ConnectionString);
}
return _context;
}
}
private static PricingDBDataContext _context = null;
这是 ADO.NET 方面的内容:
protected DataSet GetDataSet(bool isSproc, string cmdStr, params object[] args)
{
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
{
cmd.CommandType = isSproc ? CommandType.StoredProcedure : CommandType.Text;
for (int index = 0; index < args.Length; index += 2)
cmd.Parameters.AddWithValue(args[index].ToString(), args[index + 1]);
conn.Open();
DataSet set = FillSet(cmd);
conn.Close();
return set;
}
}
}
private DataSet FillSet(SqlCommand cmd)
{
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet set = new DataSet();
adapter.Fill(set);
return set;
}
谢谢,
Matt。
I have a class which uses both ADO.NET and LINQ to access a pair of databases on one server. The tables in the DBs are not extensive and so the entity objects are fairly light. I have written the code as best as I, using experience and net articles of course. For example...
http://dotnetperls.com/sqldataadapter
http://www.velocityreviews.com/forums/t71781-set-maximum-pool-size-in-web-config.html
http://msdn.microsoft.com/en-us/library/ms971481#adonetbest_topic5
The server which is running my code is only running my code and is not the same server as the db host. From looking at the connections going to the DB server from the my .NET app server (it's a Windows Service, but I don't want to dwell on that as it seem immaterial right now) the number of connections is around 200, but it should certainly be less than that; It should be around 10 as I have set the Max Pool Size to 10 in the appSettings.config.
Could anyone take a look at my connection code and tell me if I'm doing something which would cause the connections to shoot up, please?
Here is my LINQ DB context creation:
private const string ConnectionKey = "SQL2";
protected static string ConnectionString
{
get
{
return _connectionString = (_connectionString == null) ? ConfigurationManager.ConnectionStrings[ConnectionKey].ConnectionString : _connectionString;
}
}
private static string _connectionString = null;
protected static PricingDBDataContext ContextDB
{
get
{
if (_context == null)
{
_context = new PricingDBDataContext(ConnectionString);
}
return _context;
}
}
private static PricingDBDataContext _context = null;
Here is the ADO.NET side of things:
protected DataSet GetDataSet(bool isSproc, string cmdStr, params object[] args)
{
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
{
cmd.CommandType = isSproc ? CommandType.StoredProcedure : CommandType.Text;
for (int index = 0; index < args.Length; index += 2)
cmd.Parameters.AddWithValue(args[index].ToString(), args[index + 1]);
conn.Open();
DataSet set = FillSet(cmd);
conn.Close();
return set;
}
}
}
private DataSet FillSet(SqlCommand cmd)
{
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet set = new DataSet();
adapter.Fill(set);
return set;
}
Thanks,
Matt.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为一般规则,如果您对连接池执行任何明确的操作,那么您可能做错了。自 .Net 早期以来,默认设置就对我很有用。
以下是一些需要记住的事项:
IDisposable
并且应该可以显式地或在using
块中进行处理。这包括连接、命令、数据集和数据读取器IDisposable
并且应该被处置As a general rule, if you are doing anything explicit with the connection pool, you are probably doing it wrong. The default settings have served me well since the early days of .Net
Here are some things to keep in mind:
IDisposable
and should be disposed of either explicitly or in ausing
block. This includes connections, commands, datasets and data readersIDisposable
and should be disposed