| Author |
Transactions Rollback in jsp
|
manju rattan
Greenhorn
Joined: Aug 07, 2008
Posts: 1
|
|
hi, i have this Java class having SQL statements in Try block. If anyhow any query gets failed it goes in the catch exception . So is there any way to rollback the transactions done by those sql statements in try block. I have turned the AutoCommit to false and used con.rollback in my catch{} block. But rollback is not working in catch{}. Here is my code : public String chkAmount(String username,long frontendAmt,String[] mobileno,String[] ArrayAmount) { celldb db1 = new celldb(); Statementstmt=null; ResultSetrs=null,rs1=null; Connection con = null; String status=""; String sqlStmt = null; long prebalance=0; long balanceAmt = 0; long arrAmt=0; String eventType="Transfer"; String dbacctype=""; String error=""; String error1=""; try { con = db1.getConnection ( ); stmt = con.createStatement ( ); sqlStmt = "select prebalance,postbalance,acctype from account_details where msisdn='"+ username +"' order by datetime desc limit 1"; rs = stmt.executeQuery ( sqlStmt ); while( rs.next() ) { prebalance=rs.getLong(1); balanceAmt = rs.getLong (2); dbacctype=rs.getString(3); } if ( frontendAmt <= balanceAmt ) { for(int i=0;i<ArrayAmount.length;i++) { arrAmt=Long.parseLong(ArrayAmount[i]); int j=i; status= recharge(username,mobileno[j],arrAmt); if(status.equals("")) { status=""; } else if(status.equals("done")) { status="done"; } else { status="error"; } } } else { status="lessbalance"; sqlStmt ="insert into transaction_detail(srcmsisdn,amttransfer,state,annotation,type,srctype,prebalance,postbalance) values ('"+username+"','"+frontendAmt+"','Failed','Insufficient Amount','"+eventType+"','web','"+prebalance +"','"+balanceAmt +"')"; stmt.executeUpdate( sqlStmt ); } } catch(Exception e) { try { System.out.println("Error caught in class Rechargecombo chkAmount() fuctions in Exception first catch. Message ->" + e.getMessage()); con = db1.getConnection ( ); stmt = con.createStatement ( ); error = e.getMessage() ; error1 = error.replaceAll("'","`"); sqlStmt ="insert into transaction_detail(srcmsisdn,amttransfer,state,annotation,type,srctype,prebalance,postbalance) values ('"+username+"','"+frontendAmt+"','Failed','Error caught in class Rechargecombo chkAmount() fuctions in Exception first catch"+error1+"','"+eventType+"','web','"+prebalance +"','"+balanceAmt +"')"; stmt.executeUpdate( sqlStmt ); status = "error"; } catch(Exception e1) { System.out.println("Error caught in class Rechargecombo chkAmount() fuctions in Exception second catch. Message ->" + e1.getMessage()); } } finally { //close stmt, rs and connection try { rs.close ( ); stmt.close ( ); con.close ( ); } catch ( Exception e ) { System.out.println("Exception in finally block of Rechargecombo chkAmount() : "+e.toString()); } } return status; }//chkAmt function ends here
|
 |
Amit Ghorpade
Bartender
Joined: Jun 06, 2007
Posts: 2549
|
|
Hi manju rattan welcome to Javaranch , Firstly please UseCodeTags. Unformatted code is hard to read. You can edit your post to add code tags by clicking the . And your code happens to be a Java class and it has nothing to do with JSP's. Please CarefullyChooseOneForum. And take some time to read the ask good questions link below. [ August 07, 2008: Message edited by: Amit Ghorpade ]
|
SCJP, SCWCD.
|Asking Good Questions|
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16482
|
|
|
You don't actually call the rollback() method anywhere in that code. I don't know what you meant by "not working" (more details would help) but it certainly won't happen if you don't call the method.
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: Transactions Rollback in jsp
|
|
|