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. General Programming
  3. C#
  4. C# - Static classes

C# - Static classes

Scheduled Pinned Locked Moved C#
announcementcsharpdatabase
8 Posts 4 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.
  • D Offline
    D Offline
    DineshSharmain
    wrote on last edited by
    #1

    Hi All, I am really confused with.....static classes...its harmful or beneficial in terms of scalability. no. of users are accessing one static method of static class.....that performs any operation like data insertion, update, deletion and fetching from database..... my confusion is that...while all users will be calling the same method using class directly then it will maintain the version or all users will be in Q. or something else.... I am eager to know ..... Thanks

    Dinesh Sharma

    G _ N D 4 Replies Last reply
    0
    • D DineshSharmain

      Hi All, I am really confused with.....static classes...its harmful or beneficial in terms of scalability. no. of users are accessing one static method of static class.....that performs any operation like data insertion, update, deletion and fetching from database..... my confusion is that...while all users will be calling the same method using class directly then it will maintain the version or all users will be in Q. or something else.... I am eager to know ..... Thanks

      Dinesh Sharma

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      I don't really understand your question, but I'll try to shed some light. If you are building a multi-threaded application (like an ASP.NET web application), then each thread needs to have it's own connection object when working with the database. If you are trying to use the same connections for all threads, only one thread at a time can access the database, and that would indeed be harmful for the scalability.

      Despite everything, the person most likely to be fooling you next is yourself.

      1 Reply Last reply
      0
      • D DineshSharmain

        Hi All, I am really confused with.....static classes...its harmful or beneficial in terms of scalability. no. of users are accessing one static method of static class.....that performs any operation like data insertion, update, deletion and fetching from database..... my confusion is that...while all users will be calling the same method using class directly then it will maintain the version or all users will be in Q. or something else.... I am eager to know ..... Thanks

        Dinesh Sharma

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        static classes can only contain static members. These classes cannot be instantiated using new and are automatically loaded by the runtime. If the static methods refers to static variables they are not thread safe.

        «_Superman_»

        1 Reply Last reply
        0
        • D DineshSharmain

          Hi All, I am really confused with.....static classes...its harmful or beneficial in terms of scalability. no. of users are accessing one static method of static class.....that performs any operation like data insertion, update, deletion and fetching from database..... my confusion is that...while all users will be calling the same method using class directly then it will maintain the version or all users will be in Q. or something else.... I am eager to know ..... Thanks

          Dinesh Sharma

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          DineshSharmain wrote:

          no. of users are accessing one static method of static class.....that performs any operation like data insertion, update, deletion and fetching from database.....

          There is no harm in using it when the static method is not using any shared variables. See the following code

          public static class Foo
          {
          public static void FooMethod(string a,string b){
          ....
          }
          }

          In the above example, FooMethod is safe as it is not using any shared objects. Whenever you use FooMethod, you will have separate copy of variable a and b.

          public static class Foo
          {
          private static string FooVariable = "";
          public static void FooMethod(){
          // use FooVariable here
          }
          }

          Above given code is not safe as static variable(FooVariable) is shared across multiple threads. Read about thread synchronization techniques.

          Navaneeth How to use google | Ask smart questions

          1 Reply Last reply
          0
          • D DineshSharmain

            Hi All, I am really confused with.....static classes...its harmful or beneficial in terms of scalability. no. of users are accessing one static method of static class.....that performs any operation like data insertion, update, deletion and fetching from database..... my confusion is that...while all users will be calling the same method using class directly then it will maintain the version or all users will be in Q. or something else.... I am eager to know ..... Thanks

            Dinesh Sharma

            D Offline
            D Offline
            DineshSharmain
            wrote on last edited by
            #5

            Thanks for your reply..... my problem is not related to database....its related to performance & scalability of static classes. code is as follows -

            static public class TempExample
            {
            static public bool GetCombo(object a, out DataSet dsAccountClass, string strClassType)
            {

                    IDBHelper intfDBHelper = DBClient.GetDBHelper();
                    dsAccountClass = new DataSet();
                    IDataParameter\[\] arParms = intfDBHelper.CreateDBParamsArray(2);
                    arParms\[0\] = intfDBHelper.CreateNewDBParam("ID", SqlDbType.NVarChar, 6);
                    arParms\[0\].Value = a.strA;
                    arParms\[1\] = intfDBHelper.CreateNewDBParam("ClasificationType", SqlDbType.NVarChar, 1);
                    arParms\[1\].Value = strClassType;
                    try
                    {
                        using (IDbConnection connection = DBClient.GetConnection(a.strSystem))
                        {
                            intfDBHelper.FillDataset(connection, CommandType.StoredProcedure, "p\_GetSpConditionCalssCombo", dsAccountClass, new string\[\] { "dt\_ProductClassID" }, arParms);
                        }
            
                        if (dsAccountClass.Tables\[0\].Rows.Count == 0)
                            return false;
                        return true;
                    }
                    catch (Exception ex)
                    {
                        throw DBClientUtils.GetDBErrorMessages(ex, a.strUser);
                    }
                    finally
                    {
                        dsAccountClass.Dispose();
                    }
            
                }
            

            }

            Multiple user will be accessing this class without creating an object so what will happen if 100 users are trying to access the same method of class at the same time.

            Dinesh Sharma

            N 1 Reply Last reply
            0
            • D DineshSharmain

              Thanks for your reply..... my problem is not related to database....its related to performance & scalability of static classes. code is as follows -

              static public class TempExample
              {
              static public bool GetCombo(object a, out DataSet dsAccountClass, string strClassType)
              {

                      IDBHelper intfDBHelper = DBClient.GetDBHelper();
                      dsAccountClass = new DataSet();
                      IDataParameter\[\] arParms = intfDBHelper.CreateDBParamsArray(2);
                      arParms\[0\] = intfDBHelper.CreateNewDBParam("ID", SqlDbType.NVarChar, 6);
                      arParms\[0\].Value = a.strA;
                      arParms\[1\] = intfDBHelper.CreateNewDBParam("ClasificationType", SqlDbType.NVarChar, 1);
                      arParms\[1\].Value = strClassType;
                      try
                      {
                          using (IDbConnection connection = DBClient.GetConnection(a.strSystem))
                          {
                              intfDBHelper.FillDataset(connection, CommandType.StoredProcedure, "p\_GetSpConditionCalssCombo", dsAccountClass, new string\[\] { "dt\_ProductClassID" }, arParms);
                          }
              
                          if (dsAccountClass.Tables\[0\].Rows.Count == 0)
                              return false;
                          return true;
                      }
                      catch (Exception ex)
                      {
                          throw DBClientUtils.GetDBErrorMessages(ex, a.strUser);
                      }
                      finally
                      {
                          dsAccountClass.Dispose();
                      }
              
                  }
              

              }

              Multiple user will be accessing this class without creating an object so what will happen if 100 users are trying to access the same method of class at the same time.

              Dinesh Sharma

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              DineshSharmain wrote:

              its related to performance & scalability of static classes.

              Static classes will be invoked when any of the members are accessed for the first time and it will be available in memory until the application domain unloads.

              DineshSharmain wrote:

              if 100 users are trying to access the same method of class at the same time

              Nothing will happen. I reiterate, if your method is using another static variables, then you need synchronization mechanisms to ensure one thread will modify it at a time. If it is not using any static variables, your method is safe. :)

              Navaneeth How to use google | Ask smart questions

              D 1 Reply Last reply
              0
              • N N a v a n e e t h

                DineshSharmain wrote:

                its related to performance & scalability of static classes.

                Static classes will be invoked when any of the members are accessed for the first time and it will be available in memory until the application domain unloads.

                DineshSharmain wrote:

                if 100 users are trying to access the same method of class at the same time

                Nothing will happen. I reiterate, if your method is using another static variables, then you need synchronization mechanisms to ensure one thread will modify it at a time. If it is not using any static variables, your method is safe. :)

                Navaneeth How to use google | Ask smart questions

                D Offline
                D Offline
                DineshSharmain
                wrote on last edited by
                #7

                Thanks Navneet wat about the code....I have given....is this thread safe may i get any deadlock situation in my application due to this ...as m getting if load increase on application so m digging all this.

                Dinesh Sharma

                modified on Wednesday, January 7, 2009 2:52 AM

                N 1 Reply Last reply
                0
                • D DineshSharmain

                  Thanks Navneet wat about the code....I have given....is this thread safe may i get any deadlock situation in my application due to this ...as m getting if load increase on application so m digging all this.

                  Dinesh Sharma

                  modified on Wednesday, January 7, 2009 2:52 AM

                  N Offline
                  N Offline
                  N a v a n e e t h
                  wrote on last edited by
                  #8

                  I don't know what is methods DBClient.GetDBHelper() and intfDBHelper.CreateDBParamsArray(2) doing and where dsAccountClass variable is declared. Other than that, I think it is safe.

                  Navaneeth How to use google | Ask smart questions

                  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