Executing queries in parallel throws "The underlying provider failed on open." error
Asked Answered
U

2

11

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.

Upturn answered 21/2, 2011 at 15:37 Comment(0)
A
9

Have you tried to set the minimum pool size option in your connectionstring to a higher value?

Try following link: msdn

Aspinwall answered 22/2, 2011 at 14:32 Comment(1)
Hi Stephane, thx for your solution, I changed the min pool size from 0 to 1000 and the error doesn't occur any longer. I already had the max pool size specified to 1000, but I would have never thought this setting would solve the issue. Thx a million .. up to the next B)Upturn
K
2

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.

Knickerbockers answered 3/5, 2011 at 7:28 Comment(1)
This was my issue as well. I solved the problem by manually opening the connection before executing the series of parallel tasks, and then closing it when they were all finished. msdn.microsoft.com/library/bb738698(v=vs.100).aspxLong

© 2022 - 2024 — McMap. All rights reserved.