Sub topic 9
DML governor limits apply to both approaches
This chapter assumes you can write basic Apex and know that code runs inside a transaction with governor limits. Choosing insert or Database.insert changes how errors are reported. It does not change how much DML you are allowed to do.
The two DML limits
Apex enforces two limits per transaction on DML:
| Limit | Synchronous and asynchronous |
|---|---|
| DML statements issued | 150 |
| Records processed by DML | 10,000 |
A statement is one call to a DML operation. The record count is the total number of records passed to all of those calls.
Both approaches count the same way:
insert accounts;is one statement.Database.insert(accounts, false);is one statement.- Each call adds the size of its list to the record count.
Records that fail still count
With allOrNone set to false, some records can fail while others save. The failed records still count toward the 10,000-record limit, because Apex processed them. Partial success does not make a call cheaper.
Measuring usage with the Limits class
The Limits class reports consumption and the maximum for the current transaction. You can run this in Anonymous Apex:
List<Account> accounts = new List<Account>();
for (Integer i = 0; i < 200; i++) {
accounts.add(new Account(Name = 'Limit Demo ' + i));
}
System.debug('Before: ' + Limits.getDmlStatements() + ' of ' + Limits.getLimitDmlStatements());
insert accounts;
Database.update(accounts, false);
System.debug('Statements used: ' + Limits.getDmlStatements() + ' of ' + Limits.getLimitDmlStatements());
System.debug('Rows used: ' + Limits.getDmlRows() + ' of ' + Limits.getLimitDmlRows());The debug output shows 2 statements used and 400 rows used. The statement count is 2 because one insert and one Database.update ran. The row count is 400 because each call processed 200 records.
Savepoints count too
Setting a savepoint and rolling back also count as DML statements. Both Database.setSavepoint() and Database.rollback(sp) use one each. Code that sets savepoints inside a loop can use up the 150-statement limit quickly.
What happens when you exceed a limit
Exceeding either limit throws a System.LimitException. You cannot catch this exception. A catch (DmlException e) block does not handle it, and a catch (Exception e) block does not either. The whole transaction stops and rolls back.
Using Database.insert(records, false) therefore does not protect you from limit errors. The allOrNone parameter only controls record-level failures such as missing required fields or validation rule errors.
The fix is the same for both approaches
Move the DML out of the loop and pass a collection:
List<Contact> contacts = new List<Contact>();
for (Integer i = 0; i < 200; i++) {
contacts.add(new Contact(LastName = 'Demo ' + i));
}
// One statement for 200 records, with either syntax.
List<Database.SaveResult> results = Database.insert(contacts, false);
System.debug('Statements: ' + Limits.getDmlStatements());The next chapter covers this pattern in detail.
Common mistakes
- Assuming
allOrNone = falsemakes a call exempt from limits or from rollback on a limit error. - Counting records instead of statements, or the reverse. Check both counters.
- Forgetting that a savepoint and its rollback each use a statement.
- Testing with 5 records, then running a trigger with 200 records. The statement count may stay low while the row count grows.
Exercise
A loop runs 100 times. Each pass calls Database.insert(new Contact(LastName = 'X'), false); on a single record. How many DML statements and DML rows does this use, and how would you reduce the statement count?
Show answer
It uses 100 statements and 100 rows. Calling the Database method with allOrNone false does not reduce the count. Add each Contact to a List<Contact> inside the loop and call Database.insert(list, false) once afterward. That uses 1 statement and still 100 rows.