本篇文章給大家分享的是有關(guān)weed3-5中怎么實現(xiàn)事務(wù)和事務(wù)隊列,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
成都創(chuàng)新互聯(lián)公司專注于長垣網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供長垣營銷型網(wǎng)站建設(shè),長垣網(wǎng)站制作、長垣網(wǎng)頁設(shè)計、長垣網(wǎng)站官網(wǎng)定制、微信平臺小程序開發(fā)服務(wù),打造長垣網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供長垣網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
1.事務(wù)(主要用于單個庫)
//demo1:: //事務(wù)組 // 在一個事務(wù)里,做4個插入//如果出錯了,自動回滾 DbUserApi dbUserApi = XmlSqlProxy.getSingleton(DbUserApi.class); db.tran((t) -> { // // 此表達式內(nèi)的操作,會自動加入事務(wù) // //sql接口 db.sql("insert into test(txt) values(?)", "cc").insert(); db.sql("update test set txt='1' where id=1").execute(); //call接口 db.call("user_del").set("_user_id",10).execute(); //table()接口 db.table("a_config").set("cfg_id",1).insert(); //xml mapper dbUserApi.user_add(12); //大家使用統(tǒng)一的事務(wù)模式 });
2.事務(wù)隊列(主要用于多個庫的情況)
//demo2:: //事務(wù)隊列 // //假如,要跨兩個數(shù)據(jù)庫操作(一個事務(wù)對象沒法用了) // DbContext db = DbConfig.pc_user; DbContext db2 = DbConfig.pc_base; //創(chuàng)建個事務(wù)隊列(和傳統(tǒng)概念的隊列不一樣) DbTranQueue queue = new DbTranQueue(); //數(shù)據(jù)庫1的事務(wù) db.tran().join(queue).execute((t) => { // // 在這個表達示內(nèi),會自動加入事物 // db.sql("insert into test(txt) values(?)", "cc").execute(); db.sql("insert into test(txt) values(?)", "dd").execute(); db.sql("insert into test(txt) values(?)", "ee").execute(); }); //數(shù)據(jù)庫2的事務(wù) db2.tran().join(queue).execute((t) => { // // 在這個表達示內(nèi),會自動加入事物 // db2.sql("insert into test(txt) values(?)", "gg").execute(); }); //隊列結(jié)組完成(即開始跑事務(wù)) queue.complete();
3.事務(wù)隊列的加強版,跨函數(shù)或模塊跑事務(wù)
public void test_main(){ DbTranQueue queue = new DbTranQueue(); test1(queue); test2(queue); } public void test1(DbTranQueue queue){ DbTran tran = DbConfig.db1.tran();//生成個事務(wù)對象 tran.join(queue).execute((t) -> { // // 在這個表達示內(nèi),會自動加入事物 // t.db().sql("insert into $.test(txt) values(?)", "cc").insert(); t.db().sql("insert into $.test(txt) values(?)", "dd").execute(); t.db().sql("insert into $.test(txt) values(?)", "ee").execute(); t.result = t.db().sql("select name from $.user_info where user_id=3").getValue(""); }); } public void test2(DbTranQueue queue){ //...test2就不寫了 }
以上就是weed3-5中怎么實現(xiàn)事務(wù)和事務(wù)隊列,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站欄目:weed3-5中怎么實現(xiàn)事務(wù)和事務(wù)隊列
URL標(biāo)題:http://aaarwkj.com/article12/iijggc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、搜索引擎優(yōu)化、網(wǎng)站導(dǎo)航、網(wǎng)頁設(shè)計公司、微信小程序、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)