Sub topic 2
Where a transaction starts and ends
This chapter assumes you can write basic Apex, run DML (insert, update) on sObjects, use try/catch with DmlException, and know that governor limits exist. It covers how Apex decides where one transaction stops and the next begins.
What starts a transaction
A transaction starts when Salesforce begins running a request that executes Apex. That request is the entry point. Common entry points are:
- A save from the UI or an API call that fires a trigger
- A Lightning or Visualforce controller action
- An Apex REST or SOAP web service call
- Anonymous Apex run from the Developer Console
- A test method
- Each
executecall of a batch job, a queueable job, a scheduled job, or a future method
Everything that runs because of that entry point belongs to the same transaction. This includes triggers, flows, and other Apex your code calls. If a trigger on Account inserts a Contact, and a trigger on Contact runs after that, all of it is one transaction.
What ends a transaction
The transaction ends when the entry point finishes. There are two outcomes:
- Commit. The request ends with no unhandled exception, and Salesforce makes all the DML permanent.
- Rollback. An unhandled exception reaches the top, and Salesforce discards all the DML from the request.
Your code does not call a commit. Reaching the end of the request normally is the commit.
Asynchronous work is a new transaction
Asynchronous Apex does not run inside the transaction that requested it. It runs later, in its own transaction, with its own governor limits.
public class FollowUpJob implements Queueable {
public void execute(QueueableContext ctx) {
insert new Task(Subject = 'Follow up');
}
}insert new Account(Name = 'Acme');
System.enqueueJob(new FollowUpJob());
// The job starts only after this transaction finishes.
// If FollowUpJob throws, only its own DML rolls back.
// The Account insert is a separate transaction and is unaffected.The same holds for batch jobs. Each execute call is its own transaction, so one failed batch does not undo batches that already committed.
Test methods
A test method runs as one transaction, and Salesforce rolls back its data when the test finishes. Test.startTest() and Test.stopTest() reset governor limits for the code between them. They do not start a new database transaction. Test.stopTest() also runs queued asynchronous jobs so you can check their results.
Common mistakes
- Assuming a trigger runs in its own transaction. It shares the transaction of the DML that fired it, so an unhandled exception in the trigger rolls back the original save too.
- Expecting async work to roll back with the code that queued it. It runs separately.
- Assuming limits are shared across async hops. Each transaction gets its own limits.
Exercise
A batch job has three execute calls. The second throws an unhandled exception after inserting records. Which inserts are committed?
Show answer
The inserts from the first and third calls are committed, and the inserts from the second call are rolled back. Each execute call is its own transaction, so a failure in one does not affect the others.
Exercise
Your code inserts an Account and then calls System.enqueueJob. When does the queueable job run, and which transaction owns its DML?
Show answer
It runs after the current transaction finishes. Its DML belongs to the job's own, separate transaction.