2012-04-23 1 views
1

두 개의 테이블이 관련되어 있으며이 두 테이블의 모든 레코드를 삭제하는 함수를 작성하려고하지만 결과로 출력 할 수 없다는 것을 나타냅니다. 레코드를 하나씩 삭제하는 낮은 효율적인 선택이 유일한 선택입니까?두 개의 관련 테이블에있는 모든 레코드를 트랜잭션에서 함께 삭제하는 방법은 무엇입니까?

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

동일한 토론 스레드는 하나의'schema_transaction' http://erlang.org/pipermail/erlang-questions/2005-August/016594.html에있는 여러 테이블을 정리하는 주석 (일부 추가 코드로, 인정할 만하게)을 설명하고 주석 http : //erlang.org/pipermail/erlang-questions/2005-Austust/016607.html mnesia 개발자는 왜 그렇게 할 수있는 직접적인 방법이 없으며 위의 접근 방식에 어떤 의미가 있는지 설명합니다. 아직도 작동합니다 (R15B 포함). –

관련 문제