我在main方法中使用transctionScope来调用在MYSQL表中执行更新的方法。此方法从另一个方法获得打开的连接。当我强制rollBack异常抛出时没有任何反应。
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);
}
updateOnTable1()方法
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());
}
getNewConnection()方法:
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;
但正如我之前所说,这不会回滚。 我错过了什么?