I'm investigating an intermittent occurrence of Devart.Data.Linq.ChangeConflictException: Row not found or changed
.
I've been reading the Devart article on concurrency conflicts, and just as a starting point I am trying to reproduce a concurrency conflict. Using the included MS-based LINQ classes this is easy to do (using the code structure below does so successfully). However even following their example in the article I can't produce an exception. I have tried
- Using both an ADO paramaterised query exactly as shown in their article
- Using the simplified ADO query shown below (not worried about injection attacks during this test)
- Using LinqConnect with a new context (also suggested in their article).
- Pausing execution with a debugger and manually updating table using MySQL Workbench.
This is the test code I am using:
public static void MySqlTest()
{
using (MySqlDataContext db = new MySqlDataContext())
{
Customer customer = db.Customers.First(c => c.Username.Equals("ian2"));
MySqlAdoChange(db.Connection);
//MySqlLinqConnectChange();
customer.Address1 = "Original change" + DateTime.UtcNow.Ticks;
db.SubmitChanges();
}
}
public static void MySqlAdoChange(DbConnection connection)
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = "UPDATE customers SET Address1 = 'Conflicting change" + DateTime.UtcNow.Ticks + "' WHERE Username = 'ian2'";
command.ExecuteNonQuery();
}
public static void MySqlLinqConnectChange()
{
using (MySqlDataContext db = new MySqlDataContext())
{
Customer customer = db.Customers.First(c => c.Username.Equals("ian2"));
customer.Address1 = "Conflicting change" + DateTime.UtcNow.Ticks;
db.SubmitChanges();
}
}
What is doubly-strange is that the value saved to the database alternates between the two!
I had to append DateTime ticks on to the end to ensure uniqueness, otherwise it was optimising away my update, and only updating to become the value that wasn't currently active.
Can anyone explain this behaviour? Why can't I produce a ChangeConflictException?