Sub topic 1
What an Apex Transaction Is
Prerequisites
This chapter assumes you know basic Apex syntax (classes, methods, collections) and can run DML statements such as insert and update. You should also be able to use try/catch with DmlException and know that governor limits exist.
The working model
A transaction is a set of database operations that succeed or fail together. In Apex, one transaction covers all the code that runs from an entry point until that code finishes. This includes the entry point itself, the triggers it fires, and any flows, workflow field updates, and other code called along the way.
If the transaction finishes without an unhandled exception, Salesforce commits it. Every DML change becomes permanent and visible to other users. If an unhandled exception ends it, Salesforce rolls back all DML since the transaction began. Nothing is saved.
Until the commit, your changes are pending. Your own queries see them, but other users do not.
public class TransactionDemo {
public static void run() {
insert new Account(Name = 'First');
insert new Account(Name = 'Second');
// Both rows are pending here. They commit together
// when the transaction ends without an unhandled exception.
Integer pending = [SELECT COUNT() FROM Account WHERE Name IN ('First', 'Second')];
System.debug('Visible to this transaction: ' + pending);
}
}What counts as one transaction
Each of these starts its own transaction:
- An anonymous Apex execution or a call to an
@AuraEnabledor@RemoteActionmethod - A DML operation from the UI or API, which fires its triggers inside the same transaction
- A web service or REST method invocation
- Each
executecall of a batch job - A queueable job, a future method, or a scheduled job run
A trigger is not a separate transaction. It runs inside the transaction of the DML statement that fired it. If the trigger throws an unhandled exception, the whole save fails, including the original DML.
Limits belong to the transaction
Governor limits are counted per transaction. A DML statement limit of 150 and a SOQL query limit of 100 apply to the whole transaction, including triggers and everything else that ran in it. Because of this, one transaction that does too much fails as a unit. Later chapters show that savepoints and rollbacks also count against the DML statement limit.
A common mistake
Developers often assume that each insert line is its own transaction. It is not. Two inserts in one method share a transaction, so a failure later in the method can undo both of them.
public class UnhandledDemo {
public static void run() {
insert new Account(Name = 'Will be rolled back');
insert new Account(); // Name is required, so this throws DmlException
}
}The second insert throws a DmlException that nothing catches. The transaction ends in error, so Salesforce also rolls back the first account. After the call, no account named "Will be rolled back" exists.
The next chapters build on this model. They cover where transactions start and end in more detail, what happens when you catch an exception, and how to roll back only part of the work.
Exercise
A trigger on Account throws an unhandled exception during insert acct; in an Apex method. The method had earlier inserted a Contact in the same execution. Is the Contact saved?
Show answer
No. The trigger runs inside the same transaction as the method. The unhandled exception ends the transaction in error, so Salesforce rolls back all DML, including the earlier Contact insert.
Exercise
Why does the code in the "A common mistake" section leave no accounts behind, even though the first insert succeeded?
Show answer
Both inserts belong to one transaction. The second insert throws an uncaught DmlException, so Salesforce rolls back the entire transaction, including the first insert.