runtime: Failed SqlConnection with Pooling drags down entire .NET Core MVC App

When Pooling is enabled on a SqlConnection, and the connection fails (bad password, bad username, database doesn’t exist) it will crash the entire .NET Core MVC app running.

For instance, the connection string: Password=mybadpassword;User Id=api_web_admn;Data Source=127.0.0.1,1433;Initial Catalog=MyDatabase;Integrated Security=False;Min Pool Size=1;Max Pool Size=200;Pooling=true;

produces:

Unhandled Exception: System.Data.SqlClient.SqlException: Login failed for user 'api_web_admn'.
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.PoolCreateRequest(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

and causes the .NET Core runtime to exit the thread.

Removing Pooling, or Pooling=false, it simply throws an exception and you can handle it.

As it stands now, you can’t mitigate and handle a failed connection in .NET Core MVC.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16 (10 by maintainers)

Most upvoted comments

@saurabh500 We are having this issue in production so it is critical for us. This effectively means we cannot use connection pooling in Core.

@EndarValuk What version of SqlClient are you using? The fix is available in 4.5.0-rc1 https://www.nuget.org/packages/System.Data.SqlClient/4.5.0-rc1

Hello, this code should reproduce bug:

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
  </ItemGroup>

Controller method:

        public async Task<string> Index()
        {
            string result = "Success!!";
            string connString = "data source=**,35352;initial catalog=**;persist security info=False;Min Pool Size=1;user id=**; password=**;";
            using (SqlConnection conn = new SqlConnection(new SqlConnectionStringBuilder(connString).ConnectionString))
            {
                try
                {
                    await conn.OpenAsync();
                }
                catch (Exception e)
                {
                    result = e.Message;
                }
            }

            return result;
        }