-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。

因此,当用户新建一条记录时,获取新流水号的逻辑如下:

  1. 获取文档类型 = "inv" ,当年当月,
  2. 如果没有找到。新建单据类型,年月记录,流水号为0001。
  3. 如果找到。跑步+1。

现在,问题出来了。我的情况是 5 个用户在按下“保存”按钮创建新记录时收到了记录信息: - 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。

如果所有用户都收到错误/过时的信息,我们应该如何避免。希望你们都能理解我蹩脚的英语。问候, MH

4

2 回答 2

0

接近预期效果的最简单方法是对 document_running 字段使用 Auto Incrementing:https ://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html 。

警告:请注意,在这种情况下,您的 document_running 在所有文档中始终是唯一的,而不仅仅是类型/年/月相同的记录。

替代解决方案:使用 GUID 作为 document_running 字段。同样的警告也适用。

于 2015-05-26T15:07:47.037 回答
0

我看到的唯一方法是在您调用的方法上使用锁定来进行数据库管理。

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.
        }
    }
}
于 2015-05-26T15:16:40.093 回答