storing connection object in session variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vinod allapu
    New Member
    • Apr 2009
    • 28

    storing connection object in session variable

    Hi all,


    I have some webpages in my project, each web page contains same connection object more than one time .

    I want to store a single connection object in a session variable and use it .
    Is it possible and efficient to do like this ? Can anybody suggest a best way to do like this.


    Many thanks to you....
  • maliksleo
    New Member
    • Feb 2009
    • 115

    #2
    Originally posted by vinod allapu
    Hi all,


    I have some webpages in my project, each web page contains same connection object more than one time .

    I want to store a single connection object in a session variable and use it .
    Is it possible and efficient to do like this ? Can anybody suggest a best way to do like this.


    Many thanks to you....
    Use connection string in web.config file and use that connection any where in your application.

    maliksleo

    Comment

    • alonn24
      New Member
      • Apr 2009
      • 2

      #3
      My advice is singleton
      one instance for the entire application to use

      Comment

      • vinod allapu
        New Member
        • Apr 2009
        • 28

        #4
        Hi boss,

        One instance for entire application is good. Where do i declare the connection string. I'm using connection string in web.config. But in each page i need to write

        private string con = ConfigurationMa nager.Connectio nStrings["Connection "].ConnectionStri ng;

        and then when connection required i am writing

        sqlconnection objcon=new sqlconnection(c on);

        there are many instances of connection object in a single page , instead of that I want to have single connection object and use it untill the session expires . How can i do it ?

        one idea of mine is to put in session. Is it right to do like this. If possible provide sample code.


        Thanks a lot.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          One connection object for the whole application is not ideal. Imagine 2000 users trying to access the system at the same time and thus fighting for that connection object? Better read up on connection pooling and use a connection pool. Any one that wants to make a connection is allocated a connection from the pool and the connection returns to the pool when the user is done with it.

          Comment

          • vinod allapu
            New Member
            • Apr 2009
            • 28

            #6
            Hello boss,

            Many thanks for your valuable reply...I have the question of operating many users at atime and fight for connection, but my particular application is specific to a single user single institute.

            If not users may increase by 5 to 8 at a time. The reason why i want to maintain a continuous connection is my pages have many controls and bulk transactions on databases...

            Now keeping this in mind please suggest me.......
            thanking you boss...

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Do not leave your connections opened.
              You really need to research connection pooling as r0 has suggested.

              It is best practice to close connections as soon as your finished with them so that they are returned to the pool and can be used by other components....t his is especially important in web applications that have heavy traffic.

              You can store the connection string in the web.config for your project. There is a <connectionStri ngs> section dedicated to storing connection strings in the web.config. There is even a tool (the aspnet_regiis tool) that you can use to encrypt this section of the web.config so that they aren't stored in plain text.

              Once you have defined your connection string(s) you can use them through out your application where ever you need them. You use the ConfigurationMa nager.Connectio nStrings property to retrieve them. ASP.NET is smart enough to know how to decrypt these string when you use them so it's as simple as defining your connection string in the web.config:
              Code:
              <?xml version="1.0"?>
              <configuration>
              <!-- Your other configuration settings go here -->
              
              <connectionStrings>
              		<add name="myDbConnectionStringKey" connectionString="data source=...." />
              </connectionStrings>
              
              <!-- Your other configuration settings go here -->
              
              </configuration>
              And retrieving it wherever you need it in code:
              Code:
              Private conStr As String = _ 
              ConfigurationManager.ConnectionStrings("myDbConnectionStringKey").ConnectionString

              Comment

              • shiznit770
                New Member
                • Apr 2009
                • 10

                #8
                It sounds like you just don't want to have to write the code out in ever page declaring your object. I would suggest creating a custom BasePage class that way you can simply declare the connection in one place and reference it from each of your pages.

                Here's a place to start

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  I don't know if I would have suggested what shiznit770 has suggested simply because you don't need to create a Base Page class for this purpose. This blurs the line between the application's Business Layer and the Data Access Layer.

                  I would recommend creating a Data Access Layer (a class) whose sole responsibility would be for maintaining/manipulating the database.

                  When you have a tiered application (User interface layer, business layer, data access layer....and any other layers in between) it's easier to test and maintain your code.

                  Comment

                  Working...