Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generate sessionID

 
Thai Son Cung
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Reagrds
Tyson
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have u develped an algorithm ? it should be easier
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Thai Son Cung
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic