并发用户使用 C# 读取并更新到 mysql
concurrent user read and update to mysql using c#
我的程序有问题,通过解决这个问题,也许可以通过使用单例方法来完成。但是,我从一些文章中读到,他们说,这不是实现它的好方法,因为 asp.net 应用程序并发用户可能有很多。
我的问题是,系统 运行 号码是基于数据库示例中的特定记录:
- document_type="INV" , document_year=2015 , document_month=04, document_running=0102.
- document_type="INV" , document_year=2015 , document_month=05, document_running=0002.
因此,当用户创建新记录时,获取新运行号码的逻辑如下:
- 获取文档类型="inv",当前年当月,
- 如果没有找到。使用 运行 编号 0001.
创建新的文档类型、年份和月份记录
- 如果找到。 运行 + 1.
现在,问题出来了。我的情况是5个用户收到记录信息
当他们按下 "save" 按钮创建新记录时:
- document_type="INV" , document_year=2015 , document_month=05, document_running=0002.
和 5 个用户获得相同的 运行 号码 INV15-05-0003(在 0002 + 1 之后)。
按理说,应该是 INV15-05-0003、INV15-05-0004、INV15-05-0005、INV15-05-0006 和 INV15-05-0007。
如果所有用户都得到 wrong/outdated 信息,我们应该如何避免。
希望你们都能理解我糟糕的英语。
问候,
MH
接近所需效果的最简单方法是对 document_running 字段使用自动递增:https://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html.
警告:请注意,在这种情况下,您的 document_running 在所有文档中始终是唯一的,而不仅仅是具有相同 type/year/month.
的记录
备选方案:
使用 GUID 作为 document_running 字段。同样的警告也适用。
我看到的唯一方法是在您调用的方法上使用锁来进行数据库管理。
public class Dal
{
private static object syncObject = new object();
public static void InsertUpdate(...)
{
lock (syncObject)
{
// 1. Get the document type = "inv" , current year and current month,
// 2. If not found. create a new document type , year and month record with the running number 0001.
// 3. if found. running + 1.
}
}
}
我的程序有问题,通过解决这个问题,也许可以通过使用单例方法来完成。但是,我从一些文章中读到,他们说,这不是实现它的好方法,因为 asp.net 应用程序并发用户可能有很多。
我的问题是,系统 运行 号码是基于数据库示例中的特定记录:
- document_type="INV" , document_year=2015 , document_month=04, document_running=0102.
- document_type="INV" , document_year=2015 , document_month=05, document_running=0002.
因此,当用户创建新记录时,获取新运行号码的逻辑如下:
- 获取文档类型="inv",当前年当月,
- 如果没有找到。使用 运行 编号 0001. 创建新的文档类型、年份和月份记录
- 如果找到。 运行 + 1.
现在,问题出来了。我的情况是5个用户收到记录信息 当他们按下 "save" 按钮创建新记录时: - document_type="INV" , document_year=2015 , document_month=05, document_running=0002.
和 5 个用户获得相同的 运行 号码 INV15-05-0003(在 0002 + 1 之后)。 按理说,应该是 INV15-05-0003、INV15-05-0004、INV15-05-0005、INV15-05-0006 和 INV15-05-0007。
如果所有用户都得到 wrong/outdated 信息,我们应该如何避免。 希望你们都能理解我糟糕的英语。 问候, MH
接近所需效果的最简单方法是对 document_running 字段使用自动递增:https://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html.
警告:请注意,在这种情况下,您的 document_running 在所有文档中始终是唯一的,而不仅仅是具有相同 type/year/month.
的记录备选方案: 使用 GUID 作为 document_running 字段。同样的警告也适用。
我看到的唯一方法是在您调用的方法上使用锁来进行数据库管理。
public class Dal
{
private static object syncObject = new object();
public static void InsertUpdate(...)
{
lock (syncObject)
{
// 1. Get the document type = "inv" , current year and current month,
// 2. If not found. create a new document type , year and month record with the running number 0001.
// 3. if found. running + 1.
}
}
}