My Environment/Application: Ubuntu(x64) Mono xsp4 mysql(strict_all_tables on) ASP.NET MVC4 Elmah
I'm trying to use ADO.NET and wrap my requests in a transaction. I have a table with one column that is datatype integer. I try inserting 2 integers and then an invalid datatype. My goal is to get the transaction to rollback when it tries to insert the invalid data. mysql server is throwing the error when trying to execute the sql command from MySQL Workbench.
I set a breakpoint on the commit, rollback, and close lines of code. The commit line is hit, ten the close line is hit. However the server does return a 500 error(YSOD) with the sql exception, but my exception handling does not trap an error and rollback the transaction.
public class BaseController : Controller
{
private MySqlConnection _connection;
private MySqlTransaction _transaction;
protected override void OnActionExecuting (ActionExecutingContext filterContext)
{
_connection = new MySqlConnection (ConfigurationManager.ConnectionStrings["myconnstringname"].ConnectionString);
_connection.Open ();
_transaction = _connection.BeginTransaction ();
base.OnActionExecuting (filterContext);
}
protected override void OnActionExecuted (ActionExecutedContext filterContext)
{
try
{
_transaction.Commit ();
}
catch(Exception ex)
{
_transaction.Rollback();
throw ex;
}
finally
{
_connection.Close ();
}
base.OnActionExecuted (filterContext);
}
protected MySqlCommand GetCommand(string commandText)
{
return new MySqlCommand (commandText, _connection, _transaction);
}
}
public class HomeController : BaseController
{
public ActionResult Index ()
{
var cmd = GetCommand ("insert into testing select 1;");
cmd.ExecuteNonQuery ();
cmd = GetCommand ("insert into testing select 2;");
cmd.ExecuteNonQuery ();
cmd = GetCommand ("insert into testing select 'grgregre';");
cmd.ExecuteNonQuery ();
return Content ("");
}
}
Figured out the problem. I derped this one. Apparently the exception was being thrown on the line cmd.ExecuteNonQuery(); instead of when the transaction was committed.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments