I have to issue many invoices for which i use hangfire jobs. Each invoice number has to be unique. I have got this method in my NumberSequenceRepository which should prevent from using same invoice number twice:
static object padlock = new object();
public InvoiceNumberSequence GetNextInvoiceNumber(DateTime forDate)
{
lock (padlock)
{
var seq = db.InvoiceNumberSequence.Single();
seq.CurrentNumber++;
db.SaveChanges();
return seq;
}
}
Yet it looks like Hangfire ignores lock statements (also tried Mutex but with no results) and numbers do repeat. Why is that?