PDA

View Full Version : Thread and Database connection


jmohan
07-25-2008, 12:33 PM
Hi all,

I need to execute two functions(idfn, urlfn) simultaneously. Therefore, I am using the thread concept in the asp.net with c#. Back end is mysql 5.1 database.

When the thread concept is used with database connection, the two threads are not executed properly. I use the following codings,

Thread firstthread;
Thread secondthread;

protected void Page_Load(object sender, EventArgs e)
{
string str = ConfigurationManager.AppSettings["mysql"];
con = new OdbcConnection(str);


ThreadStart t1 = new ThreadStart(idfn);
ThreadStart t2 = new ThreadStart(urlfn);

firstthread = new Thread(t1);
secondthread = new Thread(t2);

firstthread.Start();
secondthread.Start();

}

void idfn()
{
con.Open();
cmd = new OdbcCommand("select userid from user_id", con);
dr = cmd.ExecuteReader();

while (dr.Read())
{
DropDownList1.Items.Add(dr.GetString(0));
}
dr.Close();
con.close();

}


void urlfn()
{
con.Open();
cmd = new OdbcCommand("select userurl from user_url_summary", con);
dr = cmd.ExecuteReader();

while (dr.Read())
{
DropDownList2.Items.Add(dr.GetString(0));
}
dr.Close();
con.close();
}
But the connection with the database is not established properly, Data are not loaded into two drop down list. please guide me.

J. Mohan.

demtron
07-25-2008, 04:12 PM
According to the MSDN Library entry for OdbcCconnection class:

"Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe."

Also, I would consider opening a separate connection for each thread. There may be a resource contention issue here.

jmohan
07-29-2008, 06:39 AM
According to the MSDN Library entry for OdbcCconnection class:

"Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe."

Also, I would consider opening a separate connection for each thread. There may be a resource contention issue here.


Thank you for your reply,

I will do as per your guidance. If any doubts, let me back to this thread.


J. Mohan.:)