File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Obtaining unique user ids Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Obtaining unique user ids" Watch "Obtaining unique user ids" New topic

Obtaining unique user ids

anuj sahai

Joined: Dec 02, 2000
Posts: 8
In my application i have to give each user who logs into the appliation a unique transaction id. for this the requiremnt is to obtain the max value from a DB2 table, increment it and the insert it. The problem is that incase another user also logs in the sametime he/she has to be provided a unique id. to ensure his i feel that i would need to implement a table level lock. However this would make it highly ineficent i the case of heavy traffic and causing timeout for many users.
Please suggest how should such a case be handled.
I am using jdbc and backend is db2 v6.1...

Joined: Jan 25, 2002
Posts: 2
I would write a method to generate a unique string identifier. First generate a large random number . Make sure it has a large range of output. Then use " ref = new StringBuffer(Long.toString(strNum))" to convert it and pass it to a StringBuffer object. Randomly insert a few chars into the string and use ref.toString() again before sending to your table. Or, if you wanted to make things a bit easier, use System.currentTimeMillis() to obtain a long which won't be duplicated unless System.currentTimeMillis() is called w/in the same millisecond. The first solution may seem like a hack but I have used this several times effectively especially with a lot of simultaneous traffic. It is possible to issue a duplicate w/ the first but if you write the method well it will be next to impossible. Hope this helps
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at .
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please re-register and select a new name which meets the requirements.
On a personal note, its nice to see someone come out of the blocks answering questions rather than asking them, it's appreciated.
I agree. Here's the link:
subject: Obtaining unique user ids
It's not a secret anymore!