C# - Static classes
-
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
-
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
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.
-
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
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_»
-
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
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 useFooMethod
, you will have separate copy of variablea
andb
.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
-
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
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
-
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
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
-
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
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
-
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
I don't know what is methods
DBClient.GetDBHelper()
andintfDBHelper.CreateDBParamsArray(2)
doing and wheredsAccountClass
variable is declared. Other than that, I think it is safe.Navaneeth How to use google | Ask smart questions