File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generating unique id with session

 
B Wiley Snyder
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"?

-thanks
 
Mark Wilcox
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Mark
 
B Wiley Snyder
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Wilcox:
Unique Id for what?
Mark


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
Posts: 823
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Jules
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic