PDI · Question #101
A developer has identified a method in an Apex class that performs resource intensive actions in memory by iterating over the result set of a SOQL statement on the account. The method also performs…
The correct answer is B. Use the System,Limit classto monitor the current CPU governor limit consumption. C. Use the Database,Savepoint method to enforce database integrity. To manage governor limits and ensure data integrity in resource-intensive Apex operations, a developer should monitor CPU consumption and use savepoints for transaction control.
Question
A developer has identified a method in an Apex class that performs resource intensive actions in memory by iterating over the result set of a SOQL statement on the account. The method also performs a DML statement to save the changes to the datadase. Which two techniques should the developer implement as a best practice to ensure transaction control and avoid exceeding governor limits? Choose 2 answers
Options
- AUse partial DML statements to ensure only valid data is committed.
- BUse the System,Limit classto monitor the current CPU governor limit consumption.
- CUse the Database,Savepoint method to enforce database integrity.
- DUse the Reedonly annotation to bypass the number of rows returned by a SOQL.
How the community answered
(60 responses)- A13% (8)
- B80% (48)
- D7% (4)
Why each option
To manage governor limits and ensure data integrity in resource-intensive Apex operations, a developer should monitor CPU consumption and use savepoints for transaction control.
Partial DML statements (e.g., Database.insert with allOrNone=false) can commit valid data but do not inherently ensure transaction control for preceding operations or prevent governor limits.
The System.Limit class provides methods to retrieve the current consumption of various governor limits, including CPU time, allowing developers to monitor and proactively manage resource usage to avoid exceeding limits.
The Database.Savepoint method allows a developer to set a point in the transaction that can be rolled back to later, which is crucial for maintaining database integrity when DML operations might fail partially or need to be reverted.
The @readonly annotation (not Reedonly) is used for Visualforce pages to bypass SOQL row limits but is not applicable for general Apex methods performing DML and resource-intensive actions in memory to avoid CPU limits or ensure transaction control.
Concept tested: Apex governor limits, transaction control, DML
Source: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_limits.htm
Topics
Community Discussion
No community discussion yet for this question.