Mnesia: unexpectedly getting aborted, cyclic transactions

匆匆过客 提交于 2019-12-04 11:57:41
Jr0

Actually, turns out the problem was using try/catch around mnesia operations within a transaction. See here for more.

Mnesia is right !!
According to your code, the function insert_keylist/1 calls the function transactional_execute/1 with a fun. Inside the function: transactional_execute/1, you ask mnesia wether its already in a transaction, and if its true, you execute the fun F. However, looking closely in fun F, you see that it calls the function: transactional_execute/1 all over again through the iterative function: insert_iter/1. So, this creates a loop of transactions. Hence your problem is arising because a process is executing the top most function: insert_keylist/1 in a transaction which is creating a nested loop of transactions which never get executed, only, they keep asking mnesia wether they are in a transaction and it accepts yet again they keep asking and asking and looping without ever getting executed !!

Before we modify your code, we have not seen whats in the functions: sc_store:replace/2 and sc_store:insert/1. I will assume that these functions are again NOT creating transactions within themselves!! I will assume that they are directly using mnesia functions (That must be executed within a transaction), such as : mnesia:write/1, mnesia:read/1,mnesia:write/3 e.t.c. Otherwise if you were creating another transaction in these functions, surely this would be a mess of mnesia transactions!! Now lets correct the code as shown below:

%% This function will always be sure to be in a 
%% mnesia transaction. So no need to call
%% transactional execute

insert_iter([])-> ok;
insert_iter([{Key, Value} | KVs]) ->
   case sc_store:lookup(Key) of
        {ok, _Value} -> sc_store:replace(Key, Value);
        {error, not_found} -> sc_store:insert(Key,Value)
    end,
    insert_iter(Kvs).

That's the only change required, assuming that the functions sc_store:insert/1 and sc_store:replace/2 access mnesia write and read functions directly without creating other transactions also.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!