修改linq-to-sql命令文本时,是否应该关闭连接?
我正在修改 linq-to-sql 的命令文本以强制它使用 nolock,就像这样...
if (db.Connection.State == System.Data.ConnectionState.Closed)
db.Connection.Open();
var cmd = db.GetCommand(db.Customers.Where(p => p.ID == 1));
cmd.CommandText = cmd.CommandText.Replace("[Customers] AS [t0]", "[Customers] AS [t0] WITH (NOLOCK)");
var results = db.Translate(cmd.ExecuteReader());
这是一个 MVC 应用程序,因此数据上下文位于基本控制器中,并且可能在此代码之前使用过,更重要的是, 后。我应该在此例程中关闭连接吗?或者根本没有?或者只有我在这里打开它?
更新:
我现在使用更通用的函数(在 DataContext 类中)来修改命令文本,并关闭连接(如果在此处打开)。并且 open 已经被下移到了 ExecuteReader。到目前为止,它一直在发挥作用,并减少了零星的僵局问题。结果不必是准确的。
public List<T> GetWithNolock<T>(IQueryable<T> query)
{
// to skip nolock, just...
// return query.ToList();
List<T> results = null;
bool opened = false;
try
{
if (Connection.State == System.Data.ConnectionState.Closed)
{
Connection.Open();
opened = true;
}
using (var cmd = GetCommand(query))
{
cmd.CommandText = Regex.Replace(cmd.CommandText, @"((from|inner join) \[dbo.*as \[t\d+\])", "$1 with (nolock)", RegexOptions.IgnoreCase);
results = Translate<T>(cmd.ExecuteReader()).ToList();
}
}
finally
{
if (opened && Connection.State == System.Data.ConnectionState.Open)
{
Connection.Close();
}
}
return results;
}
我过去发现,以推荐的方式使用事务会导致站点在一夜之间耗尽连接。据我所知,这是 linq-to-sql 中的一个错误。可能有解决办法,但我试图让我的主要代码简单明了。我现在“只是”必须这样做......
var users = GetWithNolock<User>(
Users
.Where(u => my query
);
I'm modifying the commandtext of linq-to-sql to force it to use nolock, like this...
if (db.Connection.State == System.Data.ConnectionState.Closed)
db.Connection.Open();
var cmd = db.GetCommand(db.Customers.Where(p => p.ID == 1));
cmd.CommandText = cmd.CommandText.Replace("[Customers] AS [t0]", "[Customers] AS [t0] WITH (NOLOCK)");
var results = db.Translate(cmd.ExecuteReader());
It's an MVC application, so the datacontext is in the base controller, and may have been used before this code, and more importantly, after. Should I be closing the connection in this routine? Or not at all? Or only if I opened it here?
Update:
I'm now using the more general function (in the DataContext class) to modify the commandtext, and closing the connection if it was opened here. And the open has been moved down to the ExecuteReader. So far it has been working and reducing the sporadic deadlock issues. The results do not have to be right-up-to-the-second.
public List<T> GetWithNolock<T>(IQueryable<T> query)
{
// to skip nolock, just...
// return query.ToList();
List<T> results = null;
bool opened = false;
try
{
if (Connection.State == System.Data.ConnectionState.Closed)
{
Connection.Open();
opened = true;
}
using (var cmd = GetCommand(query))
{
cmd.CommandText = Regex.Replace(cmd.CommandText, @"((from|inner join) \[dbo.*as \[t\d+\])", "$1 with (nolock)", RegexOptions.IgnoreCase);
results = Translate<T>(cmd.ExecuteReader()).ToList();
}
}
finally
{
if (opened && Connection.State == System.Data.ConnectionState.Open)
{
Connection.Close();
}
}
return results;
}
I have found in the past that using a Transaction in the recommended way causes the site to run out of connections overnight. As far as I know, that's a bug in linq-to-sql. There may be ways around it, but I'm trying to keep my main code straightforward. I now "just" have to do this...
var users = GetWithNolock<User>(
Users
.Where(u => my query
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你打开它,你应该关闭它。其他 LinqToSql 操作与此模式匹配。
在我的代码中,我无条件打开连接并在finally中关闭连接。如果有人给我一个开放的连接,那是他们的错,我碰巧为他们关闭了它。
您可以延迟打开连接,直到执行Reader 之前。
If you Open it, you should Close it. Other LinqToSql operations match this pattern.
In my code, I unconditionally open the connection and close the connection in a finally. If someone passes me an open connection, that's their fault and I happen to close it for them.
You could delay opening the connection until just before ExecuteReader.