user915303
user915303

Reputation:

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.

Upvotes: 17

Views: 30867

Answers (6)

Majid Azimi
Majid Azimi

Reputation: 5745

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

Upvotes: 1

Shweta Gulati
Shweta Gulati

Reputation: 606

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.

Upvotes: 0

Slava Semushin
Slava Semushin

Reputation: 15214

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).

Upvotes: 21

user2179737
user2179737

Reputation: 551

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.

Upvotes: 0

Naltharial
Naltharial

Reputation: 2152

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.

Upvotes: 7

Andrew Thompson
Andrew Thompson

Reputation: 168845

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.

Upvotes: 5

Related Questions