Use transactions to prevent stale db entries

If save fails, the save wrapper used to leave behind a cloned but
unmodified entry. This change uses database transactions to make sure
the save is atomic.