9

I have a fundamental question about Lightning technical design.

Today viewing debug logs I noticed that if Lightning component make multiple calls to server-side (invoke multiple apex-controller methods) all these calls appear in the same debug log. Looks like multiple server requests from lightning component are considered as part of a single transaction.

For example: we have 2 @AuraEnabled methods saveAccount() and saveContact(). Each method performs 55 SOQL queries. On LeX component we have button that invokes saveAccount() first and then saveContact().

  • Question 1: will we hit 101 SOQL query limit in this case?
  • Question 2: will database changes made by saveAccount() rolled back if saveContact() throws an exception?

Link to documentation or detailed explanation of what happening behind the scenes would be much appreciated.

UPD: Logfile Debug Log file with multiple invocations of the same server method

  • If I understand well, you're calling the first method, then in its callback, you call the second method? – Martin Lezer Sep 29 '17 at 14:22
8

See this answer where I experimented with the limits and came across some rather interesting answers:

Question 1: will we hit 101 SOQL query limit in this case?

Yes. For now, governor limits are aggregated across all actions that occur in the same transaction. Use setBackground to force your larger methods to be called in separate processes, or use a Promise chain to call one after the other. It'll take longer, slightly, but you'll reset your limits in between.

Question 2: will database changes made by saveAccount() rolled back if saveContact() throws an exception?

No. Despite the answer to question 1, each method gets its own "savepoint", so one failing will not affect the other failing.

  • I thought this is what would happen since the response is not returned until all actions are completed. I thought it might be a single transaction. Appreciate you testing this out and confirming it.. – Eric Sep 29 '17 at 15:53
  • @sfdcfox setExclusive is not part of documentation and i also see another link in SFSE where a salesforce rep says we should not use it since its not a public method ?salesforce.stackexchange.com/questions/159594/… – RedDevil Sep 29 '17 at 15:59
  • @RedDevil Fair enough. I heard of setExclusive from someone else, not the docs. Still setBackground should help prevent governor limit errors, or you can use a Promise chain. – sfdcfox Sep 29 '17 at 16:04
  • 1
    No worries @sfdcfox, you are a supreme techie, Since you said i thought it is coming in a future release no worries seems to me promise is the way to go. we tried set Background but it doesn't always return data by the time we want. For Getting around governor limits like you said background calls will do – RedDevil Sep 29 '17 at 16:07
1

If you make multiple server calls simultaneously without one being in the callback of another the below documented behaviour will happen

Batching of Actions Multiple queued foreground actions are batched in a single request (XHR) to minimize network traffic. The batching of actions is also known as boxcar’ing, similar to a train that couples boxcars together.

The server sends the XHR response to the client when all actions have been processed on the server. If a long-running action is in the boxcar, the XHR response is held until that long-running action completes. Marking an action as background results in that action being sent separately from any foreground actions. The separate transmission ensures that the background action doesn’t impact the response time of the foreground actions.

When the server-side actions in the queue are executed, the foreground actions execute first and then the background actions execute. Background actions run in parallel with foreground actions and responses of foreground and background actions may come back in either order.

Regarding your questions:

Question 1: will we hit 101 SOQL query limit in this case?

No. They will be 2 seperate transactions in server side

Question 2: will database changes made by saveAccount() rolled back if saveContact() throws an exception?

No. They will run independent of each other.

  • Your answer is exactly the same I imagine it. But on debug logs I see strange things. See my updated question – Yury Bondarau Sep 29 '17 at 15:42
  • I don't see how what you posted as reference addresses the assumptions you made.... – Eric Sep 29 '17 at 15:52
  • I posed documentation to explain box car'ing behaviour of lightning component server calls. – RedDevil Sep 29 '17 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.