5
votes

What is the difference between a series of mnesia:dirty_ commands executed within a function passed to mnesia:async_dirty() and those very same transactions executed "raw"?

I.e., is there any difference between doing:

mnesia:dirty_write({table, Rec1}),
mnesia:dirty_write({table, Rec1}),
mnesia:dirty_write({table, Rec1})

and

F = fun() ->
        mnesia:dirty_write({table, Rec1}),
        mnesia:dirty_write({table, Rec1}),
        mnesia:dirty_write({table, Rec1})
   end,

   mnesia:async_dirty(F)

Thanks

1

1 Answers

-1
votes

Lets first quote the Users' Guide on async_dirty context:

By passing the same "fun" as argument to the function 
mnesia:async_dirty(Fun [, Args]) it will be performed in dirty context.
The function calls will be mapped to the corresponding dirty functions.
This will still involve logging, replication and subscriptions but there will be
no locking, local transaction storage or commit protocols involved. Checkpoint
retainers will be updated but will be updated "dirty". Thus, they will be updated
asynchronously. The functions will wait for the operation to be performed on one
node but not the others. If the table resides locally no waiting will occur.

The two options you have provided will be executed the same way. However, when you execute the dirty functions out of the fun as in option one, each one is a separate call into mnesia. With async_dirty, the 3 calls will be bundled and mnesia will only wait until the 3 are completed on the local node to return.
However, the behavior of these two may differ in a mnesia multi-node cluster. Do some tests :)