You could solve this on the database side using transaction boundaries and an exclusive lock via UPDATE statements.
Setting up a lock table:
create table "LockTable" ( LockID integer primary key, LockTable varch
+ar(255) NOT NULL, LockTime datetime NULL);
insert into "LockTable" values (0, '', NULL);
insert into "LockTable" values (1, 'MyTable', NULL);
Then using it to force INSERT statements to queue up (all INSERT statements have to play by the rules for this to work; not as effective in an ad hoc environment but great if you control the process):
begin transaction;
update "LockTable" set LockTime=CURRENT_TIMESTAMP where LockTable = 'M
+yTable';
insert into "MyTable" values (a, b, c, etc.);
update "LockTable" set LockTime=NULL where LockTable = 'MyTable';
commit transaction;
This trick works in most DBMS systems, although you do have to pay attention to the specific effects if the transaction isolation level is set other than one might expect. It can be a performance soak, though, so use it only where you need it.
|