RSS Forums RSS
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 677 | Replies: 2
Reply
Join Date: Aug 2007
Location: Somewhere between heaven and hell
Posts: 112
Reputation: Kusno is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
Kusno's Avatar
Kusno Kusno is offline Offline
Junior Poster

MySQL rollback problem.

  #1  
Apr 24th, 2008
I have this method :

public void ExecuteSQL(String SQL)
{
Koneksi getCn = new Koneksi();
Connection cnExecute = null;
try {
cnExecute = getCn.getConnection();
int rowNgefek = 0;
Statement stExecute = cnExecute.createStatement();
cnExecute.setAutoCommit(true);
stExecute.executeUpdate(SQL, rowNgefek);
cnExecute.commit();
closingConnection (cnExecute,stExecute);
}
catch (SQLException e)
{
System.out.println(e.toString());
}
}

If insert/delete/update raise error, I want to rollback all transactions.
It means I have to use cnExecute.rollback();
but I dont know where I should put that code..
In catch(SQLException e) does not recognize cnExecute.rollback();

Thanks,

Kusno
NEVER NEVER NEVER GIVE UP
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2007
Location: Somewhere between heaven and hell
Posts: 112
Reputation: Kusno is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
Kusno's Avatar
Kusno Kusno is offline Offline
Junior Poster

Re: MySQL rollback problem.

  #2  
Apr 24th, 2008
I have found the answer. Thanks anyway.

public class Data
{
Connection cnExecute = null;
int rowNgefek = 0;
Statement stExecute;

public void ExecuteSQL(ArrayList<String> SQLArray)
{
Koneksi getCn = new Koneksi();
String SQL;
rowNgefek = 0;
try {
cnExecute = getCn.getConnection();
stExecute = cnExecute.createStatement();
cnExecute.setAutoCommit(false);
for (int i=0;i <= SQLArray.size()-1; i++)
{
SQL = SQLArray.get(i).toString().trim();
rowNgefek = stExecute.executeUpdate(SQL);
}
}
catch (SQLException e)
{
rowNgefek = 0;
JOptionPane.showMessageDialog(null, e.toString(),"Warning",JOptionPane.OK_OPTION);
}
finally
{
if(rowNgefek<=0)
{
try
{
cnExecute.rollback();
}
catch(SQLException e)
{

}
}
else
{
try
{
cnExecute.commit();
}
catch(SQLException e)
{
System.out.println(e.toString());
}
}
closingConnection (cnExecute,stExecute);
}


}
NEVER NEVER NEVER GIVE UP
Reply With Quote  
Join Date: Apr 2008
Location: Belgium
Posts: 47
Reputation: Jens is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 5
Jens's Avatar
Jens Jens is offline Offline
Light Poster

Re: MySQL rollback problem.

  #3  
Apr 24th, 2008
I can be wrong here, but I think that rollback only works for a current transaction. If you use commit, you close that transaction.

So you should catch your error before the commit, and roll back then too. If all is fine you can proceed to commit.

Also, autocommit should be set to false I think. Else your program is going to commit as soon as you release your query.

Edit: Ack, excuse me. Sort of scrolled down as soon as I saw the problem and thought of a possible answer. I should watch before I type :p. Glad you found a solution though.
Last edited by Jens : Apr 24th, 2008 at 4:45 am. Reason: -> not reading the whole thread.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 11:37 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC