System.ArgumentException: ExecuteReader requires an open and available Connection

Hi @mythz,

We’re facing a weird issue in one of our production APIs.

  1. LWe have a transaction and pass a IDbConnection around to some repository methods to insert or save data.
  2. We wanted all of this in a single transaction hence passing around the IDbConnection.
  3. Once we’ve updated the necessary records we call a AutoQuery endpoint through the Service’s Gateway to retrieve related entities.

It’s sporadic but we are seeing exceptions every now and then namely:

System.ArgumentException: ExecuteReader requires an open and available Connection. The connection's current state is open. ---> System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is open.
   at System.Data.SqlClient.SqlConnection.GetOpenTdsConnection(String method)
   at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
   at System.Data.SqlClient.SqlCommand.ValidateCommand(Boolean async, String method)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader()
   at ServiceStack.OrmLite.OrmLiteCommand.ExecuteReader()
   at ServiceStack.OrmLite.OrmLiteReadCommandExtensions.ExecReader(IDbCommand dbCmd, String sql)
   at ServiceStack.OrmLite.OrmLiteResultsFilterExtensions.ConvertToList(IDbCommand dbCmd, Type refType, String sql)
   at ServiceStack.OrmLite.Support.LoadListSync`2.SetRefField(FieldDefinition fieldDef, Type refType)
   at ServiceStack.OrmLite.OrmLiteReadCommandExtensions.LoadListWithReferences[Into,From](IDbCommand dbCmd, SqlExpression`1 expr, IEnumerable`1 include)
   at ServiceStack.OrmLite.ReadExpressionCommandExtensions.LoadSelect[Into,From](IDbCommand dbCmd, SqlExpression`1 expression, IEnumerable`1 include)
   at ServiceStack.OrmLite.OrmLiteReadExpressionsApi.<>c__DisplayClass48_0`2.<LoadSelect>b__0(IDbCommand dbCmd)
   at ServiceStack.OrmLite.OrmLiteExecFilter.Exec[T](IDbConnection dbConn, Func`2 filter)
   at ServiceStack.OrmLite.OrmLiteReadExpressionsApi.Exec[T](IDbConnection dbConn, Func`2 filter)
   at ServiceStack.OrmLite.OrmLiteReadExpressionsApi.LoadSelect[Into,From](IDbConnection dbConn, SqlExpression`1 expression, IEnumerable`1 include)
   at ServiceStack.TypedQuery`2.Execute[Into](IDbConnection db, ISqlExpression query)
   --- End of inner exception stack trace ---
   at ServiceStack.TypedQuery`2.Execute[Into](IDbConnection db, ISqlExpression query)
   at ServiceStack.AutoQuery.Execute[From](IQueryDb`1 model, SqlExpression`1 query)

I’ve attempted to create a repro here but the closest I get to the same exception is

ExecuteReader requires an open and available Connection. The connection’s current state is closed.

I tried debugging through the source to see if I could find the reason why but unfortunately I came up empty handed.

Any thoughts?

Thanks,
Deon

The Exception happens whenever you try to access the DB connection after it’s been disposed. The Services DB connection as well as all disposable resources are disposed after the service is executed so you should never try to share the same dB connection across multiple services.

If you need to execute shared code across multiple services have them inject the same shared dependency and pass the Services dB connection into that.

1 Like