erlang mnesia數(shù)據(jù)庫事物應(yīng)用

mneia數(shù)據(jù)庫時(shí)erlang自帶的一個(gè)nosql數(shù)據(jù)庫邓嘹,對(duì)其讀响驴、寫、刪除操作均有臟讀和非臟讀兩種性誉,之前存在一個(gè)誤區(qū)就是臟讀和非臟讀操作都支持transaction,在進(jìn)行代碼測(cè)試的時(shí)候出現(xiàn)和事物特性不一致的情況茎杂。

問題背景描述:

項(xiàng)目投產(chǎn)后错览,日常運(yùn)行中會(huì)產(chǎn)生大量的數(shù)據(jù),當(dāng)數(shù)據(jù)量不斷增加煌往,所占的內(nèi)存空間就會(huì)越大倾哺,對(duì)讀寫操作效率會(huì)有影響,而對(duì)于幾個(gè)月前的數(shù)據(jù)項(xiàng)目實(shí)際的運(yùn)行中基本用不到,但又不能將其直接刪除羞海,考慮將其遷移到一個(gè)備份表中闲勺,并將主表中的數(shù)據(jù)將其刪除】勖ǎ考慮到數(shù)據(jù)的安全性:將備份表的寫操作和主表的刪除操作,放入到事物中翘地,要么都成功申尤,要么都是失敗。同時(shí)為了保證項(xiàng)目性能衙耕,減少備份的時(shí)候昧穿,對(duì)數(shù)據(jù)庫的壓力,在進(jìn)行讀寫橙喘、刪除的時(shí)候采用diry的方式时鸵,在測(cè)試的時(shí)候發(fā)現(xiàn),dirty操作厅瞎,雖然加入到了事物饰潜,但是并不具事物的特性:同時(shí)成功同時(shí)失敗。

測(cè)試代碼

-module(test_mnesia).
-import(lists, [foreach/2]).
-include_lib("stdlib/include/qlc.hrl").
-compile(export_all).
-record(shop, {item, quantity, cost}).

do_this_once() ->
  mnesia:create_schema([node()]),
  mnesia:start(),
  mnesia:create_table(shop, [{attributes, record_info(fields, shop)}]),
  mnesia:stop().

start() ->
  mnesia:stop(),
  mnesia:start(),
  mnesia:wait_for_tables([shop], 2000),
  init_table(shop_info()).

get_all(TabelName) ->
  Qh =qlc:q([X || X <- mnesia:table(TabelName)]),
  F = fun() -> qlc:e(Qh) end,
  {atomic, Val} = mnesia:transaction(F),
  Val.

shop_info() ->
  [%% The shop table
    {shop, apple, 20, 2.3},
    {shop, orange, 100, 3.8},
    {shop, pear, 200, 3.6},
    {shop, banana, 420, 4.5}
  ].
init_table(TableData) ->
  F = fun(Model) -> mnesia:dirty_write(Model) end,
  lists:foreach(F, TableData).

delete(Item) ->
  Oid = {shop, Item},
  F = fun() ->
    mnesia:delete(Oid)
      end,
  mnesia:transaction(F).

dirty_delete(Item) ->
  Oid = {shop, Item},
  F = fun() ->
    mnesia:dirty_delete(Oid)
      end,
  mnesia:transaction(F).

write() ->
  Oid =  {shop, potato, 2456, 1.2},
  F = fun() ->
    mnesia:write(Oid)
      end,
  mnesia:transaction(F).

dirty_write() ->
  Oid =  {shop, potato, 2456, 1.2},
  F = fun() ->
    mnesia:dirty_write(Oid)
      end,
  mnesia:transaction(F).

execute(Function,Pram) when is_function(Function),is_list(Pram) ->
  F = fun() ->
    apply(Function,Pram)
    ,true = ok  %在事物里制造問題和簸,驗(yàn)證事物的有效性
      end,
  mnesia:transaction(F).
驗(yàn)證測(cè)試流程
  • 初始化操作
1> c(test_mnesia).
test_mnesia.erl:67: Warning: no clause will ever match
{ok,test_mnesia}
2> test_mnesia:do_this_once().
stopped
3>
=INFO REPORT==== 28-Sep-2017::18:19:14 ===
    application: mnesia
    exited: stopped
    type: temporary

