[SalesForce] In Apex, what is the difference between the “execution context” and a transaction

What are the boundaries of the execution context? What are the boundaries of a transaction?

Best Answer

I've come at this from the perspective of when the logging records.

The first and last thing the Apex log records will be EXECUTION_STARTED and EXECUTION_FINISHED respectively.

From Debug Log Details

Execution Units
An execution unit is equivalent to a transaction. It contains everything that occurred within the transaction. The execution is delimited by EXECUTION_STARTED and EXECUTION_FINISHED.

From What's considered an "execution context"? (via Bachovski's comment):

An execution context is the context information available during the lifetime of a transaction. Transactions are requests initiated by users when they update a record in the UI, use the API to upload records, etc.

From Apex Transactions

An Apex transaction is sometimes referred to as an execution context. Both terms refer to the same thing. This guide uses the Apex transaction term.

So, each execution context has a corresponding transaction. For all intents and purposes they are one and the same thing.

Limits are applied per Apex Transaction. See Per-Transaction Apex Limits

There are some exceptions for certified managed packages (Aloha Apps?). See Per-Transaction Certified Managed Package Limits

If the overall execution terminates with an exception rather than completing successfully then the transaction will be rolled back. There are finer level DML transaction controls using database savepoints, but the overall execution transaction can still be rolled back for an unhandled exception. See Transaction Control.

Related Topic