問題文
A pharmacy chain's Apex writes an audit record, then inserts a shipment header, then inserts the shipment lines. If a line fails, the header must be discarded but the audit record has to stay. Which technique meets this?
選択肢
- Write the audit record from a future method so that it is committed separately, then let an uncaught exception roll back the rest, because splitting the audit into another transaction is the only way to keep it when the main work fails and it also removes the need to manage savepoints.
- Wrap the three inserts in one try block and let the catch report the failure, because a caught exception leaves earlier inserts committed, so the audit row survives along with the header written before the failure.
- Insert the lines with the Database method that allows partial success, then remove the header when any row failed, because reading the per-row results is what tells the code whether the header should survive, with the audit row untouched throughout.
- Set a savepoint after the audit insert and roll back to that savepoint when a line fails, which discards the header while leaving the audit record in place, because a rollback undoes only what followed it.