It was difficult to choose correct title, but here is the problem:
I’ve an application with simple logic, that gets data from TFS, iterates over “items” and updates local DB with these “items”. Here is a code snippet for updating the row:
using (ResultDBDataContext local_db= new ResultDBDataContext())
{
DBTestResult dbRes = local_db.DBTestResults.getByPrimaryKey(args_supplied);
if (dbRes == null) {
dbRes = new DBTestResult(){//set properties}
local_db.DBTestResults.InsertOnSubmit(dbRes);
}
else{
dbRes.isDirty = true;
dbRes.otherPropertis = set_to_some_values;
}
local_db.SubmitChanges();
}
This is working in Windows Console program (imported >100K rows like this), which proves that logic is correct. But when I put same logic to Windows Service (against same data), after a while (<2K rows) I get an SQL exception saying “can’t insert duplicate key into the table”. Note that I do SubmitChanges() after updating/inserting each row
I think this is somehow related on the followings, but not able to find out the reasons:
- How Windows Services handle DB connectios. (Do they pool? Do they cache data? etc.)
- Linq2SQL Dataconext handling it’s update objects. (What properties of Linq2SQL Datacontext make it behave differently under Windows Services?)
Any ideas/suggestions/links/tutorials/articles on how to resolve this issue or on finding out more information about the problem are greatly welcome.
Thanks.
Update: The data coming from TFS, may violate the database’s primary key constraint – but since I’m inserting/updating one row at a time and committing changes, the last entry for the same primary key will reside in database.
This sounds to me like it’s not a problem with the service side of the code.
I think you need to log/inspect this error behaviour further – just try logging which primary key(s) this is occurring for – it may be that you’ll notice some error in your getByPrimaryKey implementation that way.
Also check that your service is single threaded – i.e. that there can’t be two instances executing simultaneously – that could certainly lead to an error.