4

We're pulling down data from SQL in a high throughput scenario here, through one app. Possibly up to 100+ SQL requests at any one time. Some may be long running, which may be why they mount up.

Intermittently (but not constantly), we're getting the following exception when calling SqlConnection.Open();:

System.Exception: Error getting document from database --->
System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ---> System.ComponentModel.Win32Exception: Access is denied
--- End of inner exception stack trace ---

at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource
1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource
1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at BridgeService.SqlDal.GetDataFromSql(Int32 entityId)
--- End of inner exception stack trace ---

The code causing this exception looks like this:

public DataSet GetDataFromSql(int entityId)
{
    DataSet tempDataSet = null;

    using (var cnn = new SqlConnection(GetConnectionString()))
    {
        cnn.Open(); // this line throws the exception

        try
        {
            // stuff gets done on cnn
        }
        catch (Exception e)
        {
            throw e;
        }
    }

    return tempDataSet;
}

I'm imagining this is something to do with the connection pool in some way. Is there a way we can check for an available connection in the pool before we open and use it? Or is this caused by something completely different?

If you need any more info, feel free to ask.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Craig Brett
  • 2,295
  • 1
  • 22
  • 27
  • It could be [network problems](http://social.msdn.microsoft.com/Forums/en-US/64e31e18-6a0b-452c-a58e-0e3fab960606/sql-server-connectivity-issues?forum=sqldatabaseengine). – stuartd Oct 06 '14 at 10:19
  • it could be one of the folowing problems : 1- uncorrect Server Adresse 2- uncorrect configuration of the server thus making it unreachable 3- you dont have the rights to connect to that server 4- the server is not propraly configured to be accessed from a remote connection – Youness Oct 06 '14 at 10:20
  • @Youness the error is **intermittent** – stuartd Oct 06 '14 at 10:40
  • @stuartd I'm checking that now. thanks. And you beat me to it with the reply :) – Craig Brett Oct 06 '14 at 10:44
  • Verify with the network admin if he limited the concurrent connections on the sever. One wise kid cut me down to 50 connections to the SQL computer and thought it was more secure that way. – Franck Oct 06 '14 at 11:15
  • I suspect the intermittent connection problem is a symptom of different issue. With 100+ concurrent queries, including long-running ones, the server may be too overloaded to respond to connection requests. What sort of hardware is the SQL box running on? – Dan Guzman Oct 06 '14 at 12:04
  • You can run easily 500+ connections doing LRP (1 to 8 min each) on very limited server on a small VM like duo 2.8, 4 gig ram. It's slow but it doesn't disconnect. The piping does it's job fine. Obviously you need to set your client drop connection time to more than default 30 seconds. I have mine set to 180 seconds and it run fine on that test server – Franck Oct 07 '14 at 11:38
  • For what it's worth, [me too](https://stackoverflow.com/q/41487778/7850) :-/ – Shaul Behr Oct 24 '17 at 12:11

3 Answers3

3

I had the same problem once too.Solved by enabling NamePipes in SQL server configuration manager and turning off windows firewall or allowing sqlserver port 1433 in windows firewall Hope it'll work for you too

T.A.P
  • 69
  • 8
  • Sorry about the delay in replying. If this is the issue, why would it be intermitant? This isn't every time. It's only sometimes. – Craig Brett Oct 20 '14 at 16:22
  • @CraigBrett did you ever find out how to fix this issue or what was causing it? – Sturla Oct 17 '17 at 13:31
  • @Sturla: No, I don't think we did. I've moved on from the company where we were using the code, so I'm not sure entirely, but I'm fairly sure we just lived with this issue. – Craig Brett Oct 23 '17 at 12:16
  • @CraigBrett Thanks for the reply. We finally discovered that sql machine had lots of strange event errors. It was just in a strange state. This just happened for no apparent reason. It all got fixed when we moved the database to another machine. – Sturla Oct 23 '17 at 15:13
2

That's outdated but if someone needs a solution,

Open Sql Configuration > Sql Server Network Configuration > Protocols for MSSQLServer Then enable Named Pipes and TCP/IP settings.

Restart MSSql Server via windows services. Named Pipes and TCP/IP settings

hhk
  • 508
  • 7
  • 15
1

I was experiencing a similar problem. Eventually we worked out that it wasn't a problem with SQL Server at all; it was a problem on the API server, where we were spinning up hundreds or even thousands of threads, each one making its own connection to the database. The API server couldn't handle the load and started throwing "Access Denied" exceptions without even really trying to connect.

Solution was to throttle the number of threads. I used the pattern described in this answer.

Shaul Behr
  • 36,951
  • 69
  • 249
  • 387