Android database transactions and android database transactions
What is a database transaction?
A Transaction is a program execution unit that accesses and may update various data items in the database ). Transactions are usually caused by the execution of user programs written in advanced database manipulation languages or programming languages, and are defined by statements such as begin transaction and end transaction (or function call. A transaction is composed of all operations performed between the start and end of a transaction.
Features
Transactions are the basic unit of recovery and concurrency control.
Transactions should have four attributes: atomicity, consistency, isolation, and persistence. These four attributes are generally called ACID properties.
Atomicity ). A transaction is an inseparable unit of work. All operations involved in a transaction are either done or not done.
Consistency ). Transactions must change the database from one consistent state to another. Consistency is closely related to atomicity.
Isolation ). The execution of a transaction cannot be disturbed by other transactions. That is to say, the operations and data used within a transaction are isolated from other concurrent transactions, and the transactions executed concurrently cannot interfere with each other.
Durability ). Once a transaction is committed, its changes to the data in the database should be permanent. Other subsequent operations or faults should not have any impact on them.
Db. beginTransaction (); try {... db. setTransactionSuccessful ();} catch (Exception e) {Toast. makeText (this, "exception occurred, transaction rollback", 0 ). show ();} finally {db. endTransaction ();}1. Modify the database
Add an account column to the previously created person. db database to record accounts
Public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {Log. I (TAG, "database version changed"); db.exe cSQL ("alter table person add account varchar (20 )");}
Modify the version number in the PersonSQLiteOpenHelper method and add it to 1.
Public PersonSQLiteOpenHelper (Context context) {// context Context // person. db database file name // factory cursor factory // version database version number starts from 1 super (context, "person. db ", null, 5); // TODO Auto-generated constructor stub}
Then run the testCreateDB method in TestPersonDB. java in the unit test.
2. Database Transaction operations
Case:
Zhang San remitted money to Wang Wu
1. Zhang San account-1000
2. Wang Wu account + 1000
Public void testTransaction () throws Exception {PersonSQLiteOpenHelper helper = new PersonSQLiteOpenHelper (getContext (); SQLiteDatabase db = helper. getReadableDatabase (); // start the transaction database of the database. beginTransaction (); try {db.exe cSQL ("update person set account = account-1000 where name =? ", New Object [] {" zhangsan "}); int I = 2/0; // create an exception test transaction operation db.exe cSQL ("update person set account = account + 1000 where name =? ", New Object [] {" wangwu "}); // mark the database transaction execution success. setTransactionSuccessful ();} catch (Exception e) {} finally {db. endTransaction (); db. close ();}}
Finally, use sqlite3 to view the data in the person table of the database. The transfer was unsuccessful, and the money for Zhang Sanhe and Wang wushi remained unchanged.