3> test_mnesia:start().
ok
4>
  • delete測(cè)試流程:最總無法刪除數(shù)據(jù)里的數(shù)據(jù)
6>  test_mnesia:get_all(shop).                                               
[{shop,apple,20,2.3},                                                        
 {shop,orange,100,3.8},                                                      
 {shop,pear,200,3.6},                                                        
 {shop,banana,420,4.5}]                                                      
7> test_mnesia:execute(fun test_mnesia:delete/1,[apple]).                    
{aborted,{{badmatch,ok},                                                     
          [{test_mnesia,'-execute/2-fun-0-',2,                               
                        [{file,"test_mnesia.erl"},{line,67}]},               
           {mnesia_tm,apply_fun,3,[{file,"mnesia_tm.erl"},{line,836}]},      
           {mnesia_tm,execute_transaction,5,                                 
                      [{file,"mnesia_tm.erl"},{line,811}]},                  
           {erl_eval,do_apply,6,[{file,"erl_eval.erl"},{line,674}]},         
           {shell,exprs,7,[{file,"shell.erl"},{line,686}]},                  
           {shell,eval_exprs,7,[{file,"shell.erl"},{line,641}]},             
           {shell,eval_loop,3,[{file,"shell.erl"},{line,626}]}]}}            
8> test_mnesia:get_all(shop).                                                
[{shop,apple,20,2.3},                                                        
 {shop,orange,100,3.8},                                                      
 {shop,pear,200,3.6},                                                        
 {shop,banana,420,4.5}]                                                      
  • dirty_delete測(cè)試流程:apple的記錄將會(huì)被刪除
8> test_mnesia:get_all(shop).
[{shop,apple,20,2.3},
 {shop,orange,100,3.8},
 {shop,pear,200,3.6},
 {shop,banana,420,4.5}]
9> test_mnesia:execute(fun test_mnesia:dirty_delete/1,[apple]).
{aborted,{{badmatch,ok},
          [{test_mnesia,'-execute/2-fun-0-',2,
                        [{file,"test_mnesia.erl"},{line,67}]},
           {mnesia_tm,apply_fun,3,[{file,"mnesia_tm.erl"},{line,836}]},
           {mnesia_tm,execute_transaction,5,
                      [{file,"mnesia_tm.erl"},{line,811}]},
           {erl_eval,do_apply,6,[{file,"erl_eval.erl"},{line,674}]},
           {shell,exprs,7,[{file,"shell.erl"},{line,686}]},
           {shell,eval_exprs,7,[{file,"shell.erl"},{line,641}]},
           {shell,eval_loop,3,[{file,"shell.erl"},{line,626}]}]}}
10> test_mnesia:get_all(shop).
[{shop,orange,100,3.8},
 {shop,pear,200,3.6},
 {shop,banana,420,4.5}]
  • write測(cè)試流程:{shop, potato, 2456, 1.2}不能寫入數(shù)據(jù)庫
10> test_mnesia:get_all(shop).                                                         
[{shop,orange,100,3.8},                                                                
 {shop,pear,200,3.6},                                                                  
 {shop,banana,420,4.5}]                                                                
11> test_mnesia:execute(fun test_mnesia:write/0,[]).                                   
{aborted,{{badmatch,ok},                                                               
          [{test_mnesia,'-execute/2-fun-0-',2,                                         
                        [{file,"test_mnesia.erl"},{line,67}]},                         
           {mnesia_tm,apply_fun,3,[{file,"mnesia_tm.erl"},{line,836}]},                
           {mnesia_tm,execute_transaction,5,                                           
                      [{file,"mnesia_tm.erl"},{line,811}]},                            
           {erl_eval,do_apply,6,[{file,"erl_eval.erl"},{line,674}]},                   
           {shell,exprs,7,[{file,"shell.erl"},{line,686}]},                            
           {shell,eval_exprs,7,[{file,"shell.erl"},{line,641}]},                       
           {shell,eval_loop,3,[{file,"shell.erl"},{line,626}]}]}}                      
