added try-finally to deal with error in withTransaction if transaction had
already been started