Friday, March 9, 2012

Need opinion about this design "Thread"

Hi all,

I am having problem with SQL connection at Godaddy where my pool connection gets MAX OUT. When it happens, I cannot access the database.
This is the thread about the problem:http://forums.asp.net/thread/1665023.aspx

I just created this with "THREAD". I hope someone who has experiences with thread can give me some advice about my design. This is my first time.

staticobject Locked =newobject();

publicobject ExecuteCommand(string queryStr,string type)
{
//*************************************************************************************//
// ExecuteCommand: Returns an object //
//*************************************************************************************//

Thread t =null;

lock(Locked)
{
SQLString = queryStr;

switch(type)
{
case"ExecuteNonQuery":
t =newThread(ExecuteNonQuery);
break;
case"ExecuteScalar":
t =newThread(ExecuteScalar);
break;
case"GetDataReader":
t =newThread(GetDataReader);
break;
}

t.Start();
t.Join();
}

returnnull;
}

First of, does this work at all? It runs, but is it a good design in term of Thread? Since I use LOCK, do I still need the t.Join() function? The switch with the three cases, is that OKAY? Basically, I'm clueless. If you read my other post, you will get an idea what I'm trying to do. Any feedback would be very very appreciated.

Thank you.

It looks a little odd to me.

You start a thread, and then immediately you do a "join" which effectively says "wait for the thread to finish before continuing". Why not just call a function instead of messing with a thread? The effect is the same and it is simpler.

Make sure you are Closing or Disposing any and all database connections when you are done using them. The Using statement is the most reliable way of doing this.

|||

SGWellen,

Thank you for the input. I use the "join" right after the "start" because I saw it on several tutorials. The "start" function automatically calls the function that is assign in the switch statement.
If I'm not wrong, I think I don't even need to "join" function there, because the whole script is inside the LOCK. Hmmmm I guess I need to dig in it further. Thank you SGWellen.

No comments:

Post a Comment