事務(wù)是數(shù)據(jù)庫(kù)保證數(shù)據(jù)唯一性和一致性的技術(shù),對(duì)于數(shù)據(jù)庫(kù)一個(gè)或一組寫操作要保證是一個(gè)原子操作就需要使用事務(wù),android使用事務(wù)的常見形式如下:
創(chuàng)新互聯(lián)建站擁有十多年成都網(wǎng)站建設(shè)工作經(jīng)驗(yàn),為各大企業(yè)提供網(wǎng)站制作、成都做網(wǎng)站服務(wù),對(duì)于網(wǎng)頁(yè)設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、重慶App定制開發(fā)、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、程序開發(fā)、網(wǎng)站優(yōu)化(SEO優(yōu)化)、微網(wǎng)站、域名注冊(cè)等,憑借多年來(lái)在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了很多網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營(yíng)銷經(jīng)驗(yàn),集策劃、開發(fā)、設(shè)計(jì)、營(yíng)銷、管理等網(wǎng)站化運(yùn)作于一體,具備承接各種規(guī)模類型的網(wǎng)站建設(shè)項(xiàng)目的能力。
SQLiteDatabase db = null; ... db.beginTransaction(); try { db.setTransactionSuccessful(); ... } finally { db.endTransaction(); }
那么db.beginTransaction是一個(gè)什么操作? 我們來(lái)看下SQLiteDatabase的源碼:
/** * Begins a transaction in EXCLUSIVE mode. * <p> * Transactions can be nested. * When the outer transaction is ended all of * the work done in that transaction and all of the nested transactions will be committed or * rolled back. The changes will be rolled back if any transaction is ended without being * marked as clean (by calling setTransactionSuccessful). Otherwise they will be committed. */ public void beginTransaction() { beginTransaction(null /* transactionStatusCallback */, true); } /** * Begins a transaction in IMMEDIATE mode. *Transactions can be nested. When * the outer transaction is ended all of the work done in that transaction * and all of the nested transactions will be committed or rolled back. The * changes will be rolled back if any transaction is ended without being * marked as clean (by calling setTransactionSuccessful). Otherwise they * will be committed. */ public void beginTransactionNonExclusive() { beginTransaction(null /* transactionStatusCallback */, false); }
從注釋中可以看到beginTransaction的調(diào)用使用EXCLUSIVE mode, beginTransactionNonExclusive使用IMMEDIATE mode,以上兩個(gè)方法都是調(diào)用SQLiteDatabase的私有方法beginTransaction,兩個(gè)方法不同之處在于第二個(gè)實(shí)參true|false, 這個(gè)私有方法源碼:
private void beginTransaction(SQLiteTransactionListener transactionListener, boolean exclusive) { verifyDbIsOpen(); lockForced(BEGIN_SQL); boolean ok = false; try { ... // This thread didn't already have the lock, so begin a database // transaction now. if (exclusive && mConnectionPool == null) { execSQL("BEGIN EXCLUSIVE;"); } else { execSQL("BEGIN IMMEDIATE;"); } ... } finally { if (!ok) { // beginTransaction is called before the try block so we must release the lock in // the case of failure. unlockForced(); } } }
當(dāng)形參exclusive為true并且mConnectionPool==null是執(zhí)行:execSQL("BEGIN EXCLUSIVE;"); false執(zhí)行execSQL("BEGIN IMMEDIATE;");
BEGIN EXCLUSIVE:當(dāng)前事務(wù)在沒有結(jié)束之前任何android中的其他線程或進(jìn)程都無(wú)法對(duì)數(shù)據(jù)庫(kù)進(jìn)行讀寫操作。
BEGIN IMMEDIATE:確保android中其他線程或者進(jìn)程之間讀取數(shù)據(jù)不能修改數(shù)據(jù)庫(kù)。
為什么需要判斷mConnectionPool==null這個(gè)條件,如果當(dāng)mConnectionPool!=null 表示調(diào)用了enableWriteAheadLogging,也就是使用了WAL MODE。 使用WAL模式是能夠提高并發(fā)性,讀與寫互不阻塞,而執(zhí)行BEGIN EXCLUSIVE卻降低了并發(fā),互相矛盾,所以當(dāng)以上兩個(gè)條件都成立的情況下執(zhí)行BEGIN EXCLUSIVE。
新聞標(biāo)題:Android事務(wù)IMMEDIATE與EXCLUSIVE模式
URL分享:http://www.rwnh.cn/article32/jsdjpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、商城網(wǎng)站、做網(wǎng)站、全網(wǎng)營(yíng)銷推廣、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)