电子商务项目管理 [英] eCommerce items management

查看:30
本文介绍了电子商务项目管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个销售门票的应用程序,我有一张可用门票的表格

I'm creating an app that selling tickets, I have a table for available tickets

id       | serial      | sold
-------------------------------
1        | 000001      | false
2        | 000002      | false
3        | 000003      | true

当用户请求门票时,应用会选择一张门票并将其标记为已售出.

When a user request a ticket the app select a ticket and mark it as sold.

我的问题是:如何管理这个表并防止将同一张票卖给不同的用户,知道同时会有数百个请求?

My question is: how to manage this table and prevent selling the same ticket to a different user knowing that there will be a hundreds of requests at the same time?

我正在考虑锁定,但是当该行被一个事务锁定时,如果另一个事务请求锁定它必须等到第一个事务完成并将该行更新为已售出,然后第二个事务将返回该票证已售出.现在,如果发现行被锁定,如何将第二个事务重定向到不同的行?

I'm thinking about locking but when the row is locked for a transaction, if another transaction request the lock it must wait till the first transaction is finished and update the row as sold, then the second transaction will return that the ticket is been sold. Now how can I redirect the second transaction to a different row if it found the row is locked?

编辑

根据 Paul Zahra 的评论,我创建了一个存储过程

According to Paul Zahra's comment I created a stored procedure

DECLARE @id varchar(MAX)        
SELECT @id = (
    SELECT top(1) [Id]
    FROM   [dbo].[AvailableItems] WITH (ROWLOCK, UPDLOCK)
    WHERE  ([Sold] = 0) )

UPDATE [dbo].[AvailableItems]
SET [Sold] = 1
WHERE [Id] = @id

SELECT [Id], [Serial], [CreateDate], [AvailableTo]
FROM   [dbo].[AvailableItems]
WHERE  ([Id] = @id)

但是我需要确定如果两个交易同时发生,第二个交易会发生什么?它会跳过这一行并移动到下一行吗?或者等待第一个事务完成然后选择同一行?

But I need to be sure what is going to happen to the second transaction if two transactions came at the same time? will it skip this row and move to the next row? or wait the first transaction to be finished then select the same row?

推荐答案

我会使用一个简单的对象锁定该项目并确保它是私有/静态的.这将允许在类的所有实例之间共享其状态,同时防止从类外部修改对象.

I would lock the item using a simple object and ensure that it is private/static. This will allow its state to be shared between all instances of the class whilst preventing the object being modified from outside the class.

有关错误处理和锁定,请参阅

For error handeling and locks please see

在 C# 中,如何安全地退出带有 try catch 块的锁?

支持//Log异常锁会自动释放声明.

in support of the //Log exception lock will be released automaticaly statement.

该代码尚未经过测试,但应该会给您一个良好的开端.

The code has not been tested but it should give you a good start.

public class NextTicketinSequence
{
    private static int _LastTicketNumber;
    private static object _TicketLock;

    public static NextTicketinSequence()
    {
         NextTicketinSequence._LastTicketNumber = 0;
    }

   public static int GetNextTicketNumber()
   {
     int ticketId = 0;

      try
      {
        lock(_TicketLock)
        {
            NextTicketinSequence.LoadLastTicketSold();
            ticketId = NextTicketinSequence._LastTicketNumber + 1;
        }
      }
      Catch(Exception ex)
      {
            //Log exception lock will be released automaticaly
      }

      return ticketId
   }

  public static void LoadLastTicketSold
  {
      //Code to get the last ticket marked as sold
  }
}

这篇关于电子商务项目管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