ASP MVC 4 MySQL, trouble handling transaction error

James

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 ("");
    }
}
James

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Error handling in ASP .Net MVC

From Dev

ASP.NET MVC 5 error handling

From Dev

Trouble with Flex error handling

From Dev

Custom Error Handling with MVC4

From Dev

Trouble with MVC 4 DropDownListFor

From Dev

Trouble understanding error handling with monads

From Dev

Little trouble with fgets and error handling

From Dev

Handling a 'Many to One' Relationship in MVC 4 ASP.NET

From Dev

Confused with error handling in ASP.net 5 MVC 6

From Dev

Asp Mvc Handling Error when entering HTML in form

From Dev

C# asp.net mvc custom error handling with JSON

From Dev

Global exception/error handling in asp.net mvc core and dapper

From Dev

Error with the ajax and transaction in mvc

From Dev

ASP.Net MVC4 configuration error after installing MySQL Connector .NET

From Dev

mvc4 global error handling modular architecture

From Dev

ASP.NET MVC 4 FileResult - In error

From Dev

ASP.NET MVC 4 Having trouble selecting the correct input element using js/jquery

From Dev

ASP.NET MVC Custom Error Handling Application_Error Global.asax?

From Dev

Trouble with json in asp.net mvc

From Dev

MVC Custom Error handling issue

From Dev

Properly handling nested resources in ASP.net MVC 4 WebApi routing

From Dev

Weird Error Upgrading ASP.NET MVC from 4 to 5

From Dev

ASP.NET MVC 4 Bundles giving 404 error

From Dev

Customised error messages are not translated in ASP.NET MVC 4

From Dev

ASP.NET MVC 4 Database Entity Error

From Dev

ASP.NET MVC4 "Getting Started" Configuration Error

From Dev

ASP.NET MVC 4 Custom Handle Error

From Dev

ASP.NET MVC 4 error with Google CalendarAPIv3

From Dev

Using Unity in ASP.NET MVC 4 - Dependency injection error

Related Related

HotTag

Archive