943,742 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3462
  • Java RSS
Apr 24th, 2008
0

MySQL rollback problem.

Expand Post »
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
Similar Threads
Reputation Points: 11
Solved Threads: 17
Junior Poster
Kusno is offline Offline
191 posts
since Aug 2007
Apr 24th, 2008
0

Re: MySQL rollback problem.

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);
}


}
Reputation Points: 11
Solved Threads: 17
Junior Poster
Kusno is offline Offline
191 posts
since Aug 2007
Apr 24th, 2008
0

Re: MySQL rollback problem.

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.
Reputation Points: 13
Solved Threads: 6
Light Poster
Jens is offline Offline
47 posts
since Apr 2008
Dec 10th, 2008
-2

Re: MySQL rollback problem.

public static boolean INSERT(Payment argPayment, Connection con) {
boolean result = false;
Connection mastercon = MyConnection.getMasterConnection();

try {
// String selectStatement = "SELECT SUM(AMOUNT) AS TOTALAMOUNT, SUM(PAID) AS TOTALPAID,OUTSTANDING FROM PAYMENT WHERE SUBID=? AND OFFICEID=? GROUP BY SUBID";
String selectStatement = "SELECT PAID AS TOTALPAID,OUTSTANDING FROM PAYMENT WHERE SUBID=? AND OFFICEID=? AND ID = (SELECT MAX(ID) FROM PAYMENT WHERE SUBID=? AND OFFICEID=?) GROUP BY SUBID";
PreparedStatement stmt = con.prepareStatement(selectStatement);
stmt.setInt(1, argPayment.getSubid());
stmt.setInt(2, argPayment.getOfficeID());
stmt.setInt(3, argPayment.getSubid());
stmt.setInt(4, argPayment.getOfficeID());
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
argPayment.setSumPaid(rs.getFloat("TOTALPAID"));
argPayment.setOutstanding(rs.getFloat("OUTSTANDING"));
// argPayment.setSumAmount(rs.getFloat("TOTALAMOUNT"));
}

String insertStatement = "INSERT INTO PAYMENT(SUBID,AMOUNT,PAID,OUTSTANDING,ENTRYDATE,REMARK,COURIERCHARGE,LESSPAYMENT,OFFICEID) VALUES (?,?,?,?,?,?,?,?,?)";
PreparedStatement stat = con.prepareStatement(insertStatement);
stat.setInt(1, argPayment.getSubid());
stat.setFloat(2, argPayment.getAmount());
stat.setFloat(3, argPayment.getAmount() + argPayment.getSumPaid());
stat.setFloat(4, argPayment.getOutstanding() - argPayment.getAmount());

stat.setDate(5, argPayment.getEntryDate());
stat.setString(6, argPayment.getRemark());
stat.setFloat(7, argPayment.getCourierCharge());
stat.setFloat(8, argPayment.getLessPayment());
stat.setInt(9, argPayment.getOfficeID());

con.setAutoCommit(false);
if (stat.executeUpdate() > 0) {
int subid = argPayment.getSubid();
Purchase purchase = DBPurchase.retrive(subid, argPayment.getOfficeID(), con);
int partyid = purchase.getParty().getPartyID();
Account partyAccount = DBAccount.retrieveAccountByIDandType(partyid, "PARTY", mastercon);
Account officeAccount = DBAccount.retrieveAccountByIDandType(argPayment.getOfficeID(), "OFFICE", mastercon);
AccountTrack partyAccountTrack = new AccountTrack();
partyAccountTrack.setId(partyAccount.getId());
partyAccountTrack.setCredit(true);
partyAccountTrack.setDate(argPayment.getEntryDate());
partyAccountTrack.setAmount(argPayment.getAmount());
partyAccountTrack.setDescription("Payment for payment id-" + argPayment.getId());
if (DBAccountTrack.insert(partyAccountTrack)) {
AccountTrack officeAccountTrack = new AccountTrack();
officeAccountTrack.setId(officeAccount.getId());
officeAccountTrack.setCredit(false);
officeAccountTrack.setDate(argPayment.getEntryDate());
officeAccountTrack.setAmount(argPayment.getAmount());
officeAccountTrack.setDescription("Payment for payment id-" + argPayment.getId());

if (DBAccountTrack.insert(officeAccountTrack)) {
AccountTrack pAccountTrack = new AccountTrack();
pAccountTrack.setId(partyAccount.getId());
pAccountTrack.setCredit(false);
pAccountTrack.setDate(argPayment.getEntryDate());
float amount = argPayment.getLessPayment() + argPayment.getCourierCharge();
pAccountTrack.setAmount(amount);
pAccountTrack.setDescription("Less Payment and Courier Charge-" + argPayment.getId());

if (DBAccountTrack.insert(pAccountTrack)) {
AccountTrack oAccountTrack = new AccountTrack();
oAccountTrack.setId(officeAccount.getId());
oAccountTrack.setCredit(true);
oAccountTrack.setDate(argPayment.getEntryDate());
float officeAmount = argPayment.getLessPayment() + argPayment.getCourierCharge();
oAccountTrack.setAmount(amount);
oAccountTrack.setDescription("Less Payment and Courier Charge-" + argPayment.getId());

result = DBAccountTrack.insert(oAccountTrack);
con.commit();
}

}
}
}

} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}finally{
try{
con.setAutoCommit(true);
mastercon.close();
}catch(Exception e){
}
}
return result;
}
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006
Dec 10th, 2008
0

Re: MySQL rollback problem.

@bugmemnot,
I am pretty sure the thread starter has already solved his problem by now.
Last edited by stephen84s; Dec 10th, 2008 at 3:44 am.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Recursion Help
Next Thread in Java Forum Timeline: working with arrays and toString method





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC