并行执行查询会抛出“底层提供程序打开失败”。错误
有时(并非总是如此),我会收到以下错误:“底层提供程序打开失败。”
这是我的情况:
我有一个并行处理的整数键列表,用作编译的选择查询中的参数。我在 RIA 域服务中使用它。
var queryResult = new List<int> {1, 2, 3}.AsParallel().Select(i => CompiledQueries.GetRecordByKey(this.ObjectContext, i)).ToList();
编译后的查询如下所示:
public static IEnumerable<CompiledQueryResult> GetRecordByKey(MyEntities _context, int _key)
{
if (_getRecordByKey == null)
{
_getRecordByKey = CompiledQuery.Compile<MyEntities, int, IEnumerable<CompiledQueryResult>>
((ctx, key) =>
ctx.Records
.Where(r => r.Id == key)
.Select(r => new CompiledQueryResult
{
Id = r.ID,
Name = r.Name,
...
})
);
}
return _getRecordByKey.Invoke(_context, _key);
}
我使用的是 EF4、RIA(实际上是将域服务的 ObjectContext 传递给编译后的查询方法),连接字符串包含著名的 MultipleActiveResultSets=True... 当 MultipleActiveResultSets 设置为 false 时,我立即收到错误消息。
这里使用的代码是真实代码的简化版本。我还传递了更多的键,因此有更多的并行查询。 有时我在内部异常中看到数据读取器正在关闭,但状态正在连接..
我尝试扩大连接池大小,但没有成功。
有没有人有好的建议来解决这个问题?提前谢谢。
Sometimes, not always, I'm getting following error: "The underlying provider failed on open."
This is my situation:
I have a list of integer keys I process in parallel to be used as parameter in a compiled select query. I use this in a RIA domainservice.
var queryResult = new List<int> {1, 2, 3}.AsParallel().Select(i => CompiledQueries.GetRecordByKey(this.ObjectContext, i)).ToList();
This is how the compiled query looks like:
public static IEnumerable<CompiledQueryResult> GetRecordByKey(MyEntities _context, int _key)
{
if (_getRecordByKey == null)
{
_getRecordByKey = CompiledQuery.Compile<MyEntities, int, IEnumerable<CompiledQueryResult>>
((ctx, key) =>
ctx.Records
.Where(r => r.Id == key)
.Select(r => new CompiledQueryResult
{
Id = r.ID,
Name = r.Name,
...
})
);
}
return _getRecordByKey.Invoke(_context, _key);
}
I'm using EF4, RIA (Actually the ObjectContext of the domainservice is passed to the compiled query method), the connection string contains the famous MultipleActiveResultSets=True...
When MultipleActiveResultSets is set to false I get the error immediately.
The code used here is a simplified version of the real code. I'm also passing a lot more keys, thus more parallel queries..
Sometimes I see in the inner exception that a data reader is being closed, but the status is connecting..
I've tried to enlarge the connection pool size, but without succes.
Has anyone good suggestions to resolve this problem? Thx in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试将连接字符串中的最小池大小选项设置为更高的值?
尝试以下链接:msdn
Have you tried to set the minimum pool size option in your connectionstring to a higher value?
Try following link: msdn
我的应用程序中也发生了同样的问题,最终导致了 ObjectContext 的跨线程使用。如果您有一个静态混合并最终同时从两个不同的线程(在同一个 ObjectContext 上)执行查询,那么当第一个完成的线程关闭连接而另一个线程尝试打开它时,您将得到一个异常。
愚蠢的我没有从以前的项目中学到这让我们使用 LinqToSQL,它实际上在连接的读取器上引发了跨线程操作异常。不幸的是,ObjectContext 不会以同样的方式阻止它。
The same issue was happening in my application and it ended up being cross thread use of an ObjectContext. If you have a static in the mix and end up executing queries from two different threads at once (on the same ObjectContext) then when the first to finish is closing the connection and the other is trying to open it you will get an exception.
Silly me didn't learn from a previous project where this got us using LinqToSQL which actually threw a cross thread operation exception on the reader for the connection. Unfortunately the ObjectContext doesn't block this in the same way.