用try / catch意外拦截Mnesia的事务重试会导致各种怪异 [英] Unintentionally intercepting Mnesia's transactional retries with try/catch results in all kinds of weirdness

查看:128
本文介绍了用try / catch意外拦截Mnesia的事务重试会导致各种怪异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我在一次交易中对记录集进行CRUD操作时遇到各种麻烦。它使我在此处发布了两个问题, MoreTrouble 但是,我认为这两个问题都是由以下原因造成的:在我的交易中,我将mnesia:writes,reads等包含在try / catch块中,捕获了包括mnesia被中止的所有内容即,

So, I was having all kinds of trouble with CRUD operations on sets of records in one transaction. It lead me to post 2 questions here, Trouble and MoreTrouble. However, I think that both those issues where created by the following: Within my transactions, I enclosed my mnesia:writes, reads, etc. in try/catch blocks that caught everything including mnesia's aborted transactions as part of its deadlock avoidance algorithm. I.e.,

insert(Key, Value) ->
   F = 
      fun() ->
         case sc_store:lookup(Key) of
           {ok, _Value}       -> sc_store:replace(Key, Value);
           {error, not_found} -> sc_store:insert(Key,Value)
         end
       end,
   try
      case mnesia:transaction(F) of
         {atomic, Result}  -> Result;
         {aborted, Reason} -> ...
      end
    catch
        Error:Reason -> ...
    end

结束

sc:lookup / 1如下:

sc:lookup/1, for example, looked like this:

lookup(Key) ->
   try 
      case mnesia:read(key_to_value, Key) of
         [#key_to_value{type = Type, scope = Scope, value = Value}] -> 
            {ok, {Value, Type, Scope}};
         []                       -> 
            {error, not_found}
      end
   catch
      _Err:Reason -> {error, Reason}
   end.

认为我一定是在拦截 /捕捉到了失忆症,锁定避免算法,而不是让它按设计重试。

I think I must have been "intercepting" / catching mnesia's dead-lock avoidance algorithm instead of letting it retry as designed.

有可能吗?如果是这样,那么它对于像我这样的新蜜蜂来说是一个(& ^&。)如果不是,那么为什么此代码会对我产生很多问题的任何想法,但要删除mnesia的try / catch:read等。函数可以解决我所有的问题吗?

Is that possible? If so, its a (&^& of a gotch-a for a newbee like me. If not, any ideas why this code produced so many problems for me, but removing the try/catch from the mnesia:read, etc. functions cleared up all of my problems?

推荐答案

是的,我不确定是否在任何地方都对此进行了正确记录,但是您应该不要掩盖mnesia操作中的异常。如果这样做,即使某些操作实际上根本不起作用,它似乎也使您的交易乐趣按预期工作了。

Yes, I'm not sure if that's properly documented anywhere, but you should not mask out the exceptions in mnesia operations. If you do that, it looks to mnesia like your transaction fun worked as intended, even though some operations actually didn't work at all.

这篇关于用try / catch意外拦截Mnesia的事务重试会导致各种怪异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