Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Transactions Rollback in jsp  RSS feed

 
manju rattan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Paul Clapham
Sheriff
Posts: 22203
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!