Does SqlTransaction need to have Dispose called?

后端 未结 3 617
情深已故
情深已故 2020-12-19 04:42

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

SqlTransaction sqlTra         


        
3条回答
  •  一整个雨季
    2020-12-19 05:21

    In the general case, every IDisposable object you construct or obtain, and own, you have to dispose of.

    In the specific case, there are some exceptions, but SqlTransaction is not one of them.

    As per the the documentation of SqlTransaction.Dispose:

    Releases the unmanaged resources used by the DbTransaction and optionally releases the managed resources.

    (my emphasis)

    Since the documentation does not state that those unmanaged resources are released when you issue a commit or a rollback, you will need to dispose of this object.

提交回复
热议问题