Transaction is not getting rolled back Java Android Sql -
*i trying run 3 asynctask 1 one . each dependent each other. if 1 of task gets exception 3 task should rolled back. previous position. task got exception getting rolled not others code below
public static int executeorder() { int morderid=-1; connection conn = null; jtdscallablestatement jtdsproc = null; resultset orderresult = null; try { conn = connectionprovider.getconnection(); jtdsproc = (jtdscallablestatement) conn .preparecall("{call saveorder(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}"); jtdsproc.setint(1, sqlorderqueries.getorderid()); jtdsproc.setint(2, sqlorderqueries.getposid()); jtdsproc.setint(3, sqlorderqueries.getemp_cash_id()); jtdsproc.setint(4, sqlorderqueries.getcust_id()); jtdsproc.setint(5, sqlorderqueries.gettranstype()); jtdsproc.setstring(6, ""+sqlorderqueries.getmdebit()); jtdsproc.setstring(7, ""+sqlorderqueries.getadebit()); jtdsproc.setstring(8, ""+sqlorderqueries.getmcredit()); jtdsproc.setstring(9, ""+sqlorderqueries.getacredit()); jtdsproc.setstring(10, ""+sqlorderqueries.getbcredit()); jtdsproc.setint(11, sqlorderqueries.getmealplanid()); jtdsproc.setint(12, sqlorderqueries.getcashresultid()); jtdsproc.setint(13, sqlorderqueries.getlunchtype()); jtdsproc.setint(14, sqlorderqueries.getschool_id()); jtdsproc.setstring(15, sqlorderqueries.getorderdate()); jtdsproc.setint(16, sqlorderqueries.getcreditauth()); if(sqlorderqueries.getchecknum()== -1) { jtdsproc.setnull(17, types.integer); }else{ jtdsproc.setint(17, sqlorderqueries.getchecknum()); } jtdsproc.setint(18, sqlorderqueries.getoverride()); jtdsproc.setint(19, sqlorderqueries.getvoid()); jtdsproc.setint(20, sqlorderqueries.getordlog_id()); jtdsproc.setstring(21, sqlorderqueries.getordlognote()); orderresult = jtdsproc.executequery(); while (orderresult.next()) { morderid=orderresult.getint(1); log.w("data sales handler: orderid", "$--" + orderresult.getint(1)); log.w("data sales handler orderidlog:", "$--" + orderresult.getint(2)); log.w("data sales handler customerid:", "$--" + orderresult.getint(3)); log.w("data sales handler abalance:", "$--" + orderresult.getfloat(4)); log.w("data sales handler mbalance:", "$--" + orderresult.getfloat(5)); log.w("data sales handler bonacebalance:", "$--" + orderresult.getfloat(6)); log.w("data sales handler error:", "$--" + orderresult.getstring(7)); } } catch (exception ee) { try { conn.rollback(); } catch (sqlexception e) { e.printstacktrace(); } ee.printstacktrace(); return morderid = utilities.network_error; } return morderid; } public static boolean insertitem(int morderid, arraylist<integer> itemsids, arraylist<integer> menuitemids, arraylist<integer> itemsquantity, arraylist<integer> itemssoldtype, arraylist<float> itemsprice) { boolean insertstatus = false; connection conn = connectionprovider.getconnection(); preparedstatement pstmt = null; string insertstring = "insert items values (?,?,?,?,?,?,?,?,?,?)"; try { conn.setautocommit(false); pstmt = conn.preparestatement(insertstring); for(int i=0;i<itemsids.size();) { pstmt.setint(1, itemsids.get(i)); pstmt.setint(2, morderid); pstmt.setint(3, menuitemids.get(i)); pstmt.setint(4, itemsquantity.get(i)); pstmt.setstring(5, ""+itemsprice.get(i)); pstmt.setstring(6, ""+itemsprice.get(i)); pstmt.setstring(7, ""+1.00); pstmt.setint(8, 0); pstmt.setint(9, itemssoldtype.get(i)); pstmt.setnull(10, types.null); int = pstmt.executeupdate(); log.d("no of row affected insert", "" + a); i++; } insertstatus = true; } catch (exception ee) { ee.printstacktrace(); if (conn != null) { try { system.err.print("transaction being rolled back"); conn.rollback(); } catch (sqlexception excep) { excep.printstacktrace(); } } insertstatus=false; } { try { pstmt.close(); conn.setautocommit(true); } catch (sqlexception eee) { eee.printstacktrace(); } } return insertstatus; } public static arraylist<integer> getnewitemid(int totalitems) { connection conn = connectionprovider.getconnection(); jtdscallablestatement jtdsproc = null; arraylist<integer> itemsids = new arraylist<integer>(); (int = 0; < totalitems; i++) { try { jtdsproc = (jtdscallablestatement) conn .preparecall("{call fastindexgen(1,1,0)}"); resultset rs = jtdsproc.executequery(); while (rs.next()) { log.d("value", "" + rs.getint(1)); itemsids.add(rs.getint(1)); } } catch (exception ee) { ee.printstacktrace(); if (conn != null) { try { system.err.print("transaction being rolled back"); conn.rollback(); } catch (sqlexception excep) { excep.printstacktrace(); } } itemsids=null; } } return itemsids; }* public void completeorder(inputbean input){ setno_payment(false); sqlorderqueries.setposid(((homeactivity)mactivity).msessionmanager.getposid()); sqlorderqueries.setemp_cash_id(((homeactivity)mactivity).msessionmanager.getcustomerid()); /*customer id different student , guest cash sale*/ sqlorderqueries.setcust_id(input.getcustomer_id()); sqlorderqueries.settranstype(gettranstype()); sqlorderqueries.setmdebit(input.getmdebit()); sqlorderqueries.setadebit(input.getadebit()); sqlorderqueries.setmcredit(input.getmcredit()); sqlorderqueries.setacredit(input.getacredit()); sqlorderqueries.setbcredit(""+0.00); /*do not need change meal plan id ,it default default*/ sqlorderqueries.setcashresultid(((homeactivity)mactivity).msessionmanager.getcashcolumnid()); sqlorderqueries.setlunchtype(detailsfragment.getlunch_type()); sqlorderqueries.setschool_id(((homeactivity)mactivity).msessionmanager.getschoolid()); sqlorderqueries.setorderdate(getdate()); /*credit auth passed default time being*/ sqlorderqueries.setchecknum(input.getchecknumber()); /*override passed 0 time being*/ /*void ordlog_id ordlognote 0 ,default ,default respectfully*/ new asynctask<void, void, integer>() { @override protected integer doinbackground(void... params) { if(connectionprovider.checkconnection()==false){ return null; } return ordersaleshandler.executeorder(); } @override protected void onpostexecute(integer result) { super.onpostexecute(result); if(result == utilities.network_error) { alertdialog.builder mealert = new alertdialog.builder(mactivity); mealert.setmessage("network problem"); mealert.create().show(); } if(result>0){ mitemshandler.onsucessorder(result); } } }.execute(); } @override public void onsucessorder(int orderid) { morderid=orderid; new asynctask<void, void, arraylist<integer>>(){ @override protected arraylist<integer> doinbackground(void... params) { if(connectionprovider.checkconnection()==false){ return null; } return ordersaleshandler.getnewitemid(mtotalitems); } @override protected void onpostexecute(arraylist<integer> result) { super.onpostexecute(result); if(result==null) { alertdialog.builder mealert = new alertdialog.builder(mactivity); mealert.setmessage("network problem , order not completed"); mealert.create().show(); }else{ mitemshandler.onsucessitemid(result); } } }.execute(); } @override public void onsucessitemid(arraylist<integer> itemids) { itemsids=itemids; new asynctask<void, void, boolean>(){ @override protected boolean doinbackground(void... params) { if(connectionprovider.checkconnection()==false) { return false; } return ordersaleshandler.insertitem(morderid,itemsids,menuitemids,itemsquantity,itemssoldtype,itemsprice); } @override protected void onpostexecute(boolean result) { super.onpostexecute(result); if(result==true){ if(isno_payment()==false){ ((homeactivity)mactivity).msalesfragment.mpaymentdialog.dismiss(); } ((homeactivity)mactivity).msalesfragment.onclick(((homeactivity)mactivity).msalesfragment.mcancelbutton); } if(result==false){ alertdialog.builder mealert = new alertdialog.builder(mactivity); mealert.setmessage("network problem , order not completed"); mealert.create().show(); } } }.execute(); } public static boolean isno_payment() { return no_payment; } public static void setno_payment(boolean no_payment) { no_payment = no_payment; }
}
interface insertitemshandler {
public void onsucessorder(int result); public void onsucessitemid(arraylist<integer> itemsids);
}
Comments
Post a Comment