{"id":4220,"date":"2018-03-29T17:45:00","date_gmt":"2018-03-29T12:15:00","guid":{"rendered":"https:\/\/www.innovationm.com\/blog\/?p=4220"},"modified":"2018-08-14T13:21:15","modified_gmt":"2018-08-14T07:51:15","slug":"design-pattern-database-transaction-handling-in-android","status":"publish","type":"post","link":"https:\/\/www.innovationm.com\/blog\/design-pattern-database-transaction-handling-in-android\/","title":{"rendered":"Design Pattern &#8211; Database Transaction Handling (in Android)"},"content":{"rendered":"<p>This blog is about database transaction handling and its design pattern in android. It also gives a brief knowledge on how to handle Exception.<\/p>\n<p>Here we have a main method which is responsible to <strong>get \u00a0database instance\u00a0 -&gt; start Database Transaction\u00a0 -&gt;\u00a0 Mark Transaction Successful\u00a0 \u00a0-&gt;\u00a0 Commit or Rollback Transaction\u00a0 -&gt;\u00a0 Close database<\/strong>. This main method have its two child method both methods perform some database transaction<\/p>\n<p style=\"text-align: left;\"><strong>1 . Main method defines Transaction boundaries.<\/strong><\/p>\n<p style=\"text-align: left;\"><strong>2.\u00a0 Methods joins the Database transaction.<\/strong><\/p>\n<h4 style=\"text-align: left;\"><strong>3.\u00a0 Another Method joins the Database Transaction.<\/strong><\/h4>\n<h2 style=\"text-align: left;\">1 . Main method defines Transaction boundaries :<\/h2>\n<p style=\"text-align: left;\">This method will:<\/p>\n<ul style=\"text-align: left;\">\n<li>Get Database instance.<\/li>\n<li>Start Database Transaction.<\/li>\n<li>Mark Transaction Successful.<\/li>\n<li>Commit or Rollback Transaction.<\/li>\n<li>Close Database.<\/li>\n<\/ul>\n<pre class=\"\">public static void prepareData() throws &lt;Name of the app&gt;AppException\r\n{\r\n    try\r\n    {\r\n        \/\/Get Database Instance\r\n        sqliteDatabase = DBUtils.openWritableDatabase();\r\n\r\n        \/\/Start Database Transaction\r\n        DBUtils.beginTransaction(sqliteDatabase);\r\n\r\n        prepareData1(sqLiteDatabase);\r\n        prepareData2(sqLiteDatabase);\r\n\r\n        \/\/Mark Transaction Successful\r\n        setTransactionSuccessful(sqLiteDatabase);\r\n    }\r\n    catch (Exception exception)\r\n    {\r\n        String exceptionMessage = \"Error in inserting data.\"\r\n        ExceptionManager.processException(AppConstant.ERROR_CODE_1015,\r\n                exceptionMessage, exception);\r\n    }\r\n    finally\r\n    {\r\n        \/\/Commit or Rollback transaction\r\n        endTransaction(sqLiteDatabase);\r\n        \/\/Close Database\r\n        appDatabase.closeDatabase();\r\n    }\r\n}\r\n\r\n<\/pre>\n<h4 style=\"text-align: left;\">1.1 <strong>Get Database Instance<\/strong><\/h4>\n<ul style=\"text-align: left;\">\n<li style=\"text-align: center;\">DbUtils.openWritableDatabase()\u00a0 &#8211;\u00a0 If our requirement is to fetch the data from database ,\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0we have to use openWritableDatabase() method.<\/li>\n<\/ul>\n<pre class=\"\">In DbUtil class create openWritableDatabase()<\/pre>\n<pre class=\"\">\/**\r\n * Opens the database\r\n * @return\r\n *\/\r\npublic static SQLiteDatabase openWritableDatabase()\r\n{\r\n    SQLiteDatabase sqliteDatabase = null;\r\n\r\n    \/\/ Opening new database\r\n    sqliteDatabase = sqliteOpenHelper.getWritableDatabase();\r\n\r\n    \/\/Enable Write Ahead Logging\r\n    sqliteDatabase.enableWriteAheadLogging();\r\n\r\n    return sqliteDatabase;\r\n}\r\n\r\n<\/pre>\n<ul style=\"text-align: left;\">\n<li>DbUtils.openReadableDatabase() &#8211; If our requirement is to read the data from database\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 use\u00a0 openReadableDatabase() method.<\/li>\n<\/ul>\n<pre class=\"\">In DbUtil class create openReadableDatabase()<\/pre>\n<pre class=\"\">\/**\r\n * Opens the database\r\n * @return\r\n *\/\r\npublic static SQLiteDatabase openReadableDatabase()\r\n{\r\n    SQLiteDatabase sqliteDatabase = null;\r\n\r\n    \/\/ Opening new database\r\n    sqliteDatabase = sqliteOpenHelper.getReadableDatabase();\r\n\r\n    return sqliteDatabase;\r\n}\r\n\r\n<\/pre>\n<h5 style=\"text-align: left;\">1.2\u00a0\u00a0<strong>Start Database Transaction<\/strong><\/h5>\n<ul style=\"text-align: left;\">\n<li>beginTransaction(SQLiteDatabase sqLiteDatabase)\u00a0 &#8211; This method is used to begin our\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 transaction in EXCLUSIVE mode and when parent transaction is completed all child\u00a0 \u00a0 \u00a0 \u00a0 transactions will be rollback or commit ed.<\/li>\n<\/ul>\n<pre class=\"\">In DbUtils class create beginTransaction() method:<\/pre>\n<pre class=\"\">public static void beginTransaction(SQLiteDatabase sqLiteDatabase)\r\n{\r\n    try\r\n    {\r\n        if(sqLiteDatabase != null)\r\n        {\r\n            sqLiteDatabase.beginTransaction();\r\n        }\r\n    }\r\n    catch(Exception exception)\r\n    {\r\n        \/\/Consume Exception\r\n    }\r\n}\r\n\r\n<\/pre>\n<h5 style=\"text-align: left;\">1.3\u00a0<strong>\u00a0Mark Transaction Successful:<\/strong><\/h5>\n<ul>\n<li>setTransactionSuccessful(SQLiteDatabase sqLiteDatabase) method is used to mark our transaction successful. All database related task must be only done between beginTransaction() method and setTransactionSuccessful(SQLiteDatabase sqliteDatabase) \u00a0method.Do not do any database related task between setTransactionSuccessful(SQLiteDatabase sqliteDatabase) method and endTransaction(SQLiteDatabase sqLiteDatabase) method.<\/li>\n<\/ul>\n<pre class=\"\">In DbUtils class createsetTransactionSuccessful(SQLiteDatabase sqliteDatabase)\u00a0:<\/pre>\n<pre class=\"\">public static void setTransactionSuccessful(SQLiteDatabase sqLiteDatabase)\r\n{\r\n    try\r\n    {\r\n        if(sqLiteDatabase != null)\r\n        {\r\n            sqLiteDatabase.setTransactionSuccessful();\r\n        }\r\n    }\r\n    catch(Exception exception)\r\n    {\r\n        \/\/Consume Exception\r\n    }\r\n}<\/pre>\n<h4 style=\"text-align: left;\">1.4\u00a0<b> <\/b><strong>Commit or Rollback Transaction<\/strong><\/h4>\n<ul style=\"text-align: left;\">\n<li>endTransaction(SQLiteDatabase sqLiteDatabase)\u00a0 &#8211; This method is used to commit or rollback our database transaction.If our transaction is marked successful by\u00a0setTransactionSuccessful() method then our transaction will be commited else all changes will be rollback.<\/li>\n<\/ul>\n<pre class=\"\">In DbUtils class create endTransaction(SQLiteDatabase sqLiteDatabase) method :<\/pre>\n<pre class=\"\">public static void endTransaction(SQLiteDatabase sqLiteDatabase)\r\n{\r\n    try\r\n    {\r\n        if(sqLiteDatabase != null)\r\n        {\r\n            sqLiteDatabase.endTransaction();\r\n        }\r\n    }\r\n    catch(Exception exception)\r\n    {\r\n        \/\/Consume Exception\r\n    }\r\n}\r\n\r\n<\/pre>\n<h3 style=\"text-align: left;\">1.4\u00a0<b> <\/b>Close Database.<\/h3>\n<pre class=\"\"> \r\n   \/\/Close Database\r\n   appDatabase.closeDatabase();\r\n\r\n<\/pre>\n<h2 style=\"text-align: left;\"><strong>2 . Method joins the Database Transaction<\/strong><\/h2>\n<p style=\"text-align: left;\">This is the child transaction of\u00a0prepareData() method here\u00a0 SQLiteDatabase object must be passed from parent transaction method .<\/p>\n<ol style=\"text-align: left;\">\n<li>Exceute query.<\/li>\n<li>Handle Exception<\/li>\n<\/ol>\n<pre class=\"\">private static void prepareData1(SQLiteDatabase sqLiteDatabase)\r\n        throws &lt;Name of the app&gt;AppException\r\n{\r\n    ContentValues contentValues = null;\r\n    long rowId = 0;\r\n\r\n    try\r\n    {\r\n        \/\/Prepare ContentValues object\r\n        contentValues = new ContentValues();\r\n\r\n        \/\/Execute Query\r\n        rowId = sqLiteDatabase.insertOrThrow(\"Table Name\", null, contentValues);\r\n\r\n        \/\/If rowId is -1 then this means that Row was not inserted.\r\n        \/\/Something went wrong. Throw Exception\r\n        if (rowId == -1)\r\n        {\r\n            String exceptionMessage = \"Error in inserting Data. Row Id is -1.\";\r\n            throw new &lt;Name of the App&gt;AppException\r\n                (AppConstant.ERROR_CODE_1011, exceptionMessage);\r\n        }\r\n    }\r\n    catch (Exception exception)\r\n    {\r\n         \/\/Handle Exception\r\n        String exceptionMessage = \"Error in inserting Data.\";\r\n        int errorCode = AppConstant.ERROR_CODE_8012;\r\n        ExceptionManager.dispatchExceptionDetails(errorCode, message, exception);\r\n    }\r\n\r\n<\/pre>\n<h4 style=\"text-align: left;\">2.1 <b>Execute Query<\/b><\/h4>\n<p style=\"text-align: left;\">Here we are inserting data in database and after execution of query we have to check whether our data is inserted or not. If our data is not inserted thow AppException.<\/p>\n<pre class=\"\">\/\/Execute Query\r\nrowId = sqLiteDatabase.insertOrThrow(\"Table Name\", null, contentValues);\r\n\r\n\/\/If rowId is -1 then this means that Row was not inserted.\r\n\/\/Something went wrong. Throw Exception\r\nif (rowId == -1)\r\n{\r\n   String exceptionMessage = \"Error in inserting Data. Row Id is -1.\";\r\n   throw new &lt;Name of the App&gt;AppException\r\n      (AppConstant.ERROR_CODE_1011, exceptionMessage);\r\n}\r\n\r\n<\/pre>\n<h4 style=\"text-align: left;\">2.2 Handle Exception<\/h4>\n<pre class=\"\">catch (Exception exception)\r\n{\r\n   \/\/Handle Exception\r\n   String exceptionMessage = \"Error in inserting Data.\";\r\n   int errorCode = AppConstant.ERROR_CODE_8012;\r\n   ExceptionManager.dispatchExceptionDetails(errorCode, message, exception);\r\n}\r\n\r\n<\/pre>\n<p style=\"text-align: left;\"><span style=\"color: #000000;\">ExceptionManager.dispatchExceptionDetails(errorCode, message, exception) method is used to throw\u00a0 or create AppException object.<\/span><\/p>\n<pre class=\"\">public static void dispatchExceptionDetails(int exceptionCode, String exceptionMessage, Exception exceptionObject) throws &lt;Name of the App&gt;AppException\r\n{\r\n   if(exceptionObject instanceof AppException)\r\n   {\r\n      throw (AppException) exceptionObject;\r\n   }\r\n   else\r\n   {\r\n      \/\/Log Exception details\r\n      logException(exceptionCode, exceptionMessage, exceptionObject);\r\n      \r\n      \/\/Throw Exception\r\n      throw new &lt;Name of the App&gt;AppException(exceptionCode, exceptionMessage);\r\n   }\r\n}\r\n\r\n<\/pre>\n<h2 style=\"text-align: left;\"><strong>3 . Another Method joins the Database Transaction<\/strong><\/h2>\n<pre class=\"\">private static void prepareData2(SQLiteDatabase sqLiteDatabase)\r\n      throws &lt;Name of the app&gt;AppException\r\n{\r\n   ContentValues contentValues = null;\r\n   long rowId = 0;\r\n\r\n   try\r\n   {\r\n      contentValues = new ContentValues();\r\n      \/\/Prepare ContentValues object\r\n\r\n      rowId = sqLiteDatabase.insertOrThrow(\"Table Name\", null, contentValues);\r\n\r\n      \/\/If rowId is -1 then this means that Row was not inserted.\r\n           \/\/Something went wrong. Throw Exception\r\n      if (rowId == -1)\r\n      {\r\n         String exceptionMessage = \"Error in inserting Data. Row Id is -1.\";\r\n         throw new &lt;Name of the App&gt;AppException\r\n            (AppConstant.ERROR_CODE_1013, exceptionMessage);\r\n      }\r\n   }\r\n   catch (Exception exception)\r\n   {\r\n      \/\/Handle Exception\r\n      String exceptionMessage = \"Error in inserting Data.\";\r\n      int errorCode = AppConstant.ERROR_CODE_8012;\r\n      ExceptionManager.dispatchExceptionDetails(errorCode, message, exception);\r\n   }\r\n}\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog is about database transaction handling and its design pattern in android. It also gives a brief knowledge on how to handle Exception. Here we have a main method which is responsible to get \u00a0database instance\u00a0 -&gt; start Database Transaction\u00a0 -&gt;\u00a0 Mark Transaction Successful\u00a0 \u00a0-&gt;\u00a0 Commit or Rollback Transaction\u00a0 -&gt;\u00a0 Close database. This main [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4266,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[159,123,27],"class_list":["post-4220","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","tag-android","tag-best-coding-practices","tag-sqlite"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Design Pattern - Database Transaction Handling (in Android) - InnovationM - Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.innovationm.com\/blog\/design-pattern-database-transaction-handling-in-android\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Design Pattern - Database Transaction Handling (in Android) - InnovationM - Blog\" \/>\n<meta property=\"og:description\" content=\"This blog is about database transaction handling and its design pattern in android. It also gives a brief knowledge on how to handle Exception. Here we have a main method which is responsible to get \u00a0database instance\u00a0 -&gt; start Database Transaction\u00a0 -&gt;\u00a0 Mark Transaction Successful\u00a0 \u00a0-&gt;\u00a0 Commit or Rollback Transaction\u00a0 -&gt;\u00a0 Close database. This main [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.innovationm.com\/blog\/design-pattern-database-transaction-handling-in-android\/\" \/>\n<meta property=\"og:site_name\" content=\"InnovationM - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2018-03-29T12:15:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-08-14T07:51:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/03\/Database-Transaction-Handling-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1149\" \/>\n\t<meta property=\"og:image:height\" content=\"641\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"InnovationM Admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InnovationM Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-database-transaction-handling-in-android\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-database-transaction-handling-in-android\\\/\"},\"author\":{\"name\":\"InnovationM Admin\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"headline\":\"Design Pattern &#8211; Database Transaction Handling (in Android)\",\"datePublished\":\"2018-03-29T12:15:00+00:00\",\"dateModified\":\"2018-08-14T07:51:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-database-transaction-handling-in-android\\\/\"},\"wordCount\":362,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-database-transaction-handling-in-android\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/03\\\/Database-Transaction-Handling-1.jpg\",\"keywords\":[\"Android\",\"Best Coding Practices\",\"SQLite\"],\"articleSection\":[\"Android\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-database-transaction-handling-in-android\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-database-transaction-handling-in-android\\\/\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-database-transaction-handling-in-android\\\/\",\"name\":\"Design Pattern - Database Transaction Handling (in Android) - InnovationM - Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-database-transaction-handling-in-android\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-database-transaction-handling-in-android\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/03\\\/Database-Transaction-Handling-1.jpg\",\"datePublished\":\"2018-03-29T12:15:00+00:00\",\"dateModified\":\"2018-08-14T07:51:15+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-database-transaction-handling-in-android\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-database-transaction-handling-in-android\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-database-transaction-handling-in-android\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/03\\\/Database-Transaction-Handling-1.jpg\",\"contentUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/03\\\/Database-Transaction-Handling-1.jpg\",\"width\":1149,\"height\":641},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/design-pattern-database-transaction-handling-in-android\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Design Pattern &#8211; Database Transaction Handling (in Android)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\",\"name\":\"InnovationM - Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\",\"name\":\"InnovationM Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"caption\":\"InnovationM Admin\"},\"sameAs\":[\"http:\\\/\\\/www.innovationm.com\\\/\"],\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/author\\\/innovationmadmin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Design Pattern - Database Transaction Handling (in Android) - InnovationM - Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.innovationm.com\/blog\/design-pattern-database-transaction-handling-in-android\/","og_locale":"en_US","og_type":"article","og_title":"Design Pattern - Database Transaction Handling (in Android) - InnovationM - Blog","og_description":"This blog is about database transaction handling and its design pattern in android. It also gives a brief knowledge on how to handle Exception. Here we have a main method which is responsible to get \u00a0database instance\u00a0 -&gt; start Database Transaction\u00a0 -&gt;\u00a0 Mark Transaction Successful\u00a0 \u00a0-&gt;\u00a0 Commit or Rollback Transaction\u00a0 -&gt;\u00a0 Close database. This main [&hellip;]","og_url":"https:\/\/www.innovationm.com\/blog\/design-pattern-database-transaction-handling-in-android\/","og_site_name":"InnovationM - Blog","article_published_time":"2018-03-29T12:15:00+00:00","article_modified_time":"2018-08-14T07:51:15+00:00","og_image":[{"width":1149,"height":641,"url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/03\/Database-Transaction-Handling-1.jpg","type":"image\/jpeg"}],"author":"InnovationM Admin","twitter_misc":{"Written by":"InnovationM Admin","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-database-transaction-handling-in-android\/#article","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-database-transaction-handling-in-android\/"},"author":{"name":"InnovationM Admin","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"headline":"Design Pattern &#8211; Database Transaction Handling (in Android)","datePublished":"2018-03-29T12:15:00+00:00","dateModified":"2018-08-14T07:51:15+00:00","mainEntityOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-database-transaction-handling-in-android\/"},"wordCount":362,"commentCount":0,"image":{"@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-database-transaction-handling-in-android\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/03\/Database-Transaction-Handling-1.jpg","keywords":["Android","Best Coding Practices","SQLite"],"articleSection":["Android"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.innovationm.com\/blog\/design-pattern-database-transaction-handling-in-android\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-database-transaction-handling-in-android\/","url":"https:\/\/www.innovationm.com\/blog\/design-pattern-database-transaction-handling-in-android\/","name":"Design Pattern - Database Transaction Handling (in Android) - InnovationM - Blog","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-database-transaction-handling-in-android\/#primaryimage"},"image":{"@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-database-transaction-handling-in-android\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/03\/Database-Transaction-Handling-1.jpg","datePublished":"2018-03-29T12:15:00+00:00","dateModified":"2018-08-14T07:51:15+00:00","author":{"@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"breadcrumb":{"@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-database-transaction-handling-in-android\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.innovationm.com\/blog\/design-pattern-database-transaction-handling-in-android\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-database-transaction-handling-in-android\/#primaryimage","url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/03\/Database-Transaction-Handling-1.jpg","contentUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/03\/Database-Transaction-Handling-1.jpg","width":1149,"height":641},{"@type":"BreadcrumbList","@id":"https:\/\/www.innovationm.com\/blog\/design-pattern-database-transaction-handling-in-android\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.innovationm.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Design Pattern &#8211; Database Transaction Handling (in Android)"}]},{"@type":"WebSite","@id":"https:\/\/www.innovationm.com\/blog\/#website","url":"https:\/\/www.innovationm.com\/blog\/","name":"InnovationM - Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.innovationm.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed","name":"InnovationM Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","caption":"InnovationM Admin"},"sameAs":["http:\/\/www.innovationm.com\/"],"url":"https:\/\/www.innovationm.com\/blog\/author\/innovationmadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/4220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/comments?post=4220"}],"version-history":[{"count":0,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/4220\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media\/4266"}],"wp:attachment":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media?parent=4220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/categories?post=4220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/tags?post=4220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}