@Future at risk for race conditions?

Question

If I invoke an @future method from a trigger that is performing a calculation based on data that is being committed in the trigger am I at a risk for a race condition?

Background

We’re implementing a complex rollup calculation that we’d like to be real time (or near real-time). Specifically we’re rolling up the latest activity date for tasks across and account hierarchy segmenting by the type of account. We can’t use the standard LastActivityDate field since we need to exclude some auto-generated tasks. To calculate this date is a pretty intensive query so we don’t want to hold up the task commit while the calculation takes place. However, if the rollup calculation occurs before the trigger fully commits the data will be incorrect. Is this a realistic concern?

Answer

@Future’s are queued transactionally along with other db changes in the trigger transaction, so the future can’t be executed until the enqueuing transaction commits. (and conversely, if the trigger transaction never commits due to errors etc, the @future is never executed)

Attribution
Source : Link , Question Author : Ralph Callaway , Answer Author : superfell

Leave a Comment