high reliability dml and concurrency design patterns for apex

Post on 23-Jan-2015

782 Views

Category:

Technology

6 Downloads

Preview:

Click to see full reader

DESCRIPTION

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.

TRANSCRIPT

High reliability DML and concurrency design patterns for Apex

Dan Appleman, Full Circle CRM, CTOAuthor: Advanced Apex Programming for Salesforce.com and Force.com@danappleman

Safe HarborSafe 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.

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.

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: []

Or

Concurrency ProblemsThe 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

Most of us learn about concurrency errors as children…

Dad, can we go to the party?

Let me think: NO!

Most of us learn about concurrency errors as children…

Dad, can we go to the party?

Let me think: NO!

Mom can we go tothe party?

15 seconds later

Sure, why not?

… or as parents

Dad, can we go to the party?

Let me check withyour mom

Mom can we go tothe party?

What did your dad say?

Pessimistic locking

Optimistic locking

…or in Salesforce

Two Salespeople receive simultaneous instructions from two different contacts on an opportunity…

This would be very bad

…or in Salesforce

Two Salespeople receive simultaneous instructions from two different contacts on an opportunity…

Salesforce detects theother users’ modification

Optimistic locking (so to speak)

APEX DOES NOT DO THIS!

…or in Salesforce

Two Salespeople receive simultaneous instructions from two different contacts on an opportunity…

SOQL “For Update” QueryPessimistic locking

Block another thread > 10 seconds = UNABLE_TO_LOCK_ROW error

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

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

Reproducing Lock Errors▪ Use future calls and anonymous Apex

▪ You can simulate them in unit tests

Handling Lock Errors▪ In triggers and VisualForce

▪ Let the user deal with them

▪ In asynchronous code▪ Logging▪ Recovery

For More InformationAdvanced 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

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

Download sample code for this session atwww.AdvancedApex.com/Dreamforce13

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

top related