using Solution.DataAccess.DataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Solution.DataAccess.Model.Common
{
public class CommonClass
{
public static List<Solution.DataAccess.DataModel.C_Details> list = new List<Solution.DataAccess.DataModel.C_Details>();
public static int isSave = 0;
public static int isInsert = 0;
public static String namestr = null;
}
}
在多个界面中调用,增删改查操作针对list进行,之后用户进行保存操作才保存到数据库中,
但是多人操作时会出现问题,新手,不知道该怎么解决,求帮助~
多人操作时操作的是公共的同一个list,想要实现的是每个用户拥有一个自己的list,并对其操作
14 个解决方案
#1
多人操作?你做的是网站吗
#2
如果你做的是网站,那么不要用static 类型的变量
#3
winform怎么也不可能出现多人操作啊
#4
是网站,用的FineUI和SubSonic做的公司内部用的系统,我对这些也不太熟,刚毕业,
现在就是遇到这个问题,不知道该怎么处理这个并行问题,
我是想是否可以公共声明list,然后每个用户实例化自己的list,这样可行吗?
#5
如果你是网站,你为什么要static数据?为什么不直接数据库访问操作?
List本来就不是线性安全的
List本来就不是线性安全的
#6
既然是新手,你还是直接操作数据库吧,别玩什么高大上的list缓存
list本来也不是用来干缓存的
list本来也不是用来干缓存的
#7
因为涉及到所做业务的问题,进入页面是不需要显示任何数据库中的数据的,增删改确定之后才需要保存到数据库中,并导出excel
如果直接对数据库操作会有很多地方需要考虑,当初选择用list来做了,当时也没考虑到多人使用会存在问题这个情况,第一次工作
经验太少了,不知道现在会有办法吗?如果实在不行的话就难办了,要重新做了
#8
你先把static 拿掉之后,再看还有啥问题
#9
如果你是针对个人的增删改全部完了之后才进行保存,那你可以考虑将List<Solution.DataAccess.DataModel.C_Details>存入Session中,最终保存时从Session中取出值
也就是将你的
改成下面的,当然要注意C_Details需要添加Serializable特性
也就是将你的
public static List<Solution.DataAccess.DataModel.C_Details> list = new List<Solution.DataAccess.DataModel.C_Details>();
改成下面的,当然要注意C_Details需要添加Serializable特性
public List<Solution.DataAccess.DataModel.C_Details> List
{
get
{
return Session["C_Details"] as List<Solution.DataAccess.DataModel.C_Details>;
}
set
{
Session["C_Details"] = value;
}
}
#10
百度关键字:
ThreadStaticAttribute
ThreadStaticAttribute
#11
static 拿掉之后根本调用不到list了
#12
我把其中一个界面的代码放一下吧
using System;
using System.Collections.Generic;
using Solution.Web.Managers.WebManage.Application;
using Solution.Logic.Managers;
using Solution.DataAccess.DataModel;
using System.IO;
/***********************************************************************
* 文件名称:ConfirmationLetter.aspx.cs
* 描 述:确认函
* 王崇飞
* 修 改 人:
* 修改日期:
* 修改原因:
***********************************************************************/
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using System.Data;
using NPOI.HSSF.Util;
using Solution.DataAccess.Model.Common;
using Solution.Web.Managers.PNR;
using Solution.DataAccess.Model.PNR;
using NPOI.SS.Util;
using Solution.DataAccess.Model.Common.CommonClass;
namespace Solution.Web.Managers.WebManage.ConfirmationLetter
{
public partial class ConfirmationLetter : PageBase
{
string loginName = "";
decimal SubTotal = 0;
decimal FullTotal = 0;
#region Page_Load
protected void Page_Load(object sender, EventArgs e)
{
//List<C_Details> CommonList = Solution.DataAccess.Model.Common.CommonClass.list;
//Solution.DataAccess.Model.Common.CommonClass.
//CommonClass.list = new List<Solution.DataAccess.DataModel.C_Details>();
//初始化时间
String dt = DateTime.Now.ToString("yyyy-MM-dd");
//String dt1 = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");
lblDatetime.Text = dt;
//获取用户名
/***当前用户信息**/
var Lmodel = OnlineUsersBll.GetInstence().GetOnlineUsersModel();
if (Lmodel == null) { }
else
{
loginName = Lmodel.Manager_CName;
}
if (!IsPostBack)
{
//CommonClass.list = new List<C_Details>();
LoadData();
}
}
#endregion
#region 加载数据
/// <summary>读取数据</summary>
public override void LoadData()
{
//设置排序
if (sortList == null)
{
Sort(null);
}
//绑定Grid表格
//bll.BindGrid(Grid1, Grid1.PageIndex + 1, Grid1.PageSize, InquiryCondition(), sortList);
//CommonClass.namestr = txtName.Text;
databind1();
//CommonClass.namestr = txtName.Text;
}
private void databind1()
{
txtName.Text = CommonClass.namestr;
Grid1.DataSource = CommonClass.list;
Grid1.DataBind();
}
#endregion
#region 接口函数,用于UI页面初始化,给逻辑层对象、列表等对象赋值
public override void Init()
{
//逻辑对象赋值
//bll = File_UploadBll.GetInstence();
//表格对象赋值
//grid = Grid1;
}
#endregion
#region 添加新记录
/// <summary>
/// 添加新记录
/// </summary>
public override void Add()
{
Window1.IFrameUrl = "ConfirmationEdit.aspx?" + MenuInfoBll.GetInstence().PageUrlEncryptString();
Window1.Hidden = false;
}
#endregion
#region 保存
/// <summary>
/// 数据保存
/// </summary>
/// <returns></returns>
public override string Save()
{
SubTotal = 0;
FullTotal = 0;
string result = string.Empty;
C_Details modelDetail = null;
if (txtName.Text == "")
{
result = "请输入姓名";
}
else
{
if (CommonClass.isSave == 0)
{
try
{
C_ConfirmationLetter model = new C_ConfirmationLetter();
model.Confirm_Id = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");
model.Name = txtName.Text;
model.CreateDate = Convert.ToDateTime(lblDatetime.Text);
model.Operator = loginName;
//获取model.Id
for (int i = 0; i < CommonClass.list.Count; i++)
{
modelDetail = CommonClass.list[i];
modelDetail.Id = 0;
modelDetail.Confirm_Id = model.Confirm_Id;
SubTotal += modelDetail.SubPrice;
FullTotal += modelDetail.FullPrice;
C_DetailsBll.GetInstence().Save(this, modelDetail);
result = "保存成功!";
CommonClass.isSave = 1;
}
model.SubTotal += SubTotal;
model.FullPriceTotal = FullTotal;
if (CommonClass.list.Count > 0)
{
C_ConfirmationLetterBll.GetInstence().Save(this, model);
}
else
{
result = "保存失败!";
}
}
catch (Exception e)
{
result = "保存失败!";
//出现异常,保存出错日志信息
CommonBll.WriteLog(result, e);
}
return result;
}
else
{
result = "数据已保存!";
}
}
return result;
}
#endregion
#region 编辑
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public override void Edit()
{
int id = Grid1.SelectedRowIndex + 1;
Window1.IFrameUrl = "ConfirmationEdit.aspx?Id=" + id + "&" + C_ConfirmationLetterBll.GetInstence().PageUrlEncryptStringNoKey(id + "");
Window1.Hidden = false;
}
#endregion
#region 删除记录
/// <summary>
/// 删除记录
/// </summary>
/// <returns></returns>
public override string Delete()
{
//获取要删除的ID
int id = Grid1.SelectedRowIndex;
//如果没有选择记录,则直接退出
try
{
//删除记录
CommonClass.list.RemoveAt(id);
return "删除成功。";
}
catch (Exception e)
{
string result = "删除失败!";
//出现异常,保存出错日志信息
CommonBll.WriteLog(result, e);
return result;
}
}
#endregion
#13
这是编辑界面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Solution.Web.Managers.WebManage.Application;
using FineUI;
using Solution.Logic.Managers;
using DotNet.Utilities;
using Solution.DataAccess.DataModel;
using SubSonic.Query;
using Solution.DataAccess.DbHelper;
using System.IO;
/***********************************************************************
* 文件名称:ConfirmationEdit.aspx.cs
* 描 述:添加或编辑确认函信息
* 王崇飞
* 修 改 人:
* 修改日期:
* 修改原因:
***********************************************************************/
using System.Text;
using Solution.DataAccess.Model.Common;
namespace Solution.Web.Managers.WebManage.ConfirmationLetter
{
public partial class ConfirmationEdit : PageBase
{
#region Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//获取ID值
hidId.Text = RequestHelper.GetInt0("Id") + "";
//加载数据
LoadData();
}
}
#endregion
#region 接口函数,用于UI页面初始化,给逻辑层对象、列表等对象赋值
public override void Init()
{
}
#endregion
#region 加载数据
/// <summary>读取数据</summary>
public override void LoadData()
{
int id = ConvertHelper.Cint0(hidId.Text);
if (id > 0)
{
//获取指定Id的管理员实体
var model = CommonClass.list[id - 1];
if (model == null)
return;
//给页面控件赋值
FlightNum.Text = model.FlightNum;
Flight.Text = model.Flight;
Mile.Text = model.Mile.ToString();
dpDate.SelectedDate = Convert.ToDateTime(model.FlightDate.ToString());
Cabin.Text = model.Cabin;
TicketNum.Text = model.TicketNum;
TicketPrice.Text = model.TicketPrice.ToString();
Taxes.Text = model.Taxes.ToString();
FullPrice.Text = model.FullPrice.ToString();
}
else
{
}
}
#endregion
#region 添加新记录
/// <summary>
/// 添加新记录
/// </summary>
public override string Save()
{
string result = string.Empty;
int id = ConvertHelper.Cint0(hidId.Text);
try
{
#region 赋值
//获取实体
C_Details model = new C_Details();
if (id > 0)
{
id = id - 1;
CommonClass.list[id].Confirm_Id = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");
CommonClass.list[id].FlightNum = FlightNum.Text;
CommonClass.list[id].Flight = Flight.Text;
CommonClass.list[id].Mile = Convert.ToInt16(Mile.Text);
CommonClass.list[id].FlightDate = Convert.ToDateTime(dpDate.Text);
CommonClass.list[id].Cabin = Cabin.Text;
CommonClass.list[id].TicketNum = TicketNum.Text;
CommonClass.list[id].TicketPrice = Convert.ToDecimal(TicketPrice.Text);
CommonClass.list[id].Taxes = Convert.ToDecimal(Taxes.Text);
CommonClass.list[id].SubPrice = Convert.ToDecimal(TicketPrice.Text) + Convert.ToDecimal(Taxes.Text);
CommonClass.list[id].FullPrice = Convert.ToDecimal(FullPrice.Text);
result = "修改成功!";
}
else
{
model.Confirm_Id = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");
model.FlightNum = FlightNum.Text;
model.Flight = Flight.Text;
model.Mile = Convert.ToInt16(Mile.Text);
model.FlightDate = Convert.ToDateTime(dpDate.SelectedDate);
model.Cabin = Cabin.Text;
model.TicketNum = TicketNum.Text;
model.TicketPrice = Convert.ToDecimal(TicketPrice.Text);
model.Taxes = Convert.ToDecimal(Taxes.Text);
model.SubPrice = Convert.ToDecimal(TicketPrice.Text) + Convert.ToDecimal(Taxes.Text);
model.FullPrice = Convert.ToDecimal(FullPrice.Text);
CommonClass.list.Add(model);
result = "保存成功!";
}
#endregion
}
catch (Exception e)
{
result = "保存失败!";
//出现异常,保存出错日志信息
CommonBll.WriteLog(result, e);
}
return result;
}
#endregion
}
}
#14
额,直接开事务用sql对数据库操作不行吗?
#1
多人操作?你做的是网站吗
#2
如果你做的是网站,那么不要用static 类型的变量
#3
winform怎么也不可能出现多人操作啊
#4
是网站,用的FineUI和SubSonic做的公司内部用的系统,我对这些也不太熟,刚毕业,
现在就是遇到这个问题,不知道该怎么处理这个并行问题,
我是想是否可以公共声明list,然后每个用户实例化自己的list,这样可行吗?
#5
如果你是网站,你为什么要static数据?为什么不直接数据库访问操作?
List本来就不是线性安全的
List本来就不是线性安全的
#6
既然是新手,你还是直接操作数据库吧,别玩什么高大上的list缓存
list本来也不是用来干缓存的
list本来也不是用来干缓存的
#7
因为涉及到所做业务的问题,进入页面是不需要显示任何数据库中的数据的,增删改确定之后才需要保存到数据库中,并导出excel
如果直接对数据库操作会有很多地方需要考虑,当初选择用list来做了,当时也没考虑到多人使用会存在问题这个情况,第一次工作
经验太少了,不知道现在会有办法吗?如果实在不行的话就难办了,要重新做了
#8
你先把static 拿掉之后,再看还有啥问题
#9
如果你是针对个人的增删改全部完了之后才进行保存,那你可以考虑将List<Solution.DataAccess.DataModel.C_Details>存入Session中,最终保存时从Session中取出值
也就是将你的
改成下面的,当然要注意C_Details需要添加Serializable特性
也就是将你的
public static List<Solution.DataAccess.DataModel.C_Details> list = new List<Solution.DataAccess.DataModel.C_Details>();
改成下面的,当然要注意C_Details需要添加Serializable特性
public List<Solution.DataAccess.DataModel.C_Details> List
{
get
{
return Session["C_Details"] as List<Solution.DataAccess.DataModel.C_Details>;
}
set
{
Session["C_Details"] = value;
}
}
#10
百度关键字:
ThreadStaticAttribute
ThreadStaticAttribute
#11
static 拿掉之后根本调用不到list了
#12
我把其中一个界面的代码放一下吧
using System;
using System.Collections.Generic;
using Solution.Web.Managers.WebManage.Application;
using Solution.Logic.Managers;
using Solution.DataAccess.DataModel;
using System.IO;
/***********************************************************************
* 文件名称:ConfirmationLetter.aspx.cs
* 描 述:确认函
* 王崇飞
* 修 改 人:
* 修改日期:
* 修改原因:
***********************************************************************/
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using System.Data;
using NPOI.HSSF.Util;
using Solution.DataAccess.Model.Common;
using Solution.Web.Managers.PNR;
using Solution.DataAccess.Model.PNR;
using NPOI.SS.Util;
using Solution.DataAccess.Model.Common.CommonClass;
namespace Solution.Web.Managers.WebManage.ConfirmationLetter
{
public partial class ConfirmationLetter : PageBase
{
string loginName = "";
decimal SubTotal = 0;
decimal FullTotal = 0;
#region Page_Load
protected void Page_Load(object sender, EventArgs e)
{
//List<C_Details> CommonList = Solution.DataAccess.Model.Common.CommonClass.list;
//Solution.DataAccess.Model.Common.CommonClass.
//CommonClass.list = new List<Solution.DataAccess.DataModel.C_Details>();
//初始化时间
String dt = DateTime.Now.ToString("yyyy-MM-dd");
//String dt1 = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");
lblDatetime.Text = dt;
//获取用户名
/***当前用户信息**/
var Lmodel = OnlineUsersBll.GetInstence().GetOnlineUsersModel();
if (Lmodel == null) { }
else
{
loginName = Lmodel.Manager_CName;
}
if (!IsPostBack)
{
//CommonClass.list = new List<C_Details>();
LoadData();
}
}
#endregion
#region 加载数据
/// <summary>读取数据</summary>
public override void LoadData()
{
//设置排序
if (sortList == null)
{
Sort(null);
}
//绑定Grid表格
//bll.BindGrid(Grid1, Grid1.PageIndex + 1, Grid1.PageSize, InquiryCondition(), sortList);
//CommonClass.namestr = txtName.Text;
databind1();
//CommonClass.namestr = txtName.Text;
}
private void databind1()
{
txtName.Text = CommonClass.namestr;
Grid1.DataSource = CommonClass.list;
Grid1.DataBind();
}
#endregion
#region 接口函数,用于UI页面初始化,给逻辑层对象、列表等对象赋值
public override void Init()
{
//逻辑对象赋值
//bll = File_UploadBll.GetInstence();
//表格对象赋值
//grid = Grid1;
}
#endregion
#region 添加新记录
/// <summary>
/// 添加新记录
/// </summary>
public override void Add()
{
Window1.IFrameUrl = "ConfirmationEdit.aspx?" + MenuInfoBll.GetInstence().PageUrlEncryptString();
Window1.Hidden = false;
}
#endregion
#region 保存
/// <summary>
/// 数据保存
/// </summary>
/// <returns></returns>
public override string Save()
{
SubTotal = 0;
FullTotal = 0;
string result = string.Empty;
C_Details modelDetail = null;
if (txtName.Text == "")
{
result = "请输入姓名";
}
else
{
if (CommonClass.isSave == 0)
{
try
{
C_ConfirmationLetter model = new C_ConfirmationLetter();
model.Confirm_Id = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");
model.Name = txtName.Text;
model.CreateDate = Convert.ToDateTime(lblDatetime.Text);
model.Operator = loginName;
//获取model.Id
for (int i = 0; i < CommonClass.list.Count; i++)
{
modelDetail = CommonClass.list[i];
modelDetail.Id = 0;
modelDetail.Confirm_Id = model.Confirm_Id;
SubTotal += modelDetail.SubPrice;
FullTotal += modelDetail.FullPrice;
C_DetailsBll.GetInstence().Save(this, modelDetail);
result = "保存成功!";
CommonClass.isSave = 1;
}
model.SubTotal += SubTotal;
model.FullPriceTotal = FullTotal;
if (CommonClass.list.Count > 0)
{
C_ConfirmationLetterBll.GetInstence().Save(this, model);
}
else
{
result = "保存失败!";
}
}
catch (Exception e)
{
result = "保存失败!";
//出现异常,保存出错日志信息
CommonBll.WriteLog(result, e);
}
return result;
}
else
{
result = "数据已保存!";
}
}
return result;
}
#endregion
#region 编辑
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public override void Edit()
{
int id = Grid1.SelectedRowIndex + 1;
Window1.IFrameUrl = "ConfirmationEdit.aspx?Id=" + id + "&" + C_ConfirmationLetterBll.GetInstence().PageUrlEncryptStringNoKey(id + "");
Window1.Hidden = false;
}
#endregion
#region 删除记录
/// <summary>
/// 删除记录
/// </summary>
/// <returns></returns>
public override string Delete()
{
//获取要删除的ID
int id = Grid1.SelectedRowIndex;
//如果没有选择记录,则直接退出
try
{
//删除记录
CommonClass.list.RemoveAt(id);
return "删除成功。";
}
catch (Exception e)
{
string result = "删除失败!";
//出现异常,保存出错日志信息
CommonBll.WriteLog(result, e);
return result;
}
}
#endregion
#13
这是编辑界面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Solution.Web.Managers.WebManage.Application;
using FineUI;
using Solution.Logic.Managers;
using DotNet.Utilities;
using Solution.DataAccess.DataModel;
using SubSonic.Query;
using Solution.DataAccess.DbHelper;
using System.IO;
/***********************************************************************
* 文件名称:ConfirmationEdit.aspx.cs
* 描 述:添加或编辑确认函信息
* 王崇飞
* 修 改 人:
* 修改日期:
* 修改原因:
***********************************************************************/
using System.Text;
using Solution.DataAccess.Model.Common;
namespace Solution.Web.Managers.WebManage.ConfirmationLetter
{
public partial class ConfirmationEdit : PageBase
{
#region Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//获取ID值
hidId.Text = RequestHelper.GetInt0("Id") + "";
//加载数据
LoadData();
}
}
#endregion
#region 接口函数,用于UI页面初始化,给逻辑层对象、列表等对象赋值
public override void Init()
{
}
#endregion
#region 加载数据
/// <summary>读取数据</summary>
public override void LoadData()
{
int id = ConvertHelper.Cint0(hidId.Text);
if (id > 0)
{
//获取指定Id的管理员实体
var model = CommonClass.list[id - 1];
if (model == null)
return;
//给页面控件赋值
FlightNum.Text = model.FlightNum;
Flight.Text = model.Flight;
Mile.Text = model.Mile.ToString();
dpDate.SelectedDate = Convert.ToDateTime(model.FlightDate.ToString());
Cabin.Text = model.Cabin;
TicketNum.Text = model.TicketNum;
TicketPrice.Text = model.TicketPrice.ToString();
Taxes.Text = model.Taxes.ToString();
FullPrice.Text = model.FullPrice.ToString();
}
else
{
}
}
#endregion
#region 添加新记录
/// <summary>
/// 添加新记录
/// </summary>
public override string Save()
{
string result = string.Empty;
int id = ConvertHelper.Cint0(hidId.Text);
try
{
#region 赋值
//获取实体
C_Details model = new C_Details();
if (id > 0)
{
id = id - 1;
CommonClass.list[id].Confirm_Id = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");
CommonClass.list[id].FlightNum = FlightNum.Text;
CommonClass.list[id].Flight = Flight.Text;
CommonClass.list[id].Mile = Convert.ToInt16(Mile.Text);
CommonClass.list[id].FlightDate = Convert.ToDateTime(dpDate.Text);
CommonClass.list[id].Cabin = Cabin.Text;
CommonClass.list[id].TicketNum = TicketNum.Text;
CommonClass.list[id].TicketPrice = Convert.ToDecimal(TicketPrice.Text);
CommonClass.list[id].Taxes = Convert.ToDecimal(Taxes.Text);
CommonClass.list[id].SubPrice = Convert.ToDecimal(TicketPrice.Text) + Convert.ToDecimal(Taxes.Text);
CommonClass.list[id].FullPrice = Convert.ToDecimal(FullPrice.Text);
result = "修改成功!";
}
else
{
model.Confirm_Id = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");
model.FlightNum = FlightNum.Text;
model.Flight = Flight.Text;
model.Mile = Convert.ToInt16(Mile.Text);
model.FlightDate = Convert.ToDateTime(dpDate.SelectedDate);
model.Cabin = Cabin.Text;
model.TicketNum = TicketNum.Text;
model.TicketPrice = Convert.ToDecimal(TicketPrice.Text);
model.Taxes = Convert.ToDecimal(Taxes.Text);
model.SubPrice = Convert.ToDecimal(TicketPrice.Text) + Convert.ToDecimal(Taxes.Text);
model.FullPrice = Convert.ToDecimal(FullPrice.Text);
CommonClass.list.Add(model);
result = "保存成功!";
}
#endregion
}
catch (Exception e)
{
result = "保存失败!";
//出现异常,保存出错日志信息
CommonBll.WriteLog(result, e);
}
return result;
}
#endregion
}
}
#14
额,直接开事务用sql对数据库操作不行吗?