Database transactions
In android it is highly important to use transactions when working with databases.
First, in android database operations - especially writing - are very slow. Batching them into transactions will make them much faster.
Second, the database remains consistent under any circumstances. The database system makes sure to all the operations in a transaction take effect, or on error, rollback all of them.
If you are used to other platforms like PHP+MySQL where the code usually runs on a powerful server, witch is not likely to stop execution "unexpectedly", you can be surprised how much it affects the performance in android.
The android system can kill apps/threads/activities and so interrupt database usage, the battery can discharge or can be removed etc.
The implementation is very simple, using 3 methods in the SQLiteDatabase class:
beginTransaction();
setTransactionSuccessful();
endTransaction();
When calling endTransaction() all operations done since beginTransaction() are committed if endTransaction() have been called, or are rolled back otherwise.
An example how the main flow of a database method can look:
- mDatabase.beginTransaction();
- try{
- //do multiple database operations here
- //which throws exceptions on error
- mDatabase.setTransactionSuccessful();
- //do not any more database operations between
- //setTransactionSuccessful and endTransaction
- //end the transaction on error too when doing exception handling
- mDatabase.endTransaction();
- throw e;
- }
- //end the transaction on no error
- mDatabase.endTransaction();
- }
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
11 years 39 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
11 years 39 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
11 years 39 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
11 years 39 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
11 years 39 weeks ago
Poll
Useful resources
Android Development Projects
- iOS/Android Developer to take older Games and bring them Current
- Android apps developer - need to finish urgent.
- Buliding MobileApp For onlie order
- looking for android APP developers
- Create an ecommerce app
- text-to voice for smartphones IOS - GOOGLE - HARMONY - AND ALEXA
- Optimize Images on App
- Create small feature with drag-drop text for Android
- Scouting for advanced website and Mobile apps developers. Potential Long-term contract.
- BLACK SCREEN