10
I have a competition control problem in entering data from a table. The scenario is as follows::
There is a table that records daily data according to user requests, and this data cannot be duplicated. The current competition control checks if there is any record of these data in the table on that day, and if it exists, blocks the insertion.
The problem is that this implementation is being inefficient because when two users simultaneously click on the button, the verification is done simultaneously (resulting in "No data in the database") and the insertion also, creating duplicate data in the table.
How can I implement a concurrency control without using a lock on the table, since this table is constantly used and a lock would probably slow down the transaction?
public void InserirFoo(Foo variavel, int id)
{
var diaDeHoje = DateTime.Now;
if (!VerificarInsercao(id, diaDeHoje))
{
contexto.FooDataSet.inserir(variavel);
contexto.SaveChanges();
}
}
private bool VerificarInsercao(int id, DateTime dataAtual)
{
return contexto.FooDataSet.Any(e => e.id == id && e.dataInsercao == dataAtual);
}
Put your code that’s doing this.
– Maniero
Bigown. Edited question!
– Vinícius