Using transaction to avoid race conditions

Signed-off-by: Arpit Goyal <agmps17@gmail.com>