There is already an open DataReader associated with this Connection which must be closed first

We have implemented a service stack service that calls a ORMLite database. We are using self hosted services.

The database has a single open connection. A bug in another program highlighted a problem. If the webserice gets called twice very quickly we get this error thrown.

Stack=MySql.Data.MySqlClient.MySqlException (0x80004005): There is already an open DataReader associated with this Connection which must be closed first.
at MySql.Data.MySqlClient.ExceptionInterceptor.Throw(Exception exception)
at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.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.ExprConvertToList[T](IDbCommand dbCmd, String sql, IEnumerable1 sqlParams, HashSet1 onlyFields)
at ServiceStack.OrmLite.Support.LoadList2..ctor(IDbCommand dbCmd, SqlExpression1 q)
at ServiceStack.OrmLite.OrmLiteReadCommandExtensions.LoadListWithReferences[Into,From](IDbCommand dbCmd, SqlExpression1 expr, IEnumerable1 include)
at ServiceStack.OrmLite.ReadExpressionCommandExtensions.LoadSelect[T](IDbCommand dbCmd, Expression1 predicate, IEnumerable1 include)
at ServiceStack.OrmLite.OrmLiteReadExpressionsApi.<>c__DisplayClass34_01.<LoadSelect>b__0(IDbCommand dbCmd) at ServiceStack.OrmLite.OrmLiteExecFilter.Exec[T](IDbConnection dbConn, Func2 filter)
at ServiceStack.OrmLite.OrmLiteReadExpressionsApi.Exec[T](IDbConnection dbConn, Func2 filter) at ServiceStack.OrmLite.OrmLiteReadExpressionsApi.LoadSelect[T](IDbConnection dbConn, Expression1 predicate, String[] include)

Q1: is there any simple way to fix this?

Q2: The problem can be fixed by, wrapping every call with a lock or calling DbOrmConnectionFactory.Open() before every ORM request. Both will have a performance cost.

In WCF I remember you could tell the service to only allow X instances of its self to be created.
Then you put the SQlconnection in the session, which would be remain open between calls. This is just an idea.

Is is possible?

DB connections are already disposed of after use, this sounds like your Service is trying to share the same DB connection.

Are you using base.Db connection in your Service as that resolves a new db connection then disposes it after the Service is executed. Can you provide the code in your Service that’s causing this exception?