简体   繁体   English

生成的GUID是重复的,我的Java代码错误吗?

[英]The resulting GUID is duplicated, is my java code error?

According to the information I found on the Internet, the probability of GUID repetition is very small, so is there a problem in my code? 根据我在Internet上找到的信息,GUID重复的可能性很小,所以我的代码有问题吗?

public class AccountTaskExecutorTask extends TimerTask {
    private static final Logger logger = Logger.getLogger(AccountTaskExecutorTask.class);
    private TellerDbCore.AccountTask.Builder aTask = null;

    public AccountTaskExecutorTask(TellerDbCore.AccountTask.Builder aTask) {
        this.aTask = aTask;
    }

    public static void schedule(TellerDbCore.AccountTask.Builder aTask) {
        Timer timer = new Timer();
        timer.schedule(new AccountTaskExecutorTask(aTask), 100L);
    }

    @Override
    public void run() {
        try {
            DataBaseStore dataBaseStore = null;
            try {
                dataBaseStore = DbHelper.getTransactableDbStore();
                invest(aTask, dataBaseStore);
                dataBaseStore.commitAndClose();
                dataBaseStore = null;
            } catch (Exception e) {
                logger.error("", e);
                if (dataBaseStore != null) {
                    dataBaseStore.rollbackAndClose();
                    dataBaseStore = null;
                }
            }
        } catch (Exception e) {
            logger.error("", e);
        }
    }


    private static void invest(TellerDbCore.AccountTask.Builder theTask, DataBaseStore dataBaseStore) throws Exception {
            switch (theTask.getTaskStatus()) {
                case TS_READY:
                    List<TellerDbCore.AccountSubTask.Builder> subList = AccountTaskHelper.querySubAtByMtId(theTask.getTaskId(), dataBaseStore);
                    if (subList.size() == 0) {
                        NewMethods.newAccountSubTask(theTask.getTaskId(), GUID.generateGUID().toLowerCase(),
                                theTask.getAccountId(), theTask.getProductType(), theTask.getTaskType(), theTask.getAmount(), dataBaseStore);
                    }
                    UpdateMethods.updateAccountTask(theTask.getTaskId(), null, BtsDbBase.TaskStatus.TS_PROCESSING, dataBaseStore);
                    break;
                case TS_PROCESSING:
                    break;
                case TS_SUCCESSED:
                case TS_FAILED:
                    break;
                default:
                    logger.info( theTask.getTaskStatus());
                    break;
            }
    }
}

In this code, the GUID is generated by GUID.generateGUID().toLowerCase() , the calss of package is oscore-2.2.4.jar . 在此代码中,GUID由GUID.generateGUID().toLowerCase() ,程序包的名称为oscore-2.2.4.jar NewMethods.newAccountSubTask is to add data to the database. NewMethods.newAccountSubTask用于将数据添加到数据库。

this is the code of GUID 这是GUID的代码

package com.opensymphony.util;

import com.opensymphony.module.random.Yarrow;
import java.math.BigInteger;

public final class GUID
{
  private static Yarrow rnd = new Yarrow();

  public static String generateFormattedGUID()
  {
    String guid = generateGUID();

    return guid.substring(0, 8) + '-' + guid.substring(8, 12) + '-' + guid.substring(12, 16) + '-' + guid.substring(16, 20) + '-' + guid.substring(20);
  }

  public static String generateGUID()
  {
    return new BigInteger(165, rnd).toString(36).toUpperCase();
  }
}

Let's say that the number of potential values of GUID is n, a very large natural number. 假设GUID的潜在值的数量为n,这是一个非常大的自然数。 Let's also say that the number of already chosen values is k. 我们还假设已经选择的值的数量为k。 Now, the probability (p) of having a duplicate in values depends on n and k. 现在,值重复的概率(p)取决于n和k。 n is a very large natural number, but let's see the possible values of k (I am using the assumption that all the possible values have largely the same probability): n是一个非常大的自然数,但让我们看一下k的可能值(我使用的是所有可能值都具有大致相同概率的假设):

k == 0 k == 0

In this case you are getting the very first GUID and it is impossible to get a duplicate. 在这种情况下,您将获得第一个GUID,并且不可能获得重复的GUID。 p == 0 p == 0

k === n k === n

In this case you already have all the possible values and it is impossible to not get a duplicate. 在这种情况下,您已经具有所有可能的值,并且不可能没有重复的值。 p == 1 p == 1

0 <= k <= n 0 <= k <= n

In this case, the probabiliti of getting a new value is: 在这种情况下,获得新价值的可能性是:

p == (n - k) / n p ==(n-k)/ n

because we need to exclude the already chosen k elements. 因为我们需要排除已经选择的k个元素。 On the other hand, the probability of getting a duplicate is 另一方面,获得重复的概率为

p' == k / n p'== k / n

so with the increase of k, p decreases and p' increases. 因此,随着k的增加,p减小而p'增大。 Note, that the two first cases are particular cases of this general case. 注意,前两种情况是该一般情况的特殊情况。 Now, what could be the solution, so that you will not get a duplicate? 现在,解决方案是什么,这样您将不会得到重复?

Well, you are using a lower case, which is basically reduces the possible values to half. 好吧,您使用的是小写字母,这基本上将可能的值减少到一半。 If you avoid using lower cases, then n practically doubles. 如果避免使用小写字母,则n实际上会翻倍。 Also, you can use two concatenated guid values for your new values, which will never be duplicated for your values used previously, since they differ in String length. 另外,您可以为新值使用两个串联的guid值,因为它们的字符串长度不同,所以它们永远不会与以前使用的值重复。 Or, you could use an id before the gui String. 或者,您可以在gui字符串之前使用id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM