Generate unique ID

I need to generate unique ID’s for my application. When I used (UUID.randomUUID()).toString(), I am getting a code (thinking this will be unique), which is very lengthy.

I am not sure how unique it will be, when we generate codes with the help of Java Timestamp or randomstring.

I need to generate unique codes which is only of 8-10 characters in length (alpha-numeric). How to get so?
I am using MySQL database.

Is generating unique code on database side is the best way or can we generate such short (but unique) codes in Java?

Any suggestions with example code will be very helpful.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I use RandomStringUtils.randomAlphanumeric() method from commons-lang to achieve this:

import org.apache.commons.lang.RandomStringUtils;

public static final int ID_LENGTH = 10;

public String generateUniqueId() {
    return RandomStringUtils.randomAlphanumeric(ID_LENGTH);
}

If you using Maven, ensure that you have added commons-lang to project’s dependencies:

<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>

Is generating unique code on database side is the best way or can we generate such short (but unique) codes in java?

It’s up to you and your project. Is id-generation part of business logic? If yes and all logic written on Java, so write it on Java. If all or some part of logic delegated to database, so generate id there (but in this case you will have strong dependency to particular database).

Method 2

Do you have any specific limitation you need to take into account? Such as cross-application uniqueness? Because otherwise, MySQL is quite capable of generating IDs by itself, all you need to do is define an autoincrement column and not specify it at insert time (meaning, inserting a NULL value for it) – that will make MySQL fill it with the next available ID, unique and requiring no work from you.

It won’t be an alphanumerical string (which I’m not sure if you specified as a requirement or restriction), but if all you require is uniqueness, it’s more than enough. 8 – 10 alphanumeric characters aren’t enough to guarantee uniqueness in a randomly-generated string, so you’d have to perform an insert check on the database.

Method 3

Is generating unique code on database side is the best way or can we generate such short (but unique) codes in Java?

Databases are designed to be able to generate unique IDs where needed. I doubt anything you (or I) could code would be a ‘better’ variant of that.

Method 4

I have written a simple service which can generate semi-unique non-sequential 64 bit long numbers. It can be deployed on multiple machines for redundancy and scalability. It uses ZeroMQ for messaging. For more information on how it works look at github page: zUID

Method 5

Take look at: UIDGenerator.java

You can customize it (unique to process only, or world), it is easy to use and fast:

    private static final UIDGenerator SCA_GEN = new UIDGenerator(new ScalableSequence(0, 100));
.......
    SCA_GEN.next();

You can change the implementation to reduce the size of the ID (and add other tradeoffs)

see my benchmarking results at:

http://zoltran.com/roller/zoltran/entry/generating_a_unique_id

or run them yourself.

Method 6

The question if id generation part be done in database or java end:
This question has to be answered by you depending on requirements of your application:

1) One way is to go by System.currenTimeMillis() . But if your applicaation will work in multi clustered env, then you may end up with duplicate values.

http://www2.sys-con.com/itsg/virtualcd/java/archives/0512/Westra/index.html

2) Another way is to use UUID Generator .It will help you in case you have different databases that need to be merged. Using this mehtod you don’t have to worry about duplication of id when merging databases.

https://marketplace.informatica.com/solutions/mapping_uuid_using_java

There may be other factors you may want to consider.
As per your question UUID method will go.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x