如何在事务中一起删除两个相关表中的所有记录?

时间:2012-04-23 10:40:49

标签: erlang mnesia

两个表是相关的,我想编写函数来删除这两个表中的所有记录,但输出表明我不能这样做。删除记录的低效率选择是唯一可行的选择吗?

clear_gyne()->
R = execute_mnesia_transaction(
 fun()->
    mnesia:clear_table(bas_gyne),
    mnesia:clear_table(bas_gyne_property)
end),
R.

execute_mnesia_transaction(TxFun) ->
    %% Making this a sync_transaction allows us to use dirty_read
    %% elsewhere and get a consistent result even when that read
    %% executes on a different node.
    %%    case worker_pool:submit(
    %%    fun () ->
    Result_a = case mnesia:is_transaction() of
                         false -> DiskLogBefore = mnesia_dumper:get_log_writes(),
                                  Res = mnesia:sync_transaction(TxFun),
                                                     DiskLogAfter  = mnesia_dumper:get_log_writes(),
                                  case DiskLogAfter == DiskLogBefore of
                                      true  -> Res;
                                      false -> {sync, Res}
                                  end;
                         true  -> mnesia:sync_transaction(TxFun)
                     end,
    case Result_a of
        {sync, {atomic,  Result}} -> mnesia_sync:sync(), Result;
        {sync, {aborted, Reason}} -> throw({error, Reason});
        {atomic,  Result}         -> Result;
        {aborted, Reason}         -> throw({error, Reason})
   end.

execute_mnesia_transaction是从rabbitmq项目的源代码中复制的。

输出

bas_store:clear_gyne().
** exception throw: {error,{aborted,nested_transaction}}
     in function  bas_store:execute_mnesia_transaction/1 (src/bas_store.erl, line 29)

1 个答案:

答案 0 :(得分:1)

mnesia:clear_table/1被分类为模式事务,因此不能嵌套在另一个事务中。

比照Mnesia的:clear_table http://erlang.org/pipermail/erlang-questions/2005-August/016582.html