Programming - Best Practice

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aaron.kempf@gmail.com

    #16
    Re: Programming - Best Practice

    re:
    Be aware that, under the hood, open / close don't actually open or close
    database connections. IT's more "Check out from pool", "Check back into
    pool". It's up the pool as to when the connections are actually opened and
    closed.


    it's OBVIOUS that Microsoft and you DOTNET _FAGS_ are on the losing
    side of this argument.

    UNNECESSARY CHANGE IS NOT SEXY
    UNNECESSARY CHANGE IS NOT EFFICIENT
    UNNECESSARY CHANGE IS NOT ACCEPTABLE



    On Feb 16, 2:13 pm, "Chris Mullins [MVP]" <cmull...@yahoo .comwrote:
    I think you misunderstood what the MS-Press books were saying.
    >
    The opening and closing of ADO.NET connections, along with the implications
    this has on the connection pool, can catch people by surprise.
    >
    The general rule for your code is open / execute / close - this is how best
    you're able to work with the connection pool. There's nothing stopping you
    from doing other things though - like holding connections open, or any of a
    dozen other (bad) things.
    >
    Be aware that, under the hood, open / close don't actually open or close
    database connections. IT's more "Check out from pool", "Check back into
    pool". It's up the pool as to when the connections are actually opened and
    closed.
    >
    --
    Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVPhttp://www.coversant.c om/blogs/cmullins
    >
    "PFC Sadr" <pfc_s...@hotma il.comwrote in message
    >
    news:1171663581 .468393.248680@ a75g2000cwd.goo glegroups.com.. .
    >
    uh a half dozen MS press books???
    uh a half dozen MS press books???
    uh a half dozen MS press books???
    >
    get a spid; get another SPID.
    >
    DO THEY MATCH?
    >
    because if they do then I'll accept a written apology from Microsoft
    and new editions of their books that are CORRECTED.
    >
    On Feb 16, 1:48 pm, "Brian Gideon" <briangid...@ya hoo.comwrote:
    On Feb 16, 3:00 pm, pfc_s...@hotmai l.com wrote:
    >
    I didn't think that you could even leave a connection open.
    >
    it's one of my biggest complaints about ADO.net; I used to use @@SPID
    similiar to sessionID in ASP in order to build some simple apps...
    >
    but now there is nothing like that in .NET from what I understand
    >
    I just don't understand; why do you even need to close a connection if
    you can't leave a connection OPEN?
    >
    like I'm being serious and honest here.
    >
    Thanks
    >
    Hi,
    >
    What made you think you couldn't leave a connection open?
    >
    Brian

    Comment

    • aaron.kempf@gmail.com

      #17
      Re: Programming - Best Practice

      I'd prefer to leave the connection open so that I can _POSSIBLY_
      troubleshoot things on the database side.

      ESPECIALLY---- ESPECILALLY-- DURING DEVELOPMENT
      ESPECIALLY---- ESPECILALLY-- DURING DEVELOPMENT
      ESPECIALLY---- ESPECILALLY-- DURING DEVELOPMENT
      ESPECIALLY---- ESPECILALLY-- DURING DEVELOPMENT


      On Feb 16, 2:33 pm, "Herfried K. Wagner [MVP]" <hirf-spam-me-
      h...@gmx.atwrot e:
      "bigHairy" <mthomas1...@gm ail.comschrieb:
      >
      >
      >
      A number of examples I have followed have the following in their
      finally statement
      >
      Try
      .....
      Catch
      .....
      Finally
      >
      If (Not IsNothing(dbCon n)) Then
      dbConn.Close()
      End If
      >
      End Try
      >
      Will my data connection always close using this method or should I
      just use dbConn.close?
      >
      It will always close because the code inside the 'Finally' branch will be
      executed even if the the procedure is left using 'Return' or 'Exit *' inside
      'Try' and 'Catch'.
      >
      However, I'd prefer this way to write down the code in the 'Finally' block:
      >
      \\\
      If dbConn IsNot Nothing Then
      dbConn.Close()
      End If
      ///
      >
      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

      Comment

      • aaron.kempf@gmail.com

        #18
        Re: Programming - Best Practice

        and for the record, I didn't 'mis-understand' the MS Press book.

        DO _NOT_ TRY TO DISCREDIT ME.

        I HAVE SERIOUS, MILITANT GRIEVANCES.
        UNLESS YOU CAN SHOW ME HOW TO KEEP A SINGLE SIMPLE CONNECTION OPEN I
        AM STILL AT _WAR_ WITH MS AND ALL MVP

        THANKS AND SORRY

        -Aaron




        On Feb 16, 2:13 pm, "Chris Mullins [MVP]" <cmull...@yahoo .comwrote:
        I think you misunderstood what the MS-Press books were saying.
        >
        The opening and closing of ADO.NET connections, along with the implications
        this has on the connection pool, can catch people by surprise.
        >
        The general rule for your code is open / execute / close - this is how best
        you're able to work with the connection pool. There's nothing stopping you
        from doing other things though - like holding connections open, or any of a
        dozen other (bad) things.
        >
        Be aware that, under the hood, open / close don't actually open or close
        database connections. IT's more "Check out from pool", "Check back into
        pool". It's up the pool as to when the connections are actually opened and
        closed.
        >
        --
        Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVPhttp://www.coversant.c om/blogs/cmullins
        >
        "PFC Sadr" <pfc_s...@hotma il.comwrote in message
        >
        news:1171663581 .468393.248680@ a75g2000cwd.goo glegroups.com.. .
        >
        uh a half dozen MS press books???
        uh a half dozen MS press books???
        uh a half dozen MS press books???
        >
        get a spid; get another SPID.
        >
        DO THEY MATCH?
        >
        because if they do then I'll accept a written apology from Microsoft
        and new editions of their books that are CORRECTED.
        >
        On Feb 16, 1:48 pm, "Brian Gideon" <briangid...@ya hoo.comwrote:
        On Feb 16, 3:00 pm, pfc_s...@hotmai l.com wrote:
        >
        I didn't think that you could even leave a connection open.
        >
        it's one of my biggest complaints about ADO.net; I used to use @@SPID
        similiar to sessionID in ASP in order to build some simple apps...
        >
        but now there is nothing like that in .NET from what I understand
        >
        I just don't understand; why do you even need to close a connection if
        you can't leave a connection OPEN?
        >
        like I'm being serious and honest here.
        >
        Thanks
        >
        Hi,
        >
        What made you think you couldn't leave a connection open?
        >
        Brian

        Comment

        • Cor Ligthert [MVP]

          #19
          Re: Programming - Best Practice

          >
          Not really. Imagine an exception gets thrown when opening the connection.
          >
          --
          Absolutely not Herfried that is in your control, I did not say you should
          not use Try and Catch. I mean there should not be needed a question as "If
          connection is open"

          Cor


          Comment

          • Cor Ligthert [MVP]

            #20
            Re: Programming - Best Practice

            PFC,

            By using a dataadapter you don't even have to open and close a connection as
            it is about one action.
            (dataset, datatable, datarow). It does it completely self. If it was closed
            it opens and closed it and leaves it in the state it was as it is closed.

            If you have more tabels datasets to do in a row, than it can be more
            efficient to open that only at the begin and close it at the end, something
            as

            Try
            open myconnection
            try
            do my stuff
            do my stuff
            do my stuff
            catch the problems in the stuff
            handle the problems in my stuff
            end try
            Catch the problems in my connection
            handle the problems in my connection
            End Catch

            Cor

            <pfc_sadr@hotma il.comschreef in bericht
            news:1171659636 .108644.239340@ q2g2000cwa.goog legroups.com...
            >I didn't think that you could even leave a connection open.
            >
            it's one of my biggest complaints about ADO.net; I used to use @@SPID
            similiar to sessionID in ASP in order to build some simple apps...
            >
            but now there is nothing like that in .NET from what I understand
            >
            I just don't understand; why do you even need to close a connection if
            you can't leave a connection OPEN?
            >
            >
            like I'm being serious and honest here.
            >
            Thanks
            >
            >
            >
            >
            >
            On Feb 16, 12:04 pm, "Stephany Young" <noone@localhos twrote:
            >The issue here has nothing to do with whether or not the database
            >connection
            >is open. It is to do with whether or not the database connection object
            >has
            >been instantiated.
            >>
            >You will probabaly note that the 'examples' have the dbConn variable
            >declared before the Try. This means that the dbConn variable is 'scoped'
            >so
            >that it is available to the Finally block.
            >>
            >With both the SqlConnection and OleDbConnection classes, calling Close on
            >an
            >object of those types that is not open will NOT cause an exception.
            >However,
            >if you call Close on an object of those types that has NOT been
            >instantiated
            >(Is Nothing) then an exception WILL be thrown.
            >>
            >In the Try block, if an exception is thrown before dbConn is instantiated
            >(let alone opened), the logic will 'jump' directly to the Catch block and
            >then fall through to the Finally block. At this point dbConn would be
            >Nothing and a call to dbConn.Close would fail. The conditional execution
            >of
            >dbConn.Close handles this situation.
            >>
            >Because the code Finally block is executed regardless of whether an
            >exception was thrown or not, having the conditional execution of
            >dbConn.Close means that you do not need to worrying about closing the
            >connection in either the Try or Catch blocks.
            >>
            >"bigHairy" <mthomas1...@gm ail.comwrote in message
            >>
            >news:117163371 1.929140.133200 @m58g2000cwm.go oglegroups.com. ..
            >>
            Hello.
            >>
            I have been teaching myself .NET over the last few months and have had
            some success. I would like to ask a question though...
            >>
            A number of examples I have followed have the following in their
            finally statement
            >>
            Try
            .....
            Catch
            .....
            Finally
            >>
            If (Not IsNothing(dbCon n)) Then
            dbConn.Close()
            End If
            >>
            End Try
            >>
            Will my data connection always close using this method or should I
            just use dbConn.close?
            >>
            I have a program that seems to be holding onto its connection - am I
            missing something?
            >
            >

            Comment

            • Herfried K. Wagner [MVP]

              #21
              Re: Programming - Best Practice

              "Cor Ligthert [MVP]" <notmyfirstname @planet.nlschri eb:
              >Not really. Imagine an exception gets thrown when opening the
              >connection.
              >>
              Absolutely not Herfried that is in your control, I did not say you should
              not use Try and Catch. I mean there should not be needed a question as "If
              connection is open"
              Why not? Pseudo code:

              \\\
              Dim c As New Connection()
              Try
              c.Open()
              c.DoSomething()
              Catch
              ...
              Finally
              c.Close()
              End Try
              ///

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

              Comment

              • Cor Ligthert [MVP]

                #22
                Re: Programming - Best Practice

                Herfried,

                In your sample you don't know if it is an opening error or a processing
                error.

                It depend of course of the situation, I think that in a common situation (I
                don't remember me that I did the first one in a real situation) that I am to
                lazy for the first one, however asked was Best Practice.

                Your sample is often used to show how finally can work. In my opinion that
                is not because it is the best way how to do an open and close but there are
                not so many more simple samples.

                :-)

                Cor

                "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atschr eef in bericht
                news:%23gnhHPpU HHA.1636@TK2MSF TNGP02.phx.gbl. ..
                "Cor Ligthert [MVP]" <notmyfirstname @planet.nlschri eb:
                >>Not really. Imagine an exception gets thrown when opening the
                >>connection.
                >>>
                >Absolutely not Herfried that is in your control, I did not say you should
                >not use Try and Catch. I mean there should not be needed a question as
                >"If connection is open"
                >
                Why not? Pseudo code:
                >
                \\\
                Dim c As New Connection()
                Try
                c.Open()
                c.DoSomething()
                Catch
                ...
                Finally
                c.Close()
                End Try
                ///
                >
                --
                M S Herfried K. Wagner
                M V P <URL:http://dotnet.mvps.org/>
                V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

                Comment

                • Spam Catcher

                  #23
                  Re: Programming - Best Practice

                  pfc_sadr@hotmai l.com wrote in news:1171659636 .108644.239340
                  @q2g2000cwa.goo glegroups.com:
                  I just don't understand; why do you even need to close a connection if
                  you can't leave a connection OPEN?
                  It depends on the reader you use. If you use a data adapter, it'll close
                  for you.

                  However, datareaders use a firehose mode to read from the database, thus
                  you need to manually close it when you're done with the reader/connection.

                  Comment

                  • Stephany Young

                    #24
                    Re: Programming - Best Practice

                    That is not correct.

                    When a DataAdapter 'starts' it's operation it determines thae current state
                    of the Connection. If the Connection was already open then it leaves it open
                    when it finishes. If it needs to open the Connection then it closes the
                    Connection when it finishes.

                    In other words, it leaves the Connection in the state that it found it.


                    "Spam Catcher" <spamhoneypot@r ogers.comwrote in message
                    news:Xns98DA9DE DBD6Cusenethone ypotrogers@127. 0.0.1...
                    pfc_sadr@hotmai l.com wrote in news:1171659636 .108644.239340
                    @q2g2000cwa.goo glegroups.com:
                    >
                    >I just don't understand; why do you even need to close a connection if
                    >you can't leave a connection OPEN?
                    >
                    It depends on the reader you use. If you use a data adapter, it'll close
                    for you.
                    >
                    However, datareaders use a firehose mode to read from the database, thus
                    you need to manually close it when you're done with the reader/connection.

                    Comment

                    • aaron.kempf@gmail.com

                      #25
                      Re: Programming - Best Practice

                      Spam

                      that is not an acceptable answer.

                      Your ADO.net _CRAP_ is unusable

                      UNNECESSARY CHANGE IS NOT SEXY
                      UNNECESSARY CHANGE IS NOT WANTED
                      UNNECESSARY CHANGE IS NOT ACCEPTABLE






                      On Feb 17, 12:31 pm, Spam Catcher <spamhoney...@r ogers.comwrote:
                      pfc_s...@hotmai l.com wrote in news:1171659636 .108644.239340
                      @q2g2000cwa.goo glegroups.com:
                      >
                      I just don't understand; why do you even need to close a connection if
                      you can't leave a connection OPEN?
                      >
                      It depends on the reader you use. If you use a data adapter, it'll close
                      for you.
                      >
                      However, datareaders use a firehose mode to read from the database, thus
                      you need to manually close it when you're done with the reader/connection.

                      Comment

                      • aaron.kempf@gmail.com

                        #26
                        Re: Programming - Best Practice

                        Stephany

                        I don't care whether is 'says' it is open or not.
                        it does not maintain the same spid; you and Microsoft merely play word
                        games.

                        ADO.net is not usable, UNNECESSARY CHANGE IS NOT ACCEPTABLE






                        On Feb 17, 12:38 pm, "Stephany Young" <noone@localhos twrote:
                        That is not correct.
                        >
                        When a DataAdapter 'starts' it's operation it determines thae current state
                        of the Connection. If the Connection was already open then it leaves it open
                        when it finishes. If it needs to open the Connection then it closes the
                        Connection when it finishes.
                        >
                        In other words, it leaves the Connection in the state that it found it.
                        >
                        "Spam Catcher" <spamhoney...@r ogers.comwrote in message
                        >
                        news:Xns98DA9DE DBD6Cusenethone ypotrogers@127. 0.0.1...
                        >
                        >
                        >
                        pfc_s...@hotmai l.com wrote in news:1171659636 .108644.239340
                        @q2g2000cwa.goo glegroups.com:
                        >
                        I just don't understand; why do you even need to close a connection if
                        you can't leave a connection OPEN?
                        >
                        It depends on the reader you use. If you use a data adapter, it'll close
                        for you.
                        >
                        However, datareaders use a firehose mode to read from the database, thus
                        you need to manually close it when you're done with the reader/connection.- Hide quoted text -
                        >
                        - Show quoted text -

                        Comment

                        • PFC Sadr

                          #27
                          Re: Programming - Best Practice

                          Spam

                          ok.. ok.. how we're getting somewhere

                          if I just use datareaders (why in the hell would I want to use
                          datasets?)
                          then it will keep the same SPID even with 20 people hitting the
                          database?

                          I just think that the whole 'automagically close connections' is
                          misguided
                          and when MS Press sits there and says crap like 'uh you guys cant
                          remember to use the .MoveNext method so we've fixed it for you by
                          using the .Read' method

                          I just get a little bit defensive

                          I didn't ask for MIcrosoft to talk down to us-- just because we use
                          the WORLDS MOST POPULAR PROGRAMMING LANGUAGE


                          I mean; are you guys not pissed off by the tone of that authors voice?

                          it's just reflective of what M$ thinks of VB people

                          so... _FUCK_MS_




                          On Feb 17, 12:31 pm, Spam Catcher <spamhoney...@r ogers.comwrote:
                          pfc_s...@hotmai l.com wrote in news:1171659636 .108644.239340
                          @q2g2000cwa.goo glegroups.com:
                          >
                          I just don't understand; why do you even need to close a connection if
                          you can't leave a connection OPEN?
                          >
                          It depends on the reader you use. If you use a data adapter, it'll close
                          for you.
                          >
                          However, datareaders use a firehose mode to read from the database, thus
                          you need to manually close it when you're done with the reader/connection.

                          Comment

                          • PFC Sadr

                            #28
                            Re: Programming - Best Practice

                            I don't WANT to manually close it.
                            I want to leave it OPEN.


                            I don't see the point of opening it and re-opening it and all that
                            crap
                            and you can't utilize temp tables if your provider keeps on randomly
                            closing connections



                            uh; like seriously here

                            let's talk about programming apps for small business.
                            why would I constantly open and close and open and close connections??



                            On Feb 17, 12:31 pm, Spam Catcher <spamhoney...@r ogers.comwrote:
                            pfc_s...@hotmai l.com wrote in news:1171659636 .108644.239340
                            @q2g2000cwa.goo glegroups.com:
                            >
                            I just don't understand; why do you even need to close a connection if
                            you can't leave a connection OPEN?
                            >
                            It depends on the reader you use. If you use a data adapter, it'll close
                            for you.
                            >
                            However, datareaders use a firehose mode to read from the database, thus
                            you need to manually close it when you're done with the reader/connection.

                            Comment

                            • Cor Ligthert [MVP]

                              #29
                              Re: Programming - Best Practice

                              PFC,

                              I have the same, when I have to drive less than 150miles an hour, than I
                              hate that, why would I do that, just for security by instance like opening
                              and closing the connections to prevent that there are pooling of whatever
                              problems.

                              Cor


                              "PFC Sadr" <pfc_sadr@hotma il.comschreef in bericht
                              news:1171924291 .984584.229210@ l53g2000cwa.goo glegroups.com.. .
                              >I don't WANT to manually close it.
                              I want to leave it OPEN.
                              >
                              >
                              I don't see the point of opening it and re-opening it and all that
                              crap
                              and you can't utilize temp tables if your provider keeps on randomly
                              closing connections
                              >
                              >
                              >
                              uh; like seriously here
                              >
                              let's talk about programming apps for small business.
                              why would I constantly open and close and open and close connections??
                              >
                              >
                              >
                              On Feb 17, 12:31 pm, Spam Catcher <spamhoney...@r ogers.comwrote:
                              >pfc_s...@hotma il.com wrote in news:1171659636 .108644.239340
                              >@q2g2000cwa.go oglegroups.com:
                              >>
                              I just don't understand; why do you even need to close a connection if
                              you can't leave a connection OPEN?
                              >>
                              >It depends on the reader you use. If you use a data adapter, it'll close
                              >for you.
                              >>
                              >However, datareaders use a firehose mode to read from the database, thus
                              >you need to manually close it when you're done with the
                              >reader/connection.
                              >
                              >

                              Comment

                              • aaron.kempf@gmail.com

                                #30
                                Re: Programming - Best Practice

                                Cor

                                I don't understand what you're trying to say to me here.

                                Are you saying that you are GLAD that they automagically close
                                connections?

                                IT SHOULD BE OPTIONAL NOT MANDATORY

                                JUST LIKE IT WAS IN ADO; USING CONNECTION SHARING




                                On Feb 19, 8:32 pm, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
                                wrote:
                                PFC,
                                >
                                I have the same, when I have to drive less than 150miles an hour, than I
                                hate that, why would I do that, just for security by instance like opening
                                and closing the connections to prevent that there are pooling of whatever
                                problems.
                                >
                                Cor
                                >
                                "PFC Sadr" <pfc_s...@hotma il.comschreef in berichtnews:117 1924291.984584. 229210@l53g2000 cwa.googlegroup s.com...
                                >
                                >
                                >
                                I don't WANT to manually close it.
                                I want to leave it OPEN.
                                >
                                I don't see the point of opening it and re-opening it and all that
                                crap
                                and you can't utilize temp tables if your provider keeps on randomly
                                closing connections
                                >
                                uh; like seriously here
                                >
                                let's talk about programming apps for small business.
                                why would I constantly open and close and open and close connections??
                                >
                                On Feb 17, 12:31 pm, Spam Catcher <spamhoney...@r ogers.comwrote:
                                pfc_s...@hotmai l.com wrote in news:1171659636 .108644.239340
                                @q2g2000cwa.goo glegroups.com:
                                >
                                I just don't understand; why do you even need to close a connection if
                                you can't leave a connection OPEN?
                                >
                                It depends on the reader you use. If you use a data adapter, it'll close
                                for you.
                                >
                                However, datareaders use a firehose mode to read from the database, thus
                                you need to manually close it when you're done with the
                                reader/connection.- Hide quoted text -
                                >
                                - Show quoted text -

                                Comment

                                Working...