Solved

Transaction Rollback not working using TableAdapters

Posted on 2008-09-29
2
1,834 Views
Last Modified: 2013-12-17
I'm attempting to use Transactions while using TableAdapters.  I created partial classes for each of my table adapters from some code I found online.  I'd rather not use Transaction Scopes since I read it's inefficient. My code snippets contain the partial classes I created as well as my code to implement the transactions.

I'm noticing that when an exception occurs, insert statements are still committing.

Any thoughts?
****** PARTIAL CLASSES ********
    partial class PASSWORDSTableAdapter
    {
        private OracleTransaction _transaction;
        public OracleTransaction Transaction
        {
            get { return _transaction; }
            set
            {
                _transaction = value;
                _connection = _transaction.Connection;
 
                if (_adapter == null)
                    this.InitAdapter();
 
                foreach (OracleCommand command in this.CommandCollection)
                {
                    command.Transaction = _transaction;
                }
 
                this.Adapter.InsertCommand.Transaction = _transaction;
                this.Adapter.UpdateCommand.Transaction = _transaction;
                this.Adapter.DeleteCommand.Transaction = _transaction;
            }
        }
    }
 
    partial class ACCOUNT_PASSWORDSTableAdapter
    {
        private OracleTransaction _transaction;
        public OracleTransaction Transaction
        {
            get { return _transaction; }
            set
            {
                _transaction = value;
                _connection = _transaction.Connection;
 
                if (_adapter == null)
                    this.InitAdapter();
 
                foreach (OracleCommand command in this.CommandCollection)
                {
                    command.Transaction = _transaction;
                }
 
                this.Adapter.InsertCommand.Transaction = _transaction;
 
            }
        }
    }
 
****** TRANSACTION CODE ********
        PASSWORDSTableAdapter ta1 = new PASSWORDSTableAdapter();
        ACCOUNT_PASSWORDSTableAdapter ta2 = new ACCOUNT_PASSWORDSTableAdapter();
        OracleTransaction dbTransaction = null;
        OracleConnection taConn = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
 
        try
        {
            taConn.Open();
            dbTransaction = taConn.BeginTransaction();
            ta1.Transaction = dbTransaction;
            ta2.Transaction = dbTransaction;
 
            // insert password
            ta1.Insert(0, txtPassword.Text.ToString(), ddGlobal.SelectedValue.ToString());
 
            // encrypt/store password password
            using (OracleCommand cmd2 = new OracleCommand("security.encrypt_password",
                dbTransaction.Connection, dbTransaction))
            {
                cmd2.CommandType = CommandType.StoredProcedure;
                cmd2.Parameters.Add("key_in", OracleType.VarChar).Value = txtEncryptionKey.Text.ToString();
                cmd2.Parameters.Add("p_password_id", OracleType.Number).Value = myNewPasswordID;
                cmd2.ExecuteNonQuery();
            }
 
            // store system id, account id and password id in related table
            ta2.Insert(decimal.Parse(ddSystems.SelectedValue.ToString()), decimal.Parse(ddAccounts.SelectedValue.ToString()), myNewPasswordID);
 
            dbTransaction.Commit();
        }
 
        catch (Exception ex)
        {
            dbTransaction.Rollback();
            lblExceptionDetails.Visible = true;
            lblExceptionDetails.Text = "There was a problem inserting the password. ";
        }
        finally
        {
            if (dbTransaction != null) dbTransaction.Dispose();
            ta1.Dispose();
            ta2.Dispose();
        }

Open in new window

0
Comment
Question by:peerlesslv
  • 2
2 Comments
 

Author Comment

by:peerlesslv
ID: 22601939
Is there anybody out there?
0
 

Accepted Solution

by:
peerlesslv earned 0 total points
ID: 22664689
It turns out it's the Stored Procedure that I call that wasn't being Rolled Back.  There was a commit statement in the Stored Procedure which obviously made it impossible to Roll it Back.  
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question