Question

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.

Was it helpful?

Solution

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

Try following link: msdn

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top