Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I always use transactions in android sqlite?

Here is a simple example:

public boolean containsId(Long userid) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("select * from " + getTableName() + " where id = " + userid, null);
    boolean rows = cursor.getCount() > 0;
    db.close();
    return rows;
}

I thought sqlite automatically starts a transaction. My colleague said I must always start a transaction.

So what is the right pattern? Should I start a transaction if I read data from database? I am sure that no other thread will write at this time in this table.

like image 611
dieter Avatar asked Apr 18 '15 20:04

dieter


People also ask

Why do we need to control transactions in SQLite?

It is important to control transactions to ensure data integrity and to handle database errors. Practically, you will club many SQLite queries into a group and you will execute all of them together as part of a transaction.

How much data is too much for SQLite?

SQLite database files have a maximum size of about 140 TB. On a phone, the size of the storage (a few GB) will limit your database file size, while the memory size will limit how much data you can retrieve from a query. Furthermore, Android cursors have a limit of 1 MB for the results.

Does SQLite have transactions?

SQLite supports multiple simultaneous read transactions coming from separate database connections, possibly in separate threads or processes, but only one simultaneous write transaction. A read transaction is used for reading only. A write transaction allows both reading and writing.

What are the several important methods that can be used in SQLite database for Android?

Important Methods in SQLite DatabaseThis method will return the number of rows in the cursor. This method returns a Boolean value when our cursor is closed. This method returns the total number of columns present in our table. This method will return the name of the column when we passed the index of our column in it.


1 Answers

No changes can be made to the database except within a transaction. Any command that changes the database (basically, any SQL command other than SELECT) will automatically start a transaction if one is not already in effect. Automatically started transactions are committed when the last query finishes.

Transactions can be started manually using the BEGIN command. Such transactions usually persist until the next COMMIT or ROLLBACK command. But a transaction will also ROLLBACK if the database is closed or if an error occurs and the ROLLBACK conflict resolution algorithm is specified.

SQLite Query Language.

So yes, a transaction starts automatically. However, if you execute multiple queries without starting the transaction manually, multiple transactions are started and finished, which impacts performance negatively.
Furthermore, the automatic rollback mechanism only rolls back the single query when it fails.

like image 135
nhaarman Avatar answered Sep 29 '22 15:09

nhaarman