By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,984 Members | 2,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,984 IT Pros & Developers. It's quick & easy.

tran.rollback does not work

P: 72
Hi,

Can someone check my code below? The second UPDATE fails because there is no GNameX column but the first UPDATE gets written to the db anyway. I have done a trace and it definitely executes the trans.rollback.

Any help would be appreciated.

Expand|Select|Wrap|Line Numbers
  1.             MySqlConnection conn = new MySqlConnection(DefaultSettings.ConnString);
  2.             conn.Open();
  3.             MySqlCommand cmd = conn.CreateCommand();
  4.             MySqlTransaction trans;
  5.             trans = conn.BeginTransaction();
  6.             cmd.Connection = conn;
  7.             cmd.Transaction = trans;
  8.             try
  9.             {
  10.                 cmd.CommandText = "SET autocommit = 0";
  11.                 cmd.ExecuteNonQuery();
  12.                 cmd.CommandText = "UPDATE UserTbl SET FName='denden2' WHERE UserID='10113'";
  13.                 cmd.ExecuteNonQuery();
  14.                 cmd.CommandText = "UPDATE UserTbl SET GNameX='milmil2' WHERE UserID='10113'";
  15.                 cmd.ExecuteNonQuery();
  16.                 trans.Commit();
  17.                 return true;
  18.             }
  19.             catch (Exception ex)
  20.             {
  21.                 try
  22.                 {
  23.                     trans.Rollback();
  24.                 }
  25.                 catch (MySqlException mse)
  26.                 {
  27.                     //log.error(mse);
  28.                 }
  29.                 return false;
  30.             }
  31.             finally
  32.             {
  33.                 conn.Close();
  34.             }
  35.  
Mar 1 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.