Workflow Foundation and locking

Locking in Windows Workflow Foundation wasn’t very clear in my mind until I made a few experimentations.
 
  1. If you set the unloadOnIdle parameter of SqlWorkflowPersistence service as true and your workflow becomes idle , the workflow will be persisted and unloaded.
  2. When a workflow is unloaded it’s unlocked.
  3. If your workflow is reloaded, it will be locked.
 
Locking durating
================
Locking duration depends on the thirth and fourth parameter of SqlWorkFlowPersistence constructor; for instance in the following case:
 
new SqlWorkflowPersistenceService
  (connectionString,true, new TimeSpan(0,1,0),new TimeSpan(0,0,5));
 
your workflow will remains locked for 1 minute; the system will check
the db every 5 seconds.
 
For example: app1 starts and the first activity is an DelayActivity
=>the workflow is persisted and unloaded;
if we take a look at the stateinstance table of our persistence db, will notice that the ownerID is null =>the workflow is unlocked.
After the idle period the workflow is automaticallly reloaded.
Now if we take a look at the stateinstance table , we will notice that the ownerID is not null and that the ownedUntil column as been set to now + 1 minute.SqlPersistence services takes care of setting the ownerid.
By using SQLProfiler while the workflow is in the database,  we can also notice that the stored procedure RetrieveExpiredTimerIds
checks the StateInstance  table every 5 seconds (Indeed : we ‘ve specified TimeSpan(0,0,5)).
 
If another host application (app2) tries to load a locked workflow , an WorkflowOwnerException will be triggered.
 
In summary:
 
1°workflow unloaded: workflow unlocked in the persistence store.
2°workflow reloaded: workflow locked in the persistence store.
3°locking happens in the StateInstance table of your persistence DB.
 

Leave a comment