How to use Session variables with multi threading where session is stored in state server (Web Garden)

788 views Asked by At

I need to change the state management from inProc to StateServer in old application.

There is method that execute within a new thread.

The code look like as follows.

protected void btnTransaction_Click(object sender, EventArgs e)
{
    timerTrans.Enabled = true;
    timerTrans.Interval = 200;
    Thread thread = new Thread(new ParameterizedThreadStart(DoAllWork));
    thread.Start(dbConnectionString);
}

public void DoAllWork(object connectionString)
{
   DBAccess dba = new DBAccess(connectionString.ToString());
   Session["NextTransactionId"] = dba.getNewTransactionId();

   //Other work

   foreach(transaction t in transactions)
   {
       ShowTrasnactionStatus("Processing transaction "+t.id);
   }

   //Other work

   Session["FinalStatus"] = "All Transactions Completed";
   Session["TransactionStatus"] = "Done";
}

private void ShowTrasnactionStatus(string statusMsg)
{
    Session["TransactionStatus"] = statusMsg;
}

protected void timerTrans_Tick(object sender, EventArgs e)
{
    lblStatus.Text = Session["TransactionStatus"].ToString();
    
    if (Session["TransactionStatus"].ToString() == "Done")
    {
         //Final Stage of Transaction Processing 
         lblStatus.Text = "Getting final status ...";
         billEndTime = DateTime.Now;

         ShowFinalStatus();
         timerBilling.Enabled = false;
    }
}

When the web garden is enabled the usage of session variables to update the status does not work as expected.

  • I have tried using class variable instead of session variables.
  • Used Cache instead of session.

None of them worked for me.

Note: This code is from a existing application that was developed at least 8 years ago.

I only want to run this in web garden without breaking the functionality.

1

There are 1 answers

0
Sanke On BEST ANSWER

I created a database table and store the values there instead of the session vars. There I used a GUID+KEY to identify the correct value in DB.

I created a method to delete the records in db when the process completes, in that way the searching cost in the DB is reduced.

Note: There is a considerable cost of writing to db and retrieving it.

This may be not the perfect solution for this. But this can be considered as a high-cost workaround.