I am using a transctionScope in main method to calling a method to perform a update in MYSQL table. This method get an open connection from another method. When I force rollBack with a exception throw nothing happens.
try
{
using (TransactionScope scope = new TransactionScope ( ))
{
updateOnTable1(); // executes
throw new Exception("xxx something has happened test xxx");
scope.Complete(); // not executing because my exception
}
}
catch (Exception e)
{
Console.WriteLine("Cannot complete transaction:\n"+e);
}
The updateOnTable1() method
try
{
DbConnection conn = getNewConnection();
if (SGBDativo == MYSQL)
{
using (MySqlCommand sqlCommand = ((MySqlConnection)conn).CreateCommand())
{
sqlCommand.CommandType = CommandType.Text;
sqlCommand.CommandText = ".......sql code.....";
if (sqlCommand.ExecuteNonQuery() != 1)
{
throw new InvalidProgramException("SQLuser_Client Err");
}
}
}
conn.Close();
}
catch (Exception e)
{
Console.WriteLine("Error: SQLuser_Clients - del() - \n" + e.ToString());
}
The getNewConnection() method:
DbConnection connection = null;
try
{
switch (SGBDpadrao)
{
case SQLSERVER:
conn_sqlserver = new SqlConnection(string_conn_sqlserver);
conn_sqlserver.Open();
SGBDativo = SQLSERVER;
connection = conn_sqlserver;
break;
case MYSQL:
conn_mysql = new MySqlConnection(string_conn_mysql);
conn_mysql.Open();
SGBDativo = MYSQL;
connection = conn_mysql;
break;
}
}
catch (Exception)
{
return connection;
}
return connection;
But as I said before, this will not roll back. What am I missing?