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. Database & SysAdmin
  3. Database
  4. Using Pooling or SqlConnection object variable is better ?

Using Pooling or SqlConnection object variable is better ?

Scheduled Pinned Locked Moved Database
databaseperformancequestion
7 Posts 3 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.
  • M Offline
    M Offline
    MrKBA
    wrote on last edited by
    #1

    Hi, I need to know which is better for work and performance please : I have to do many operation on database so I am hesitating between : 1. create object "SqlConnection" and let connection opened during service (webservice) works 2. using the instruction below every time I want to do a modification in dtatabase

    using (SqlConnection sqlconnection = GetSqlConnection(connectionString, false))
    {
    sqlconnection.Open();
    .....

    }

    Where

    public static SqlConnection GetSqlConnection(string connectionString, bool disablePooling = true, bool forceMasterDB = false)
    {
    if (disablePooling || forceMasterDB)
    {
    SqlConnectionStringBuilder sqlConnBuilder = new SqlConnectionStringBuilder(connectionString);

                if (disablePooling)
                    sqlConnBuilder.Pooling = false;
    
                if (forceMasterDB)
                    sqlConnBuilder.InitialCatalog = "master";
    
                connectionString = sqlConnBuilder.ConnectionString;
            }
            return new SqlConnection(connectionString);
        }
    

    Thank you

    T Richard DeemingR 2 Replies Last reply
    0
    • M MrKBA

      Hi, I need to know which is better for work and performance please : I have to do many operation on database so I am hesitating between : 1. create object "SqlConnection" and let connection opened during service (webservice) works 2. using the instruction below every time I want to do a modification in dtatabase

      using (SqlConnection sqlconnection = GetSqlConnection(connectionString, false))
      {
      sqlconnection.Open();
      .....

      }

      Where

      public static SqlConnection GetSqlConnection(string connectionString, bool disablePooling = true, bool forceMasterDB = false)
      {
      if (disablePooling || forceMasterDB)
      {
      SqlConnectionStringBuilder sqlConnBuilder = new SqlConnectionStringBuilder(connectionString);

                  if (disablePooling)
                      sqlConnBuilder.Pooling = false;
      
                  if (forceMasterDB)
                      sqlConnBuilder.InitialCatalog = "master";
      
                  connectionString = sqlConnBuilder.ConnectionString;
              }
              return new SqlConnection(connectionString);
          }
      

      Thank you

      T Offline
      T Offline
      Tim Carmichael
      wrote on last edited by
      #2

      If you have the code, why not write a simple app to process a number of statements and check the execution time. Ensure the statements executed are the same for each.

      M 1 Reply Last reply
      0
      • M MrKBA

        Hi, I need to know which is better for work and performance please : I have to do many operation on database so I am hesitating between : 1. create object "SqlConnection" and let connection opened during service (webservice) works 2. using the instruction below every time I want to do a modification in dtatabase

        using (SqlConnection sqlconnection = GetSqlConnection(connectionString, false))
        {
        sqlconnection.Open();
        .....

        }

        Where

        public static SqlConnection GetSqlConnection(string connectionString, bool disablePooling = true, bool forceMasterDB = false)
        {
        if (disablePooling || forceMasterDB)
        {
        SqlConnectionStringBuilder sqlConnBuilder = new SqlConnectionStringBuilder(connectionString);

                    if (disablePooling)
                        sqlConnBuilder.Pooling = false;
        
                    if (forceMasterDB)
                        sqlConnBuilder.InitialCatalog = "master";
        
                    connectionString = sqlConnBuilder.ConnectionString;
                }
                return new SqlConnection(connectionString);
            }
        

        Thank you

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Create the connection as late as possible, and wrap it in a using block to ensure that it's always disposed of properly. Don't disable connection pooling, since the underlying network connections can be expensive to set up and tear down. The connection pool is there to improve the performance of your code! Trying to share a single connection object across multiple threads will only lead to errors. To avoid the errors, you'd have to synchronise access to the shared connection, which will significantly degrade the performance of your code.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        M 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          Create the connection as late as possible, and wrap it in a using block to ensure that it's always disposed of properly. Don't disable connection pooling, since the underlying network connections can be expensive to set up and tear down. The connection pool is there to improve the performance of your code! Trying to share a single connection object across multiple threads will only lead to errors. To avoid the errors, you'd have to synchronise access to the shared connection, which will significantly degrade the performance of your code.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          M Offline
          M Offline
          MrKBA
          wrote on last edited by
          #4

          thank you for response Yes what I did is that every time I need the connection I use it in using block. Also I use pooling always because I'm using the same connection string. so you see this safe ?

          Richard DeemingR 1 Reply Last reply
          0
          • T Tim Carmichael

            If you have the code, why not write a simple app to process a number of statements and check the execution time. Ensure the statements executed are the same for each.

            M Offline
            M Offline
            MrKBA
            wrote on last edited by
            #5

            thank you for response but as I said I know what you suggest but I ask what is more safe also for me.

            1 Reply Last reply
            0
            • M MrKBA

              thank you for response Yes what I did is that every time I need the connection I use it in using block. Also I use pooling always because I'm using the same connection string. so you see this safe ?

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              Yes, it sounds like you're doing the right thing. :)


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              M 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                Yes, it sounds like you're doing the right thing. :)


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                M Offline
                M Offline
                MrKBA
                wrote on last edited by
                #7

                thank you for help

                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