Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Strategy of usage SqlConnection in Web-forms application

Strategy of usage SqlConnection in Web-forms application

Scheduled Pinned Locked Moved ASP.NET
databasequestioncsharpasp-netsql-server
8 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bash
    wrote on last edited by
    #1

    Hi, I would like to ask a general question about ASP.NET and SQL Server. I'm creating an Web-forms application used Microsoft SQL Server and included several ASP.NET-pages. I assume that there are two strategies when I should open a database connection. 1. "Open once during session start" strategy. This means that class System.Web.HttpApplication.Global contains SqlConnection object and the method "Session_Start" contains an invoking of SqlConnection.Open(). If I need to get a pointer to the existed SqlConnection, I will get a pointer to the current instance of class System.Web.HttpApplication.Global. So this method doesn't requires to re-open the database connection on the every page. When current session is finished, the database connection will be closed automatically since object System.Web.HttpApplication.Global will be deleted. 2. "Open and close on the every page" strategy. This means that I don't keep the existed SqlConnection opened. If I need a data from SqlServer (say, to fill DataGrid), I will create a new instance of SqlConnection object each times when it will need. What do you think? What method do you use? Yours sincerely, Alex Bash

    I R 2 Replies Last reply
    0
    • B Bash

      Hi, I would like to ask a general question about ASP.NET and SQL Server. I'm creating an Web-forms application used Microsoft SQL Server and included several ASP.NET-pages. I assume that there are two strategies when I should open a database connection. 1. "Open once during session start" strategy. This means that class System.Web.HttpApplication.Global contains SqlConnection object and the method "Session_Start" contains an invoking of SqlConnection.Open(). If I need to get a pointer to the existed SqlConnection, I will get a pointer to the current instance of class System.Web.HttpApplication.Global. So this method doesn't requires to re-open the database connection on the every page. When current session is finished, the database connection will be closed automatically since object System.Web.HttpApplication.Global will be deleted. 2. "Open and close on the every page" strategy. This means that I don't keep the existed SqlConnection opened. If I need a data from SqlServer (say, to fill DataGrid), I will create a new instance of SqlConnection object each times when it will need. What do you think? What method do you use? Yours sincerely, Alex Bash

      I Offline
      I Offline
      Ian Darling
      wrote on last edited by
      #2

      Go for 2. It's a lot easier, and I think ADO.NET connection pools anyway. It's the approach I use, and it seems to work well. -- Ian Darling If I was any more loopy, I'd be infinite.

      A N T 3 Replies Last reply
      0
      • I Ian Darling

        Go for 2. It's a lot easier, and I think ADO.NET connection pools anyway. It's the approach I use, and it seems to work well. -- Ian Darling If I was any more loopy, I'd be infinite.

        A Offline
        A Offline
        Arjan Einbu
        wrote on last edited by
        #3

        Same here. I agree with Ian on this...

        1 Reply Last reply
        0
        • I Ian Darling

          Go for 2. It's a lot easier, and I think ADO.NET connection pools anyway. It's the approach I use, and it seems to work well. -- Ian Darling If I was any more loopy, I'd be infinite.

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          Yes the connection will be pooled if all parameters are the same. Store the connection string globally (web.config) and you will be set.

          B 1 Reply Last reply
          0
          • I Ian Darling

            Go for 2. It's a lot easier, and I think ADO.NET connection pools anyway. It's the approach I use, and it seems to work well. -- Ian Darling If I was any more loopy, I'd be infinite.

            T Offline
            T Offline
            theJazzyBrain
            wrote on last edited by
            #5

            I am doing it the same way and it works well but I dont know if it the best way. To be honest though, I haven't ever tried it the other way. It would be nice to compare the 2 methods by doing a stress test on the web application. theJazzyBrain Wise is he who asks good questions, not he who gives good answers

            P 1 Reply Last reply
            0
            • B Bash

              Hi, I would like to ask a general question about ASP.NET and SQL Server. I'm creating an Web-forms application used Microsoft SQL Server and included several ASP.NET-pages. I assume that there are two strategies when I should open a database connection. 1. "Open once during session start" strategy. This means that class System.Web.HttpApplication.Global contains SqlConnection object and the method "Session_Start" contains an invoking of SqlConnection.Open(). If I need to get a pointer to the existed SqlConnection, I will get a pointer to the current instance of class System.Web.HttpApplication.Global. So this method doesn't requires to re-open the database connection on the every page. When current session is finished, the database connection will be closed automatically since object System.Web.HttpApplication.Global will be deleted. 2. "Open and close on the every page" strategy. This means that I don't keep the existed SqlConnection opened. If I need a data from SqlServer (say, to fill DataGrid), I will create a new instance of SqlConnection object each times when it will need. What do you think? What method do you use? Yours sincerely, Alex Bash

              R Offline
              R Offline
              Rocky Moore
              wrote on last edited by
              #6

              Bash wrote: What do you think? What method do you use? Yep, #2. There is no reason to have a connection open if there is no business for it. A lot like leaving the door on your house open while you are at work because you do not want to wear out the hinge by opening and closing it all day. It may not seem so bad to have a few connections open. But since you are talking about in the Session, the Session can last quite some time even after the person has left your site. If you wait until the session finally exits and you happen to get hit by a few hundred people, you would have hundreds of open connections just sitting there not doing anything. This breaks scalablity of your web application. If you are only interested in a few visits per hour, there would be nothing hurting on your web application. But it is better to just create and close as needed. Rocky Moore <><

              1 Reply Last reply
              0
              • N Not Active

                Yes the connection will be pooled if all parameters are the same. Store the connection string globally (web.config) and you will be set.

                B Offline
                B Offline
                Bash
                wrote on last edited by
                #7

                Thank you very much for your advise. Yours sincerely, Alex Bash

                1 Reply Last reply
                0
                • T theJazzyBrain

                  I am doing it the same way and it works well but I dont know if it the best way. To be honest though, I haven't ever tried it the other way. It would be nice to compare the 2 methods by doing a stress test on the web application. theJazzyBrain Wise is he who asks good questions, not he who gives good answers

                  P Offline
                  P Offline
                  Philip Patrick
                  wrote on last edited by
                  #8

                  Not needed at all. The first way (opening connection each time you need it) is the best one, since ASP.NET do connection pooling. But always CLOSE(!) your connection at the end. The connection pool works this way: It creates a connection for each unique connection string passed, gives it to you if you create one (new SqlConnection or such) and returns the connection to pool ONLY when you close it. If the connection remains open, it is not returned to the pool, thus next time you'll require a database connection, it will be created again ( a new one) Philip Patrick Web-site: www.stpworks.com "Two beer or not two beer?" Shakesbeer

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups