I think that's a common problem but I haven't found any solution, perhaps I'm not searching for the problem correctly in google. In summary, I have a process that inserts multiple rows in a table (among a lot of other things in the same transaction), but the process is executed in multiple threads and multiple servers.
TABLE: COVERAGES
COLUMNS: COV_Id, COV_Description
The description is Unique, but not as a constraint in database (legacy), and I want to avoid to insert duplicate descriptions. I've isolated the search and insert in a independent transaction and I want to lock the table before select and liberate it after "Save" if it not exists.
I would something like that (high level):
{
this.Lock(Coverage); // Lock table Coverages to avoid select out of this transaction
Coverage coverage = session.QueryOver<Coverage>().Where(g => g.Description == description).Take(1).SingleOrDefault();
if (coverage == null)
{
this.Save(new Coverage { Description = description });
}
return coverage;
};
I can't use lock instruction of C# because the process is executed in multiple servers, and I can't use Lock instruction of NHibernate, because precisely I want to block when I've no results.
I'm using NHibernate 3.3 for SqlServer and Oracle.