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 ServiceStack.OrmLite.OrmLiteReadCommandExtensions.ExecReader(IDbCommand dbCmd, String sql)
at ServiceStack.OrmLite.OrmLiteResultsFilterExtensions.ExprConvertToList[T](IDbCommand dbCmd, String sql, IEnumerable
1 sqlParams, HashSet1 onlyFields)
2..ctor(IDbCommand dbCmd, SqlExpression1 q)
at ServiceStack.OrmLite.OrmLiteReadCommandExtensions.LoadListWithReferences[Into,From](IDbCommand dbCmd, SqlExpression
1 expr, IEnumerable1 include)
at ServiceStack.OrmLite.ReadExpressionCommandExtensions.LoadSelect[T](IDbCommand dbCmd, Expression
1 predicate, IEnumerable1 include)
1.<LoadSelect>b__0(IDbCommand dbCmd)2 filter)
at ServiceStack.OrmLite.OrmLiteExecFilter.Exec[T](IDbConnection dbConn, Func
at ServiceStack.OrmLite.OrmLiteReadExpressionsApi.Exec[T](IDbConnection dbConn, Func
2 filter)1 predicate, String include)
at ServiceStack.OrmLite.OrmLiteReadExpressionsApi.LoadSelect[T](IDbConnection dbConn, Expression
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?