Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

beginTransaction(), endTransaction(), and setTransactionSuccessful(). What exactly do they do?

I have to provide synchronization when inserting, querying, updating and deleting items from a database. As far as I understand beginTransaction() and beginTransactionNonExclusive() are the methods I need.

Besides SQLite documentation describes EXCLUSIVE, IMMEDIATE, and DEFERRED quite well.

Transactions can be deferred, immediate, or exclusive. Deferred means that no locks are acquired on the database until the database is first accessed.

If the transaction is immediate, then RESERVED locks are acquired on all databases as soon as the BEGIN command is executed, without waiting for the database to be used. After a BEGIN IMMEDIATE, no other database connection will be able to write to the database or do a BEGIN IMMEDIATE or BEGIN EXCLUSIVE. Other processes can continue to read from the database, however.

An exclusive transaction causes EXCLUSIVE locks to be acquired on all databases. After a BEGIN EXCLUSIVE, no other database connection except for read_uncommitted connections will be able to read the database and no other connection without exception will be able to write the database until the transaction is complete.

It seems to provide some protection from unwanted insertions and queries while some thread is working with the database. But I'm not sure that it guarantees synchronization.

There is the insert method of my ContentProvider.

@Override
public Uri insert(Uri baseUri, ContentValues values) {
    try {
        mDatabase = mHelper.getWritableDatabase();
        mDatabase.beginTransaction(); // EXCLUSIVE
        switch (sUriMatcher.match(baseUri)) {
        case UriCodes.COUNTRIES:
        case UriCodes.CONTINENTS:
        case UriCodes.ORGS:
            String table = baseUri.getLastPathSegment();
            long rowId = mDatabase.insert(table, null, values);
            Uri uri = Uri.withAppendedPath(baseUri, Long.toString(rowId));
            mDatabase.setTransactionSuccessful();
            return uri;

        default:
            mDatabase.endTransaction();
            throw new IllegalArgumentException(UNSUPPORTED_URI + SPACE + baseUri);
        }
    } finally {
        mDatabase.endTransaction();
    }
}

I haven't had any problems without beginTransaction(), endTransaction(), and setTransactionSuccessful() before. Do I really need to add them?

like image 817
Maksim Dmitriev Avatar asked May 15 '13 11:05

Maksim Dmitriev


People also ask

Which of the function is called to execute query in android SQLite?

SQLiteDatabase has methods to create, delete, execute SQL commands, and perform other common database management tasks.

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.

How to use transaction in SQLite android?

SQLite transaction statements First, open a transaction by issuing the BEGIN TRANSACTION command. After executing the statement BEGIN TRANSACTION , the transaction is open until it is explicitly committed or rolled back. Second, issue SQL statements to select or update data in the database.

What is the purpose of an SQLite database to an android application?

SQLite is an open-source relational database i.e. used to perform database operations on android devices such as storing, manipulating or retrieving persistent data from the database. It is embedded in android bydefault. So, there is no need to perform any database setup or administration task.


2 Answers

Off course with Android! If you are working for reliable data manipulation then it is important to use following supported methods.

BeginTransaction();
SetTransactionSuccessful();
EndTransaction(); 

For further see this…In android it is highly important to use transactions when working with databases.

like image 63
Shujaat Abdi Avatar answered Sep 23 '22 03:09

Shujaat Abdi


Yes, it makes app smooth.Because SQLite consider one cursor as one transaction, so if we use begin transaction() before we insert big amount of data, then use setTransactionSuccessful() second, then endTransaction() in finally, SQLite will consider this insert event as one transaction, instead of hundreds of transactions.

like image 38
JC Wang Avatar answered Sep 22 '22 03:09

JC Wang