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

generating unique id with session

B Wiley Snyder
Ranch Hand

Joined: Nov 26, 2003
Posts: 50
Is it a good idea to use the date and time with the first or last few values of the session ID. Or should I just use the complete session ID value for my "unique id"?

Mark Wilcox

Joined: Sep 01, 2004
Posts: 6
Unique Id for what?

In general, I strongly discourage using any system's identifier for any purpose outside of it's stated purpose. The reason is that systems change and they're not expecting you to be using their session id for your system.

And so they change their behavior - which then may break your system.

Thus to be less risky - don't use the Java session identifier for your own unique identity. Instead generate your own unique ids. The simplest approach (assuming you're doing this for a database) - is to create a function that simply returns a counter (start with 1 and each request increments that number).

Make it static and synchronized to minimize chances of two users with the same id.

And then use a database or text file to remember the last count when your system shutsdown.

B Wiley Snyder
Ranch Hand

Joined: Nov 26, 2003
Posts: 50
Originally posted by Mark Wilcox:
Unique Id for what?

For a shopping cart.

I need to send a unique id to the billing agent to identify and seperate different purchasers. I know I need to use the session variable for this and I've heard of different people using different versions, I've never heard of the your idea and I'm not really sure that its a good one for my cart. I have heard though of people using the date and time mixed with the session Id but I don't know if thats a good idea but would like to hear some feedback about it.

Thanks for the response

Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Logically a combination of customer number and order number would be unique, if not order number by itself. You could generate your order numbers as Mark suggests (good practice in my experience) or use an inverted timestamp such as 20040905233000, i.e. YYYYMMDDHHMISS format. If your system could realistically process more than one order in the same second you could add milliseconds or some other discriminator.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

Mark's advice is 100% on-track. Using the session ID sin't a great idea for all the resons he stated.

If your database supports sequences that's a great way to generate unique sequential values.

[Asking smart questions] [About Bear] [Books by Bear]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
yeah you can surely go for sequences, as bear said.

but in Oracle 9i Certifcation Guide SQL/PLSQL published by McGraw Hill, dont remember the author. it is written there that it is not encouraged to use sequences to generate your primary keys when you really have to manipulate the records with that key.

sequences are best suited when you just want a unique auto-generated no. for your key column, and you are not gonna use that much while manipulating records.

cuz it has some parameters like,

lower limit
upper limit

and sometimes later, when your database turns huge then you have to tune the sequences as well.

i think jules solution would be better. Enjoy the same no. of characters ever.
I agree. Here's the link:
subject: generating unique id with session
jQuery in Action, 3rd edition