Sub topic 11
Savepoints and the DML Statement Limit
This chapter assumes you can write basic Apex, run insert and update statements, use try/catch with DmlException, and know that governor limits exist. It also assumes you have read the chapters on setting a savepoint and rolling back to one.
Savepoints cost DML statements
Apex allows 150 DML statements in one transaction. The count includes more than insert, update, delete, and undelete statements. Two other calls also use up this budget:
- Each
Database.setSavepoint()call counts as one DML statement. - Each
Database.rollback(sp)call counts as one DML statement.
A savepoint does not change any data, but it still uses part of your budget. A guarded block that sets a savepoint, inserts one record, and rolls back uses three statements.
Measuring the cost
The Limits class reports how much of the budget you have used. Run this in anonymous Apex:
Integer start = Limits.getDmlStatements();
Savepoint sp = Database.setSavepoint();
System.debug('After setSavepoint: ' + (Limits.getDmlStatements() - start));
insert new Account(Name = 'Temp');
System.debug('After insert: ' + (Limits.getDmlStatements() - start));
Database.rollback(sp);
System.debug('After rollback: ' + (Limits.getDmlStatements() - start));
System.debug('Limit: ' + Limits.getLimitDmlStatements());The debug log shows 1, 2, and 3, followed by a limit of 150. Neither the savepoint nor the rollback is free.
The per-record savepoint mistake
The common error is a savepoint inside a loop. This code uses up to three statements per record:
List<Account> accts = new List<Account>();
for (Integer i = 0; i < 100; i++) {
accts.add(new Account(Name = 'Bulk ' + i));
}
for (Account a : accts) {
Savepoint sp = Database.setSavepoint(); // 1 statement per record
try {
insert a; // 1 statement per record
} catch (DmlException e) {
Database.rollback(sp); // 1 more on failure
}
}With 100 records, the savepoints and inserts alone use 200 statements. The transaction fails with System.LimitException: Too many DML statements: 151 before the loop finishes. The code also breaks bulk processing, because a trigger can receive up to 200 records at once.
Spend the budget once per transaction
Set one savepoint around the whole unit of work. If the unit needs to tolerate individual failures, use the partial-success form of DML instead of a savepoint for each record:
List<Account> accts = new List<Account>();
for (Integer i = 0; i < 100; i++) {
accts.add(new Account(Name = 'Bulk ' + i));
}
Savepoint sp = Database.setSavepoint(); // 1 statement
Database.SaveResult[] results = Database.insert(accts, false); // 1 statement
Integer failures = 0;
for (Database.SaveResult sr : results) {
if (!sr.isSuccess()) {
failures++;
}
}
if (failures > 0) {
Database.rollback(sp); // 1 statement
}This block uses at most three statements for 100 records. A single Database.insert call counts as one DML statement no matter how many records are in the list. The limit on rows processed by DML is a separate limit of 10,000.
Practical rules
- Set a savepoint only where you need a rollback point, not by default.
- Never set a savepoint inside a loop over records.
- Check
Limits.getDmlStatements()againstLimits.getLimitDmlStatements()when a transaction does a lot of DML. - Remember that savepoints used in earlier code in the same transaction, such as a trigger that fired before yours, have already used part of the 150.
Exercise
A method sets one savepoint, runs 10 insert statements, and calls Database.rollback once. How many DML statements does it use?
Show answer
12. The savepoint counts as 1, the 10 inserts count as 10, and the rollback counts as 1.