Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7570969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:33:06+00:00 2026-05-30T15:33:06+00:00

Do I need to call dispose in the finally block for SqlTransaction? Pretend the

  • 0

Do I need to call dispose in the finally block for SqlTransaction? Pretend the developer didnt use USING anywhere, and just try/catch.

SqlTransaction sqlTrans = con.BeginTransaction();

try
{
     //Do Work
sqlTrans.Commit()
}
catch (Exception ex)
        {

           sqlTrans.Rollback();
        }

 finally
        {
            sqlTrans.Dispose();
            con.Dispose();
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:33:08+00:00Added an answer on May 30, 2026 at 3:33 pm

    Do I need to use try-finally or the using-statement to dispose the SqlTransaction?

    It does not hurt to have it. This is true for every class implementing IDisposable, otherwise it would not implement this interface.

    But normally the garbage collector deals with unreferenced objects(it doesn’t mean that the GC calls dispose, which isn’t true), so you need it only for unmanaged resources. But because i also don’t want to call dispose on every other variable or use the using-statement everywhere, it it’s always worth to look into the actual implementation of the class’ Dispose method.

    SqlTransaction.Dispose:

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            SNIHandle target = null;
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                target = SqlInternalConnection.GetBestEffortCleanupTarget(this._connection);
                if (!this.IsZombied && !this.IsYukonPartialZombie)
                {
                    this._internalTransaction.Dispose();
                }
            }
            catch (OutOfMemoryException e)
            {
                this._connection.Abort(e);
                throw;
            }
            catch (StackOverflowException e2)
            {
                this._connection.Abort(e2);
                throw;
            }
            catch (ThreadAbortException e3)
            {
                this._connection.Abort(e3);
                SqlInternalConnection.BestEffortCleanup(target);
                throw;
            }
        }
        base.Dispose(disposing);
    }
            
    

    Without understanding all(or anything) what is happening here i can say that this is more than a simple base.Dispose(disposing). So it might be a good idea to ensure that a SqlTransaction gets disposed.

    But because SqlConnection.BeginTransaction creates the transaction it could also be a good idea to reflect this also:

    public SqlTransaction BeginTransaction(IsolationLevel iso, string transactionName)
    {
        SqlStatistics statistics = null;
        string a = ADP.IsEmpty(transactionName) ? "None" : transactionName;
        IntPtr intPtr;
        Bid.ScopeEnter(out intPtr, "<sc.SqlConnection.BeginTransaction|API> %d#, iso=%d{ds.IsolationLevel}, transactionName='%ls'\n", this.ObjectID, (int)iso, a);
        SqlTransaction result;
        try
        {
            statistics = SqlStatistics.StartTimer(this.Statistics);
            SqlTransaction sqlTransaction = this.GetOpenConnection().BeginSqlTransaction(iso, transactionName);
            GC.KeepAlive(this);
            result = sqlTransaction;
        }
        finally
        {
            Bid.ScopeLeave(ref intPtr);
            SqlStatistics.StopTimer(statistics);
        }
        return result;
    }
    

    As you can see. The GC will also keep the Connection alive when a Transaction is created. It also doesn’t hold a reference to the transaction since it only returns it. Hence it might not be disposed even when the connection is already disposed. Another argument to dispose the transaction.

    You might also have a look at the TransactionScope class which is more fail-safe than BeginTransaction. Have a look at this question for more informations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Certainly we should call Dispose() on IDisposable objects as soon as we don't need
The question is: why do we need to call Dispose() on some objects? Why
Do I need to call Dispose() on a Graphics object obtained through PaintEventArgs in
I have learned that you need to call dispose() on Graphics objects that you
If I always need to call RemoveHandler after using AddHandler, where is the best
Need to call a filter function on some options based on a radio selected
I need to call into a Win32 API to get a series of strings,
I need to call a method that accepts a stream argument. The method loads
I need to call an external dll from c#. This is the header definition:
I need to call a VBScript file (.vbs file extension) in my C# Windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.