【发布时间】:2009-10-13 21:59:43
【问题描述】:
今天我发布了一个小的asp.net beta web 应用程序,它允许内部人员修改一些产品信息。我们开始遇到用户覆盖彼此产品信息的问题......即使每个工作人员正在编辑完全不同的行(产品)。
在谷歌上搜索后,我想我知道发生了什么,它与静态变量的使用有关,下面是一个快速粗略的问题示例:
// EditProductGroup.aspx.cs
public partial class EditProductGroup : System.Web.UI.Page
{
private static int _groupId = 0;
protected void Page_Load(object sender, EventArgs e)
{
_groupId = Convert.ToInt16(Request.QueryString["GroupID"]);
// get existing ProductGroup information from database using
//_groupId to find the row and populate textboxes
}
private void saveProductGroupData()
{
// when user hits 'save changes' button, update row
//where the primary key column 'ProductGroupID' matches _groupId in the table
}
}
因此,根据我的研究,整个应用程序实际上存在一个静态变量,这意味着如果多个用户正在使用该应用程序,他们将有效地为“_groupId”读取相同的“值”,并且在某些情况下实际上将其设置为不同的值会导致页面的另一个用户“实例”将数据保存到错误的行 (ProductGroupId)。
我的意图是静态变量与其他用户隔离,并且不应干扰 - 每个用户都有自己的页面实例,因此他们自己的 '_groupId' 变量实例。
幸运的是,这一切都发生在开发/暂存数据库而不是实时数据库上。我不确定是否只需要删除“静态”关键字来阻止每个人设置/读取变量。
有什么想法吗?谢谢
【问题讨论】:
标签: c# asp.net static variables