scope of command and garbage collection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Smokey Grindle

    #1

    scope of command and garbage collection

    Say I have the following piece of code

    Dim dtPerson As New DataTable
    Dim cmd As New SqlClient.SqlCo mmand("sp_test" , database)
    dtPerson.Load(c md.ExecuteReade r)
    cmd.dispose()

    which instanciates the command object, uses it then destroys it, now if i
    rewrote it like this

    Dim dtPerson As New DataTable
    dtPerson.Load(N ew SqlClient.SqlCo mmand("test", database)

    would that still act the same? because the commands scope is inside the load
    method, shouldnt it be destroyed after the point of execution has passed the
    load method? basicly creating a command that destroys itself after
    execution? or is it better not to do it this way because that is not how it
    works?


  • Herfried K. Wagner [MVP]

    #2
    Re: scope of command and garbage collection

    "Smokey Grindle" <nospamhere@don tspam.net> schrieb:[color=blue]
    > Say I have the following piece of code
    >
    > Dim dtPerson As New DataTable
    > Dim cmd As New SqlClient.SqlCo mmand("sp_test" , database)
    > dtPerson.Load(c md.ExecuteReade r)
    > cmd.dispose()
    >
    > which instanciates the command object, uses it then destroys it, now if i
    > rewrote it like this
    >
    > Dim dtPerson As New DataTable
    > dtPerson.Load(N ew SqlClient.SqlCo mmand("test", database)
    >
    > would that still act the same? because the commands scope is inside the
    > load method, shouldnt it be destroyed after the point of execution has
    > passed the load method?[/color]

    No, it won't. It will be destoyed by the garbage collector later, but there
    is no guarantee that it gets destroyed immediately.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: scope of command and garbage collection

      Smokey,
      Addition to Herfried's comments.

      In .NET 2.0 I would recommend:

      | Dim dtPerson As New DataTable
      Using cmd As New SqlClient.SqlCo mmand("sp_test" , database)
      | dtPerson.Load(c md.ExecuteReade r)
      End Using

      If .NET 1.x I would recommend:

      | Dim dtPerson As New DataTable
      | Dim cmd As New SqlClient.SqlCo mmand("sp_test" , database)
      Try
      | dtPerson.Load(c md.ExecuteReade r)
      Finally
      | cmd.dispose()
      End Try

      This ensures that the SqlCommand is disposed of even if the DataTable.Load
      method itself threw an exception.

      --
      Hope this helps
      Jay B. Harlow [MVP - Outlook]
      ..NET Application Architect, Enthusiast, & Evangelist
      T.S. Bradley - http://www.tsbradley.net


      "Smokey Grindle" <nospamhere@don tspam.net> wrote in message
      news:eOBMA7yfGH A.5088@TK2MSFTN GP02.phx.gbl...
      | Say I have the following piece of code
      |
      | Dim dtPerson As New DataTable
      | Dim cmd As New SqlClient.SqlCo mmand("sp_test" , database)
      | dtPerson.Load(c md.ExecuteReade r)
      | cmd.dispose()
      |
      | which instanciates the command object, uses it then destroys it, now if i
      | rewrote it like this
      |
      | Dim dtPerson As New DataTable
      | dtPerson.Load(N ew SqlClient.SqlCo mmand("test", database)
      |
      | would that still act the same? because the commands scope is inside the
      load
      | method, shouldnt it be destroyed after the point of execution has passed
      the
      | load method? basicly creating a command that destroys itself after
      | execution? or is it better not to do it this way because that is not how
      it
      | works?
      |
      |


      Comment

      • Smokey Grindle

        #4
        Re: scope of command and garbage collection

        Yes, I was also looking at USING.

        thanks both

        "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.net> wrote in
        message news:eJEJTKzfGH A.2208@TK2MSFTN GP05.phx.gbl...[color=blue]
        > Smokey,
        > Addition to Herfried's comments.
        >
        > In .NET 2.0 I would recommend:
        >
        > | Dim dtPerson As New DataTable
        > Using cmd As New SqlClient.SqlCo mmand("sp_test" , database)
        > | dtPerson.Load(c md.ExecuteReade r)
        > End Using
        >
        > If .NET 1.x I would recommend:
        >
        > | Dim dtPerson As New DataTable
        > | Dim cmd As New SqlClient.SqlCo mmand("sp_test" , database)
        > Try
        > | dtPerson.Load(c md.ExecuteReade r)
        > Finally
        > | cmd.dispose()
        > End Try
        >
        > This ensures that the SqlCommand is disposed of even if the DataTable.Load
        > method itself threw an exception.
        >
        > --
        > Hope this helps
        > Jay B. Harlow [MVP - Outlook]
        > .NET Application Architect, Enthusiast, & Evangelist
        > T.S. Bradley - http://www.tsbradley.net
        >
        >
        > "Smokey Grindle" <nospamhere@don tspam.net> wrote in message
        > news:eOBMA7yfGH A.5088@TK2MSFTN GP02.phx.gbl...
        > | Say I have the following piece of code
        > |
        > | Dim dtPerson As New DataTable
        > | Dim cmd As New SqlClient.SqlCo mmand("sp_test" , database)
        > | dtPerson.Load(c md.ExecuteReade r)
        > | cmd.dispose()
        > |
        > | which instanciates the command object, uses it then destroys it, now if
        > i
        > | rewrote it like this
        > |
        > | Dim dtPerson As New DataTable
        > | dtPerson.Load(N ew SqlClient.SqlCo mmand("test", database)
        > |
        > | would that still act the same? because the commands scope is inside the
        > load
        > | method, shouldnt it be destroyed after the point of execution has passed
        > the
        > | load method? basicly creating a command that destroys itself after
        > | execution? or is it better not to do it this way because that is not how
        > it
        > | works?
        > |
        > |
        >
        >[/color]


        Comment

        • Robin Mark Tucker

          #5
          Re: scope of command and garbage collection


          Hi Jay,

          Can you explain a little more about the difference between the Using in .NET
          2.0 and the .NET 1.1 version of the code? I am new to .NET 2.0, but quite
          experienced with .NET 1.1. I had mostly used the Try/Catch method, but I'm
          thinking .NET 2.0 wraps a hidden exception handler around the Using
          statement for just such eventualities. Is there an MSDN document about
          this?

          Thanks,


          Robin

          "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.net> wrote in
          message news:eJEJTKzfGH A.2208@TK2MSFTN GP05.phx.gbl...[color=blue]
          > Smokey,
          > Addition to Herfried's comments.
          >
          > In .NET 2.0 I would recommend:
          >
          > | Dim dtPerson As New DataTable
          > Using cmd As New SqlClient.SqlCo mmand("sp_test" , database)
          > | dtPerson.Load(c md.ExecuteReade r)
          > End Using
          >
          > If .NET 1.x I would recommend:
          >
          > | Dim dtPerson As New DataTable
          > | Dim cmd As New SqlClient.SqlCo mmand("sp_test" , database)
          > Try
          > | dtPerson.Load(c md.ExecuteReade r)
          > Finally
          > | cmd.dispose()
          > End Try
          >
          > This ensures that the SqlCommand is disposed of even if the DataTable.Load
          > method itself threw an exception.
          >
          > --
          > Hope this helps
          > Jay B. Harlow [MVP - Outlook]
          > .NET Application Architect, Enthusiast, & Evangelist
          > T.S. Bradley - http://www.tsbradley.net
          >
          >
          > "Smokey Grindle" <nospamhere@don tspam.net> wrote in message
          > news:eOBMA7yfGH A.5088@TK2MSFTN GP02.phx.gbl...
          > | Say I have the following piece of code
          > |
          > | Dim dtPerson As New DataTable
          > | Dim cmd As New SqlClient.SqlCo mmand("sp_test" , database)
          > | dtPerson.Load(c md.ExecuteReade r)
          > | cmd.dispose()
          > |
          > | which instanciates the command object, uses it then destroys it, now if
          > i
          > | rewrote it like this
          > |
          > | Dim dtPerson As New DataTable
          > | dtPerson.Load(N ew SqlClient.SqlCo mmand("test", database)
          > |
          > | would that still act the same? because the commands scope is inside the
          > load
          > | method, shouldnt it be destroyed after the point of execution has passed
          > the
          > | load method? basicly creating a command that destroys itself after
          > | execution? or is it better not to do it this way because that is not how
          > it
          > | works?
          > |
          > |
          >
          >[/color]


          Comment

          • Cor Ligthert [MVP]

            #6
            Re: scope of command and garbage collection

            Jay,

            I would not use "using" without global exception handling or without forcing
            the errors to the datarow.rowerro r property.

            I know that you use probably the first and I the last, however in my opinion
            is it good to explain that.

            Cor


            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: scope of command and garbage collection

              Cor,
              Agree, using (no pun intended) a global exception handler is a good idea
              along with appropriate "local" error handling if appropriate.


              --
              Hope this helps
              Jay B. Harlow [MVP - Outlook]
              ..NET Application Architect, Enthusiast, & Evangelist
              T.S. Bradley - http://www.tsbradley.net


              "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
              news:%23GVZjO1f GHA.4940@TK2MSF TNGP05.phx.gbl. ..
              | Jay,
              |
              | I would not use "using" without global exception handling or without
              forcing
              | the errors to the datarow.rowerro r property.
              |
              | I know that you use probably the first and I the last, however in my
              opinion
              | is it good to explain that.
              |
              | Cor
              |
              |


              Comment

              • Jay B. Harlow [MVP - Outlook]

                #8
                Re: scope of command and garbage collection

                Robin,
                The Using statement is effectively:

                Dim dtPerson As New DataTable

                Using cmd As New SqlClient.SqlCo mmand("sp_test" , database)
                + Dim cmd As New SqlClient.SqlCo mmand("sp_test" , database)
                + Try
                dtPerson.Load(c md.ExecuteReade r)
                End Using
                + Finally
                + If cmd IsNot Nothing
                + cmd.dispose()
                + End If
                +End Try

                Where the lines marked with + are what VB creates in the IL. You can use
                ILDASM to see specifically what VB does.

                The IsNot Nothing is there to allow for the case where cmd is set to nothing
                within the Using block.

                For details of the new statement see:

                http://msdn2.microsoft.com/en-US/library/htd05whh.aspx

                --
                Hope this helps
                Jay B. Harlow [MVP - Outlook]
                ..NET Application Architect, Enthusiast, & Evangelist
                T.S. Bradley - http://www.tsbradley.net


                "Robin Mark Tucker" <robintuckerhom e@removehotmail .comremove> wrote in
                message news:e51m7d$jl1 $1$8302bc10@new s.demon.co.uk.. .
                |
                | Hi Jay,
                |
                | Can you explain a little more about the difference between the Using in
                ..NET
                | 2.0 and the .NET 1.1 version of the code? I am new to .NET 2.0, but quite
                | experienced with .NET 1.1. I had mostly used the Try/Catch method, but
                I'm
                | thinking .NET 2.0 wraps a hidden exception handler around the Using
                | statement for just such eventualities. Is there an MSDN document about
                | this?
                |
                | Thanks,
                |
                |
                | Robin
                |
                | "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @tsbradley.net> wrote in
                | message news:eJEJTKzfGH A.2208@TK2MSFTN GP05.phx.gbl...
                | > Smokey,
                | > Addition to Herfried's comments.
                | >
                | > In .NET 2.0 I would recommend:
                | >
                | > | Dim dtPerson As New DataTable
                | > Using cmd As New SqlClient.SqlCo mmand("sp_test" , database)
                | > | dtPerson.Load(c md.ExecuteReade r)
                | > End Using
                | >
                | > If .NET 1.x I would recommend:
                | >
                | > | Dim dtPerson As New DataTable
                | > | Dim cmd As New SqlClient.SqlCo mmand("sp_test" , database)
                | > Try
                | > | dtPerson.Load(c md.ExecuteReade r)
                | > Finally
                | > | cmd.dispose()
                | > End Try
                | >
                | > This ensures that the SqlCommand is disposed of even if the
                DataTable.Load
                | > method itself threw an exception.
                | >
                | > --
                | > Hope this helps
                | > Jay B. Harlow [MVP - Outlook]
                | > .NET Application Architect, Enthusiast, & Evangelist
                | > T.S. Bradley - http://www.tsbradley.net
                | >
                | >
                | > "Smokey Grindle" <nospamhere@don tspam.net> wrote in message
                | > news:eOBMA7yfGH A.5088@TK2MSFTN GP02.phx.gbl...
                | > | Say I have the following piece of code
                | > |
                | > | Dim dtPerson As New DataTable
                | > | Dim cmd As New SqlClient.SqlCo mmand("sp_test" , database)
                | > | dtPerson.Load(c md.ExecuteReade r)
                | > | cmd.dispose()
                | > |
                | > | which instanciates the command object, uses it then destroys it, now
                if
                | > i
                | > | rewrote it like this
                | > |
                | > | Dim dtPerson As New DataTable
                | > | dtPerson.Load(N ew SqlClient.SqlCo mmand("test", database)
                | > |
                | > | would that still act the same? because the commands scope is inside
                the
                | > load
                | > | method, shouldnt it be destroyed after the point of execution has
                passed
                | > the
                | > | load method? basicly creating a command that destroys itself after
                | > | execution? or is it better not to do it this way because that is not
                how
                | > it
                | > | works?
                | > |
                | > |
                | >
                | >
                |
                |


                Comment

                Working...