High Reliability DML and Concurrency Design Patterns for Apex


Published on

Join us to learn several strategies for dealing with concurrency errors like the UNABLE_TO_LOCK_ROW error in Apex . We'll also review design patterns that can fail gracefully, and even recover silently when these errors occur ? all without compromising your data integrity.

Published in: Technology, Business
1 Like
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

High Reliability DML and Concurrency Design Patterns for Apex

  1. 1. High reliability DML and concurrency design patterns for Apex Dan Appleman, Full Circle CRM, CTO Author: Advanced Apex Programming for Salesforce.com and Force.com @danappleman
  2. 2. Safe Harbor Safe harbor statement under the Private Securities Litigation Reform Act of 1995: This presentation may contain forward-looking statements that involve risks, uncertainties, and assumptions. If any such uncertainties materialize or if any of the assumptions proves incorrect, the results of salesforce.com, inc. could differ materially from the results expressed or implied by the forward-looking statements we make. All statements other than statements of historical fact could be deemed forward-looking, including any projections of product or service availability, subscriber growth, earnings, revenues, or other financial items and any statements regarding strategies or plans of management for future operations, statements of belief, any statements concerning new, planned, or upgraded services or technology developments and customer contracts or use of our services. The risks and uncertainties referred to above include – but are not limited to – risks associated with developing and delivering new functionality for our service, new products and services, our new business model, our past operating losses, possible fluctuations in our operating results and rate of growth, interruptions or delays in our Web hosting, breach of our security measures, the outcome of any litigation, risks associated with completed and any possible mergers and acquisitions, the immature market in which we operate, our relatively limited operating history, our ability to expand, retain, and motivate our employees and manage our growth, new releases of our service and successful customer deployment, our limited history reselling non-salesforce.com products, and utilization and selling to larger enterprise customers. Further information on potential factors that could affect the financial results of salesforce.com, inc. is included in our annual report on Form 10-K for the most recent fiscal year and in our quarterly report on Form 10-Q for the most recent fiscal quarter. These documents and others containing important disclosures are available on the SEC Filings section of the Investor Information section of our Web site. Any unreleased services or features referenced in this or other presentations, press releases or public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make the purchase decisions based upon features that are currently available. Salesforce.com, inc. assumes no obligation and does not intend to update these forward-looking statements.
  3. 3. Have you ever seen… EXCEPTION_THROWN [32]|System.QueryException: Record Currently Unavailable: The record you are attempting to edit, or one of its related records, is currently being modified by another user. Please try again. Or FATAL_ERROR System.DmlException: Update failed. First exception on row 0 with id ……………; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: []
  4. 4. Concurrency Problems The most dangerous kind of bug in all of computer science. Why? Because concurrency problems and bugs can be: ▪Virtually impossible to detect ▪Virtually impossible to reproduce
  5. 5. Most of us learn about concurrency errors as children… Dad, can we go to the party? Let me think: NO!
  6. 6. Most of us learn about concurrency errors as children… Dad, can we go to the party? Let me think: NO! 15 seconds later Mom can we go to the party? Sure, why not?
  7. 7. … or as parents Dad, can we go to the party? Let me check with your mom Pessimistic locking Mom can we go to the party? What did your dad say? Optimistic locking
  8. 8. …or in Salesforce Two Salespeople receive simultaneous instructions from two different contacts on an opportunity… This would be very bad
  9. 9. …or in Salesforce Two Salespeople receive simultaneous instructions from two different contacts on an opportunity… Salesforce detects the other users’ modification Optimistic locking (so to speak) APEX DOES NOT DO THIS!
  10. 10. …or in Salesforce Two Salespeople receive simultaneous instructions from two different contacts on an opportunity… SOQL “For Update” Query Pessimistic locking Block another thread > 10 seconds = UNABLE_TO_LOCK_ROW error
  11. 11. Common Lock Conditions ▪ Data skew ▪ Insert or owner change can lock a parent during sharing calculations ▪ After a record update ▪ Records have to be locked to allow rollback in case of an exception ▪ “For Update” query ▪ Explicit lock
  12. 12. Common Lock Solutions ▪ Data skew ▪ Reassign record ownership ▪ After a record update ▪ Defer record updates to the end of the execution context ▪ “For Update” query ▪ Avoid or delay where possible ▪ You cannot absolutely prevent these errors ▪ More common on high traffic systems with lots of code
  13. 13. Reproducing Lock Errors ▪ Use future calls and anonymous Apex ▪ You can simulate them in unit tests
  14. 14. Handling Lock Errors ▪ In triggers and VisualForce ▪ Let the user deal with them ▪ In asynchronous code ▪ Logging ▪ Recovery
  15. 15. For More Information Advanced Apex Programming for Salesforce.com and Force.com – Second Edition • • • • • Incorporates changes through Winter 14 Designing for CPU Time limits Extended chapter on triggers Extended chapter on asynchronous patterns New chapter on concurrency patterns
  16. 16. All About Full Circle CRM Full Circle CRM develops native Salesforce.com applications that enable marketers to accurately track and measure marketing campaign performance directly in Salesforce ▪ Bridge the information gap between leads and contacts ▪ Obtain complete and detailed campaign performance metrics ▪ Develop advanced campaign attribution and influence analytics Booth 1324 Moscone North
  17. 17. Download sample code for this session at www.AdvancedApex.com/Dreamforce13
  18. 18. We want to hear from YOU! Please take a moment to complete our session survey Surveys can be found in the “My Agenda” portion of the Dreamforce app
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.