File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Generate sessionID Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generate sessionID" Watch "Generate sessionID" New topic

Generate sessionID

Thai Son Cung
Ranch Hand

Joined: Aug 25, 2005
Posts: 46
Hi all

I have to generate sessionID for each client who login to my server and the sessionID must be:

1. int value
2. unique ( never be repeat)
3. preferly based on current data-time

Please help

Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
Your questions isn't very specific

1. Int value:
java.lang.Integer class
2. Unique
Autoincremental number
3. curent time
look at: java.lang.System class

/Svend Rost
brahmam naane

Joined: Aug 28, 2005
Posts: 1
have u develped an algorithm ? it should be easier
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
A pure timestamp approach doesn't quite work because you can get two keys in a single clock tick. Even if you find a class with nanosecond resolution, it's not really updated more often than the system clock, which just isn't fast enough.

A sequential approach is solid so long as you have a place to reliably save the last key used. Databases are the most reliable or at least report their errors well, but a text file might do for you. Databases have mechanisms to vend sequential numbers so you might just use that.

The overhead of updating a file or database for every key can be high, especially if you have multiple instances of your system across town or the world. A "High/Low" technique helps here. Get the "high" part of the key from the common source, sequence the "low" part of the key from 0 to some max in memory. You only get a new "high" part at startup and when the "low" part overflows.

Do any of those choices sound good?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Thai Son Cung
Ranch Hand

Joined: Aug 25, 2005
Posts: 46
Thanks all!

Stan James I think I will implement your idea, high/low would be a good choice.

Acttualy if the type of the sessionID is long then we can use Calendar..getTimeInMillis(), but since I have to implement a shared interface and the sessionID there is int,

I will try it out give you my feed back
It is sorta covered in the JavaRanch Style Guide.
subject: Generate sessionID