I am using mnesia table.This table has two attributes(primary key and its value). Now i am trying delete a tuple from mnesia table.I am using delete/1 function of mnesia for deletion purpose.This function takes table name and key corresponding to tuple fro which deletion has to be made.My problem is how can i handle the scenrio when tuple corresponding to passed key is not present.This delete function gives {atomic,ok} every time?
For your case you have to read the record first and delete it only after that. To prevent an access to the record from other transactions between 'read' and 'delete' operations use 'write' lock kind when you are reading the record. It gives your transaction an exclusive access to it:
delete_record(Table, Key) ->
F = fun () ->
case mnesia:read(Table, Key, write) of
[Record] ->
mnesia:delete({Table, Key}),
{ok, Record};
[] ->
mnesia:abort(not_exist)
end
end,
mnesia:transaction(F).