Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql Server Compact Edition concurrency / thread safety (i.e. not thread safe)

Over the last few months I have been developing an application using Entity Framework code first and sql server CE for the first time. I have found the combination of the 2 very useful, and compared to my old way of doing things (particularly ADO.NET) it allows for insanely faster dev times.

However, this morning me and some colleagues came across a problem which we have never seen in any documentation regarding SqlServer CE. It cannot handle more than one insert at once!

I was of the opinion that CE may become my database of choice until I came across this problem. The reason I discovered this was in my application I needed to make multiple requests to a web service at once, and it was introducing a bit of a bottleneck so I proceeded to use a Parallel.Invoke call to make the multiple requests.

This was all working fine untill I turned on my applications message logging service. At this point I began to get the following error when making the web requests:

A duplicate value cannot be inserted into a unique index. [ Table name = Accounts,Constraint name = PK__Accounts__0000000000000016 ]
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlServerCe.SqlCeException: A duplicate value cannot be inserted into a unique index. [ Table name = Accounts,Constraint name = PK__Accounts__0000000000000016 ]

Strange I thought. And my first recation was that it must be something to do with the DbContext, maybe the DbContext I was using was static or something else in my Repository class was static and causing the problem, but after sniffing around I was certaing it was nothing to do with my code.

I then brought it to the attention of my colleagues and after a while it was decided it must be SqlServer CE, and after us all setting up different test projects attempting to recreate the problem using threads it was recreated almost every time, and when using Sql Server Express the problem wasn't ocurring.

I just think it is a bit strange that CE cannot handle something as simple as this. I mean the problem is not only with threading - are you telling me that it cannot be used for a web application where two users may insert into a table at the same time...INSANITY!

Anyway, just wondering if anyone else has come across this late into a project like me and been shocked (and annoyed) that it works this way? Also if anyone could shed light on why it is limited in this way that would be cool.

like image 657
jcvandan Avatar asked Nov 05 '22 20:11

jcvandan


1 Answers

It looks like a bug in SQL CE. See http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=641518

like image 158
Serghei Gorodetki Avatar answered Nov 09 '22 16:11

Serghei Gorodetki