如何生成唯一的订单号? [英] How to generate unique order number?

查看:328
本文介绍了如何生成唯一的订单号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找生成一个唯一的订单ID的好方法。你可以看到下面的代码的任何问题。

I'm looking for a good way to generate a unique order ID. Can you see any problems with the code below?

int customerId = 10000000;

long ticks = DateTime.UtcNow.Ticks;

long orderId = customerId + ticks;

int orderNumber = orderId.GetHashCode();



我要检查创建订单之前的数量是在数据库中是独一无二的。

I'm going to check that the number is unique in the database before creating the order.

推荐答案

如果你是存储在数据库中的记录,你真的应该考虑提供有生成唯一代理键的功能在SQLServer的,这将是一个 IDENTITY 场Oracle中这将是一个使用 SEQUENCE 以产生一个新的值的字段。

If you are storing your records in a database, you should really look into the capabilities available there to generate unique surrogate keys. In SQLServer this would be an IDENTITY field and in Oracle it would be a field that uses a SEQUENCE to generate a new value.

如果有一个令人信服的理由,为什么你不能使用你的数据库生成一个唯一的密钥,你应该看看有点像的 的Guid - 其中有一个比日期时间操纵mucher概率更高生成一个唯一的值。的GUID可以轻易地转化为字符串,所以你的标识符是在这种情况下的字符串。

If there's a compelling reason why you can't use your database to generate a unique key, you should look at something like a Guid - which has a mucher higher probability than date-time manipulation to generate a unique value. Guids can be trivially converted into strings, so your identifier would be a string in this case.

你所用哈希做的是不是一个好主意 - 没什么gaurantees该散列将是唯一的 - 在许多情况下它们确实发生碰撞。 GUID的 - 不提供独特的跨机器的,但一台机器上,他们应该永远是唯一一个100%的保证。甚至跨机器,他们的碰撞的机会非常渺茫。此外,使用机器的时间,以此来建立基础值是受竞争情况(像埃里克描述)。

What you are doing with hashes is not a good idea - nothing gaurantees that hashes will be unique - and in many cases they do indeed collide. Guids - don't provide a 100% guarantee of uniqueness across machines but on a single machine they should always be unique. And even across machines, their chances of collision are extremely remote. Also, using machine time as a way to build up the underlying value is subject to race conditions (like those Eric describes).

GUID是128位值,所以你不能代表他们作为一个简单的 INT 。这将需要您使用字符串作为你的ID ,这可能会或可能无法在你的情况,这取决于其他因素(如是否你不控制数据模型)。如果可以使用它们,使用GUID是很容易的:

Guids are 128-bit values, so you can't represent them as a simple int or long. It would require you to use string as your IDs, which may or may not be possible in your case, depending on other considerations (like whether or not you control the data model). If can use them, using a Guid is really easy:

string customerId = Guid.NewGuid().ToString(); // fetch new guid and save as string
string orderNumber = Guid.NewGuid().ToString(); // same story here...

如果你真的必须使用数字标识符,并你愿意放弃轻松扩展在多个服务器上的应用程序,你可以使用自动增加全球数以提供一个独特的密钥。您将不得不种子这个数字与下一个可用值(Max + 1)当应用程序启动数据库。你还必须保护,然后从多个线程同时使用该值。我想包在一类责任:

If you really must use a numeric identifier, and you are willing to abandon easily scaling your application across multiple servers, you could use an auto-incrementing global number to supply a unique key. You would have to seed this number with the next available value (max+1) from your database when the application starts up. You would also have to then protect this value from concurrent use from multiple threads. I would wrap this responsibility in a class:

class static UniqueIDGenerator 
{
    // reads Max+1 from DB on startup
    private static long m_NextID = InitializeFromDatabase(); 

    public static long GetNextID() { return Interlocked.Increment( ref m_NextID ); }
}





编辑: 在这个时代,在应用程序层,而不是在数据库生成唯一ID令人信服的理由非常少见。你真的应该使用数据库提供的功能。

这篇关于如何生成唯一的订单号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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