On Wed, 07 Apr 2010 21:11:01 +0000, Andreas Leitgeb wrote:
"Should be ..." doesn't really sound too convincing in the context of
possible concurrency problems ;-)
That was me being cautious since we don't know what DBMS the OP has in
mind. For the following code to work the DBMS *must* support explicit
transaction control, serialised transaction mode and (probably) the
ability to set the lock timeout period. If we have all that the code is
straight forward:
set_session_conditions
{
set auto-commit off;
set transaction serialisation;
set lock timeout = 5 seconds; // needs to be at least 2x the max
// transaction execution time.
}
add_entry
{
start transaction // may be implied
select count(*) from table where key='?'
if (count == zero)
insert into table (key, value) values ('?', '?');
commit
}
The sunlights differ, but there is only one darkness. -- Ursula K. LeGuin,