Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transaction not rolling back [DNET540] #517

Closed
firebird-automations opened this issue Jan 2, 2014 · 8 comments
Closed

Transaction not rolling back [DNET540] #517

firebird-automations opened this issue Jan 2, 2014 · 8 comments

Comments

@firebird-automations
Copy link

Submitted by: pritaeas (pritaeas)

When I run the below C#⁠ code (.NET 3.5 Framework) with FirebirdSql.Data.FirebirdClient V2.5.1 the TransactionScope works as expected, not inserting any records at all. If I use the V2.5.2 client then all three records are inserted into their respective tables. My connection string contains Enlist = true. I've had identical results using a local and remote database.

public partial class frmMain : Form
\{
    private TransactionScopeOption transactionScopeOption;
    private TransactionOptions transactionOptions;

    public frmMain\(\)
    \{
        InitializeComponent\(\);

        transactionScopeOption = TransactionScopeOption\.Required;
        transactionOptions = new TransactionOptions\(\)
            \{
                IsolationLevel = IsolationLevel\.ReadUncommitted,
                Timeout = TimeSpan\.FromMinutes\(2\)
            \};
    \}

    private void btnTestFb\_Click\(object sender, System\.EventArgs e\)
    \{
        FbConnection connection = new FbConnection\(ConfigurationManager\.ConnectionStrings\["ConnectionStringFb"\]\.ConnectionString\);

        try
        \{
            using \(TransactionScope scope = new TransactionScope\(transactionScopeOption, transactionOptions\)\)
            \{
                connection\.Open\(\);

                NestedInserts\(connection\);

                scope\.Complete\(\);
            \}

            connection\.Close\(\);

            MessageBox\.Show\("Done\."\);
        \}
        catch \(Exception ex\)
        \{
            MessageBox\.Show\(ex\.Message\);
        \}
    \}

    private void ExecuteNonQuery\(FbConnection connection, string commandText\)
    \{
        using \(FbCommand command = new FbCommand\(commandText, connection\)\)
        \{
            command\.ExecuteNonQuery\(\);
        \}
    \}

    private void NestedInserts\(FbConnection connection\)
    \{
        using \(TransactionScope scope = new TransactionScope\(transactionScopeOption, transactionOptions\)\)
        \{
            InsertA\(connection\);

            scope\.Complete\(\);
        \}
    \}

    private void InsertA\(FbConnection connection\)
    \{
        using \(TransactionScope scope = new TransactionScope\(transactionScopeOption, transactionOptions\)\)
        \{
            ExecuteNonQuery\(connection, "INSERT INTO TABLE\_A \(DESCRIPTION\) VALUES \('A'\)"\);

            InsertB\(connection\);

            scope\.Complete\(\);
        \}
    \}

    private void InsertB\(FbConnection connection\)
    \{
        using \(TransactionScope scope = new TransactionScope\(transactionScopeOption, transactionOptions\)\)
        \{
            ExecuteNonQuery\(connection, "INSERT INTO TABLE\_B \(DESCRIPTION\) VALUES \('B'\)"\);

            InsertC\(connection\);

            scope\.Complete\(\);
        \}
    \}

    private void InsertC\(FbConnection connection\)
    \{
        using \(TransactionScope scope = new TransactionScope\(transactionScopeOption, transactionOptions\)\)
        \{
            ExecuteNonQuery\(connection, "INSERT INTO TABLE\_C \(DESCRIPTION\) VALUES \('C'\)"\);

            InsertD\(connection\);

            scope\.Complete\(\);
        \}
    \}

    private void InsertD\(FbConnection connection\)
    \{
        using \(TransactionScope scope = new TransactionScope\(\)\)
        \{
            throw new Exception\("Fb exception thrown\!"\);

            scope\.Complete\(\);
        \}
    \}
@firebird-automations
Copy link
Author

Commented by: @cincuranet

Why it should not insert records when you're calling `scope.Complete();`?

@firebird-automations
Copy link
Author

Modified by: @cincuranet

status: Open [ 1 ] => In Progress [ 3 ]

@firebird-automations
Copy link
Author

Commented by: pritaeas (pritaeas)

Why? Because the lowest method InsertD() throws an exception, breaking out of the method, and thus not calling scope.Complete in the lowest level. All others higher up in the hierarchy should be rolled back as well.

@firebird-automations
Copy link
Author

Commented by: @cincuranet

Ahh, I see. I missed the exception.

@firebird-automations
Copy link
Author

Commented by: @cincuranet

I'm testing your code now with latest version of provider and everything works as expected. Can you try latest version?

@firebird-automations
Copy link
Author

Commented by: pritaeas (pritaeas)

It was only a problem in the version mentioned (2.5.2). And no, I cannot test anymore, since the code has changed drastically since the reporting of this issue 18 months ago.

@firebird-automations
Copy link
Author

Commented by: @cincuranet

OK, so I'll close it.

@firebird-automations
Copy link
Author

Modified by: @cincuranet

status: In Progress [ 3 ] => Closed [ 6 ]

resolution: Cannot Reproduce [ 5 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants