This post describes the concept of execution context in Apex. We are aware that Force.com enforces governor limits to ensure that no single tenant is completely using the runtime engine resources. Earlier it was different for apex class, trigger, anonymous apex code block. But now they have generalized it for execution context.
So let's dive in following section to get more insight.
Consider we have a class:
And consider submit() method is invoked when a button is clicked. This method inturn invokes 3 other methods: method1() then method2() and finally method3() of ABC class. So here is how execution takes place:
Starting from "Submit()" execution start point, Force.com runtime engine considers beginning of execution context. It involves all actions like arithmetic operations,other method invocations, service invocations performed from this point as part of same execution context till the record is finally committed to database. So number of SOQL operations performed by each say method 1 makes 20 SOQL queries; method 2 makes 30 SOQL and method 3 of ABC class makes 120 SOQL queries, then on total SOQL made in execution context will be 170. Thus runtime engine will throw error saying "SOQL limit exceeded". Reason behind this is governor limits.
Force.com enforces governor limits to ensure that no single tenant is completely using the runtime engine resources. Thus it has limits on number of service invocations or callouts made in a execution context and that is 10. In order to overcome such situations we can make use of asynchronous methods in Force.com to invoke external services. There is no such limit on number of invocation in their case.
I hope you find this post helpful in understanding the concept of execution context within Force.com platform.