The problem i have is i could DELETE but then when i hit refresh and send the post data it will try to delete again. Which isnt a problem but now the second statment is a problem since it decreases when it shouldnt.
What is a concurrent safe way to decrease but only if delete has removed an entry? note msgId is a PK so i'll either delete 0 or 1
public void removeMediaMsg(long userId, long msgId)
{
using (var dbTrans = connection.BeginTransaction())
{
command.CommandText = "DELETE FROM user_media_subscription "
+ "WHERE msgId=@msgId AND recipientId=@recipientId;";
command.Parameters.Add("@msgId", DbType.Int64).Value = msgId;
command.Parameters.Add("@recipientId", DbType.Int64).Value = userId;
command.ExecuteNonQuery();
command.CommandText = "UPDATE user_data SET mediaMsgCount=mediaMsgCount-1 WHERE userId=@userId;";
command.Parameters.Add("@userId", DbType.Int64).Value = userId;
command.ExecuteNonQuery();
dbTrans.Commit();
}
}