12>  test_mnesia:get_all(shop).                                                        
[{shop,orange,100,3.8},                                                                
 {shop,pear,200,3.6},                                                                  
 {shop,banana,420,4.5}]                                                                
  • dirty_write測(cè)試流程:{shop, potato, 2456, 1.2}可以寫入數(shù)據(jù)庫
12>  test_mnesia:get_all(shop).
[{shop,orange,100,3.8},
 {shop,pear,200,3.6},
 {shop,banana,420,4.5}]
13> test_mnesia:execute(fun test_mnesia:dirty_write/0,[]).
{aborted,{{badmatch,ok},
          [{test_mnesia,'-execute/2-fun-0-',2,
                        [{file,"test_mnesia.erl"},{line,67}]},
           {mnesia_tm,apply_fun,3,[{file,"mnesia_tm.erl"},{line,836}]},
           {mnesia_tm,execute_transaction,5,
                      [{file,"mnesia_tm.erl"},{line,811}]},
           {erl_eval,do_apply,6,[{file,"erl_eval.erl"},{line,674}]},
           {shell,exprs,7,[{file,"shell.erl"},{line,686}]},
           {shell,eval_exprs,7,[{file,"shell.erl"},{line,641}]},
           {shell,eval_loop,3,[{file,"shell.erl"},{line,626}]}]}}
14>  test_mnesia:get_all(shop).
[{shop,potato,2456,1.2},
 {shop,orange,100,3.8},
 {shop,pear,200,3.6},
 {shop,banana,420,4.5}]

總結(jié):從上面的測(cè)試可以看出彭雾,mnesia數(shù)據(jù)庫關(guān)于dirty的操作不支持事物。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末锁保,一起剝皮案震驚了整個(gè)濱河市薯酝,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌爽柒,老刑警劉巖吴菠,帶你破解...
    沈念sama閱讀 207,113評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異浩村,居然都是意外死亡做葵,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,644評(píng)論 2 381
  • 文/潘曉璐 我一進(jìn)店門心墅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蜂挪,“玉大人,你說我怎么就攤上這事嗓化√匿蹋” “怎么了?”我有些...
    開封第一講書人閱讀 153,340評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵刺覆,是天一觀的道長(zhǎng)严肪。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么驳糯? 我笑而不...
    開封第一講書人閱讀 55,449評(píng)論 1 279
  • 正文 為了忘掉前任篇梭,我火速辦了婚禮,結(jié)果婚禮上酝枢,老公的妹妹穿的比我還像新娘恬偷。我一直安慰自己,他們只是感情好帘睦,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,445評(píng)論 5 374
  • 文/花漫 我一把揭開白布袍患。 她就那樣靜靜地躺著,像睡著了一般竣付。 火紅的嫁衣襯著肌膚如雪诡延。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,166評(píng)論 1 284
  • 那天古胆,我揣著相機(jī)與錄音肆良,去河邊找鬼。 笑死逸绎,一個(gè)胖子當(dāng)著我的面吹牛惹恃,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播棺牧,決...
    沈念sama閱讀 38,442評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼座舍,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了陨帆?” 一聲冷哼從身側(cè)響起曲秉,我...
    開封第一講書人閱讀 37,105評(píng)論 0 261
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎疲牵,沒想到半個(gè)月后承二,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,601評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡纲爸,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,066評(píng)論 2 325
  • 正文 我和宋清朗相戀三年亥鸠,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片识啦。...
    茶點(diǎn)故事閱讀 38,161評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡负蚊,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出颓哮,到底是詐尸還是另有隱情家妆,我是刑警寧澤,帶...
    沈念sama閱讀 33,792評(píng)論 4 323
  • 正文 年R本政府宣布冕茅,位于F島的核電站伤极,受9級(jí)特大地震影響蛹找,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜哨坪,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,351評(píng)論 3 307
  • 文/蒙蒙 一庸疾、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧当编,春花似錦届慈、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,352評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至牵舱,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間缺虐,已是汗流浹背芜壁。 一陣腳步聲響...
    開封第一講書人閱讀 31,584評(píng)論 1 261
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留高氮,地道東北人慧妄。 一個(gè)月前我還...
    沈念sama閱讀 45,618評(píng)論 2 355
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像剪芍,于是被迫代替她去往敵國(guó)和親塞淹。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,916評(píng)論 2 344

推薦閱讀更多精彩內(nèi)容