Transactions for the Android database

Source: Internet
Author: User

What is a transaction for a database

A transaction (Transaction) is a program execution unit (unit) that accesses and possibly updates various data items in a database. Transactions are usually caused by the execution of user programs written in the advanced Database manipulation language or programming language, and are defined using the form transaction and end transaction statements (or function calls). A transaction consists of all operations performed between the start of a transaction and the end of a transaction.

Characteristics

Transactions are the basic unit of recovery and concurrency control.

A transaction should have 4 properties: atomicity, consistency, isolation, persistence. These four properties are often called acid properties.

Atomicity (atomicity). A transaction is an inseparable unit of work, and the operations included in the transaction are either done or not.

Consistency (consistency). The transaction must be to change the database from one consistency state to another. Consistency is closely related to atomicity.

Isolation (Isolation). Execution of one transaction cannot be disturbed by other transactions. That is, the operations inside a transaction and the data used are isolated from other transactions that are concurrently executing, and cannot interfere with each other concurrently.

Persistence (durability). When a transaction is committed, its changes to the data in the database should be permanent. The next operation or failure should not have any effect on it.

    db.begintransaction ();     Try  {        ...        Db.settransactionsuccessful ();     Catch (Exception e) {        Toast.maketext (This, "produces an exception, transaction rollback", 0). Show ();      finally {        db.endtransaction ();    }
1. Modify the Database

Add a list of account accounts to the PERSON.DB database you created earlier to record the accounting

     Public void int int newversion) {        "the version of the database has changed");        Db.execsql ("ALTER TABLE person ' Add account  varchar");    }

and modify the version number in the Personsqliteopenhelper method to add 1

     Public Personsqliteopenhelper (Context context) {        //  contextual contexts         //  Person.db database file name         //  factory cursor Factory         //  version database, starting from 1        Supernull, 5);         // TODO auto-generated constructor stub    }

Then the Testcreatedb method in the unit test run Testpersondb.java

2. Database transaction Operations

Case:
Zhang San to Harry Remittance
1. Zhang San account-1000
2. Harry account + 1000

     Public voidTesttransaction ()throwsException {personsqliteopenhelper helper=NewPersonsqliteopenhelper (GetContext ()); Sqlitedatabase DB=helper.getreadabledatabase (); //start a transaction for the databasedb.begintransaction (); Try{db.execsql ("Update person set account=account-1000 where name=?",                    NewObject[] {"Zhangsan" }); inti = 2/0;//Manufacturing exception test transaction operationsDb.execsql ("Update person set account=account+1000 where name=?",                    NewObject[] {"Wangwu" }); //Mark database transaction execution succeededdb.settransactionsuccessful (); } Catch(Exception e) {}finally{db.endtransaction ();        Db.close (); }    }

Finally use Sqlite3 to view the data in the Database person table, the transfer did not succeed, Zhang San and Harry's money did not change

Transactions for the Android database

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.