Only write to the database in a transaction