I need to insert three tables independently. but if the first table is inserted successfully, only the second table is required to insert data. If any error occurs when inserting the 2nd table, then it needs to roll back the 1st table of the last inserted, similar to the 3rd table:
I currently like the following:
DBAdapter dbAdapter = DBAdapter.getDBAdapterInstance(RetailerOrderActivity.this); dbAdapter.openDataBase(); for (Map.Entry<String, MyProduct> entry : myProductMap.entrySet()) { String key = entry.getKey(); MyProduct myProduct = entry.getValue(); ContentValues initialValue = new ContentValues(); initialValue.put("BusinessUnit",strBusinessUnit); initialValue.put("ExecutiveCode",strExecutive); if(salesType.equalsIgnoreCase("I")){ initialValue.put("InvoiceNo",transactionControl.getNextInvoiceNo()); initialValue.put("SalesCategory",transactionControl.getInvoicePrefix()); }else if(salesType.equalsIgnoreCase("O")){ initialValue.put("InvoiceNo",transactionControl.getNextOrderNo()); initialValue.put("SalesCategory",transactionControl.getOrderPrefix()); } initialValue.put("ProductCode",key); initialValue.put("LineNumber",i); initialValue.put("Qty",myProduct.getQty()); initialValue.put("UnitPrice",myProduct.getPrice()); initialValue.put("DiscountValue",myProduct.getDisValue()); initialValue.put("DiscountQty",myProduct.getDisQty()); long nl = dbAdapter.insertRecordsInDB("WMInvoiceLine", null, initialValue); //update WMStockRecord table if(nl != -1 ){ if((salesType.equalsIgnoreCase("I") && orderStockValidation.equals("1")) || (salesType.equalsIgnoreCase("O") && orderStockValidation.equals("1"))){ ContentValues stockValue = new ContentValues(); if(myProduct.getAvailableQuantity() < myProduct.getQty()){ stockValue.put("Stock",0.00); }else{ double tmp = myProduct.getAvailableQuantity() - myProduct.getQty(); stockValue.put("Stock",tmp); } stockValue.put("LastUpdatedOn",strDate); stockValue.put("LastUpdatedBy",strExecutive); stockValue.put("ActiveStatus","1"); String whereCon = "BusinessUnit = '"+ strBusinessUnit +"' WarehouseCode = '"+defaultSalesWarehouse + "' LocationCode = '" + defaultSalesLocation + "' ProductCode = '" + key + "'"; long stock = dbAdapter.updateRecordsInDB("WMStockRecord", stockValue, whereCon, null); } //TO-DO WMInvoicekit } i++; insertStatus = true; lineStatus = true; }
but the transaction is not available here. How can we implement a transaction object? I am using DBAdapter
source share