2012-04-23 8 views
1

2つのテーブルが関連しており、これらの2つのテーブル内のすべてのレコードを削除する関数を書きたいと思いますが、出力ではできません。レコードを1つずつ削除する効率の低い選択肢が唯一の選択肢ですか?2つの関連テーブルのすべてのレコードをトランザクションで一緒に削除する方法は?

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) 
+0

「execute_mnesia_transaction」関数を表示する必要があります。 –

+0

私はソースコードを更新しました。 –

答えて

1

mnesia:clear_table/1がスキーマ取引に分類されているので、他のトランザクションにネストすることはできません。

cf. mnesia:clear_table http://erlang.org/pipermail/erlang-questions/2005-August/016582.html

+0

ありがとうございます。シェルの出力から、私はすでにこれを知っています。私はもっ​​と良い解決策を知りたい。 –

+0

同じディスカッションスレッドでは、1つの 'schema_transaction'で複数のテーブルを消去する方法(いくつかの余分なコードを使用して、間違いなく)http://erlang.org/pipermail/erlang-questions/2005-August/016594.htmlとコメントhttp ://erlang.org/pipermail/erlang-questions/2005-August/016607.html mnesiaの開発者から、それを行う直接的な方法がないこと、そして上記のアプローチが何を意味するのか。まだ動作します(R15B)。 –

関連する問題