Michael
Michael

Reputation: 173

ServiceStack ORMLite Bug

Is there anywhere to report bugs/ request features in ServiceStack?

While using ServiceStack, my ServiceStack.ServiceInterface.Service object was throwing this error: ExecuteReader requires an open and available Connection. The connection's current state is closed.

The Service class includes a Db property (used in examples), which is a IDbConnection - db connections are not thread safe.

I'm interested to know why this non thread safe method of access a database is included in the Service class. It's no good for servicing multiple web service requests.

Upvotes: 0

Views: 442

Answers (1)

Jeff Mitchell
Jeff Mitchell

Reputation: 1479

Service.cs will try to resolve an IDbConnectionFactory that will create a new IDbConnection for you, so there isn't a thread safety issue here.

If you'd like to handle it differently, you can override it.

private IDbConnection db;
public virtual IDbConnection Db
{
    get { return db ?? (db = TryResolve<IDbConnectionFactory>().OpenDbConnection()); }
}

Source: https://github.com/ServiceStack/ServiceStack/blob/ada0f43012610dc9ee9ae863e77dfa36b7abea28/src/ServiceStack/Service.cs#L68

Edit: Maybe it's not clear that OrmLiteConnectionFactories automatically create a new connection in conjunction with an OpenDbConnection call, but they do:

Source: https://github.com/ServiceStack/ServiceStack.OrmLite/blob/db40347532a14441eba32e575bcf07f3b2f45cef/src/ServiceStack.OrmLite/OrmLiteConnectionFactory.cs#L72

Upvotes: 2

Related Questions