Android transaction immediate and exclusive mode

Source: Internet
Author: User

A transaction is a technique that ensures data uniqueness and consistency in a database, and for a database or a set of writes to ensure that an atomic operation is required to use transactions, the common form of Android usage transactions is as follows:


Sqlitedatabase db = null, ... db.begintransaction (); try {db.settransactionsuccessful (); ...} finally {db.endtransaction ();}


So what's the operation of Db.begintransaction? Let's look at the source code of 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);     }

You can see from the comments that the BeginTransaction call uses exclusive mode, Begintransactionnonexclusive uses immediate mode, The above two methods are called Sqlitedatabase Private method BeginTransaction, two methods differ in the second argument True|false, the private method source code:


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 ();            }         }    }

When the formal parameter EXCLUSIVE is true and Mconnectionpool==null is executed: Execsql ("BEGIN EXCLUSIVE;"); False execution Execsql ("BEGIN IMMEDIATE;");


BEGIN EXCLUSIVE: The current transaction cannot read or write to the database until the end of any other thread or process in Android.
BEGIN IMMEDIATE: Make sure that reading data between other threads or processes in Android cannot modify the database.

Why you need to judge mconnectionpool==null this condition, if when mconnectionpool!=null means call enablewriteaheadlogging, that is, using the Wal MODE. The use of the Wal mode is to improve concurrency, read and write non-blocking, while the implementation of begin EXCLUSIVE reduced concurrency, contradiction, so when the above two conditions are established in the case of the implementation of Begin EXCLUSIVE.


This article is from the "Itlife" blog, make sure to keep this source http://xwteacher.blog.51cto.com/9653444/1584555

Android Transaction immediate and exclusive mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.