newtomysql
Technical User
Dear All,
I have this application which is using Innodb engine and .net connector 5.0.8.1. So I am using C# for my coding. So I want to run transaction base appplication where either all my sql statement go through or all fail. So the funny part I notice it works well for most of my cases but for certain cases only the second sql statement go through. I really cant figure out why is this happening. I have attached the code below. So the problem there is that my myInsertQuery1 goes through but not my myUpdateQuery1 ? So why is this happening. I have separately attached below my function for the transactionConnectionLocal1 where I keep the connection details. The rare cases are like 5 out of 400 records.
int rollbackBoolean = 0;
MySqlTransaction transactionLocal1 = null;
MySqlConnection connectionLocal1 = null;
transactionConnectionLocal1 callTransactionConnectionLocal1 = null;
try
{
callTransactionConnectionLocal1 = new transactionConnectionLocal1();
connectionLocal1 = callTransactionConnectionLocal1.localConnection1;
connectionLocal1.Open();
transactionLocal1 = connectionLocal1.BeginTransaction();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
rollbackBoolean = 1;
MessageBox.Show("Error From Database Connection " + ex.Message);
}
catch (System.Net.Sockets.SocketException ex)
{
rollbackBoolean = 1;
MessageBox.Show("Error Sockets From Database Connection " + ex.Message);
}
String myUpdateQuery1 = "Update tblStock " +
"Set tblStock.stockStatus = 'b'" +
"Where tblStock.stockSerial ='" + serial + "'" ;
MySqlCommand myCommand1 = new MySqlCommand(myUpdateQuery1);
try
{
myCommand1.Connection = connectionLocal1;
myCommand1.Transaction = transactionLocal1;
myCommand1.ExecuteNonQuery();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
rollbackBoolean = 1;
MessageBox.Show("Error From myUpdateQuery1 " + ex.Message);
}
catch (System.Net.Sockets.SocketException ex)
{
rollbackBoolean = 1;
MessageBox.Show("Error Sockets From myUpdateQuery1 " + ex.Message);
}
finally
{
myCommand1.Dispose();
}
String myInsertQuery1 = "Insert into tblTemp " +
"Set stockSerial ='" + serial+ "', " +
"tempTimeStamp='" +
DateTime.Now.ToString(("yyyy:MM:dd HH:mm:ss")) + "';";
MySqlCommand myCommand2 = new MySqlCommand(myInsertQuery1);
try
{
myCommand2.Connection = connectionLocal1;
myCommand2.Transaction = transactionLocal1;
myCommand2.ExecuteNonQuery();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
rollbackBoolean = 1;
MessageBox.Show("Error From myInsertQuery1 " + ex.Message);
}
catch (System.Net.Sockets.SocketException ex)
{
rollbackBoolean = 1;
MessageBox.Show("Error Sockets From myInsertQuery1 " + ex.Message);
}
finally
{
myCommand2.Dispose();
}
if (rollbackBoolean == 1)
{
transactionLocal1.Rollback();
}
else
{
try
{
transactionLocal1.Commit();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
try
{
transactionLocal1.Rollback();
}
catch (MySqlException ex1)
{
MessageBox.Show("An exception of type " + ex.GetType() +
" was encountered while inserting the data.");
if (transactionLocal1.Connection != null)
{
MessageBox.Show("An exception of type " + ex1.GetType() +
" was encountered while attempting to roll
back the transaction.");
}
}
}
catch (System.Net.Sockets.SocketException ex)
{
rollbackBoolean = 1;
MessageBox.Show("Error Sockets From Commit Process " + ex.Message);
}
finally
{
connectionLocal1.Close();
}
}
// connection details function
using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
namespace mSytemNonFranchise
{
public class transactionConnectionLocal1
{
public MySqlConnection localConnection1;
public MySqlCommand command;
public transactionConnectionLocal1()
{
this.localConnection1 = new
MySqlConnection("Address='localhost';Database='localDB';User
Name='root';Password='local12';Pooling='false'");
// this.command = this.localConnection1.CreateCommand();
// this.localConnection1.Open();
}
// destructor - explicitly
~transactionConnectionLocal1()
{
//this.command.Dispose();
this.localConnection1.Close();
this.localConnection1.Dispose();
}
}
}
I have this application which is using Innodb engine and .net connector 5.0.8.1. So I am using C# for my coding. So I want to run transaction base appplication where either all my sql statement go through or all fail. So the funny part I notice it works well for most of my cases but for certain cases only the second sql statement go through. I really cant figure out why is this happening. I have attached the code below. So the problem there is that my myInsertQuery1 goes through but not my myUpdateQuery1 ? So why is this happening. I have separately attached below my function for the transactionConnectionLocal1 where I keep the connection details. The rare cases are like 5 out of 400 records.
int rollbackBoolean = 0;
MySqlTransaction transactionLocal1 = null;
MySqlConnection connectionLocal1 = null;
transactionConnectionLocal1 callTransactionConnectionLocal1 = null;
try
{
callTransactionConnectionLocal1 = new transactionConnectionLocal1();
connectionLocal1 = callTransactionConnectionLocal1.localConnection1;
connectionLocal1.Open();
transactionLocal1 = connectionLocal1.BeginTransaction();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
rollbackBoolean = 1;
MessageBox.Show("Error From Database Connection " + ex.Message);
}
catch (System.Net.Sockets.SocketException ex)
{
rollbackBoolean = 1;
MessageBox.Show("Error Sockets From Database Connection " + ex.Message);
}
String myUpdateQuery1 = "Update tblStock " +
"Set tblStock.stockStatus = 'b'" +
"Where tblStock.stockSerial ='" + serial + "'" ;
MySqlCommand myCommand1 = new MySqlCommand(myUpdateQuery1);
try
{
myCommand1.Connection = connectionLocal1;
myCommand1.Transaction = transactionLocal1;
myCommand1.ExecuteNonQuery();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
rollbackBoolean = 1;
MessageBox.Show("Error From myUpdateQuery1 " + ex.Message);
}
catch (System.Net.Sockets.SocketException ex)
{
rollbackBoolean = 1;
MessageBox.Show("Error Sockets From myUpdateQuery1 " + ex.Message);
}
finally
{
myCommand1.Dispose();
}
String myInsertQuery1 = "Insert into tblTemp " +
"Set stockSerial ='" + serial+ "', " +
"tempTimeStamp='" +
DateTime.Now.ToString(("yyyy:MM:dd HH:mm:ss")) + "';";
MySqlCommand myCommand2 = new MySqlCommand(myInsertQuery1);
try
{
myCommand2.Connection = connectionLocal1;
myCommand2.Transaction = transactionLocal1;
myCommand2.ExecuteNonQuery();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
rollbackBoolean = 1;
MessageBox.Show("Error From myInsertQuery1 " + ex.Message);
}
catch (System.Net.Sockets.SocketException ex)
{
rollbackBoolean = 1;
MessageBox.Show("Error Sockets From myInsertQuery1 " + ex.Message);
}
finally
{
myCommand2.Dispose();
}
if (rollbackBoolean == 1)
{
transactionLocal1.Rollback();
}
else
{
try
{
transactionLocal1.Commit();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
try
{
transactionLocal1.Rollback();
}
catch (MySqlException ex1)
{
MessageBox.Show("An exception of type " + ex.GetType() +
" was encountered while inserting the data.");
if (transactionLocal1.Connection != null)
{
MessageBox.Show("An exception of type " + ex1.GetType() +
" was encountered while attempting to roll
back the transaction.");
}
}
}
catch (System.Net.Sockets.SocketException ex)
{
rollbackBoolean = 1;
MessageBox.Show("Error Sockets From Commit Process " + ex.Message);
}
finally
{
connectionLocal1.Close();
}
}
// connection details function
using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
namespace mSytemNonFranchise
{
public class transactionConnectionLocal1
{
public MySqlConnection localConnection1;
public MySqlCommand command;
public transactionConnectionLocal1()
{
this.localConnection1 = new
MySqlConnection("Address='localhost';Database='localDB';User
Name='root';Password='local12';Pooling='false'");
// this.command = this.localConnection1.CreateCommand();
// this.localConnection1.Open();
}
// destructor - explicitly
~transactionConnectionLocal1()
{
//this.command.Dispose();
this.localConnection1.Close();
this.localConnection1.Dispose();
}
}
}