Forums Register Login

B&S Is it OK To Generate The long Value For The Lock in "lockRecord()" Randomly?

+Pie Number of slices to send: Send
On page 151 of Andrews book, he hints at the possibility of generating the locks randomly, but I can't find where it definitely says this is acceptable or not. How are the rest of you determining what to return when the lockRecord() method is used? Thanks so much for any hints...
+Pie Number of slices to send: Send
Looks like I just found it. This topic seems to mean that this is pretty much the way to do it:

topic
+Pie Number of slices to send: Send
I used this approach. I would bet money against the possibility of a call to Random.NextLog returning duplicate locks..its possible but extremely improbable.
+Pie Number of slices to send: Send
I intend on using it, but just in case I will kind of keep track of used longs so I guarantee there will be no duplicates.
+Pie Number of slices to send: Send
Thanks, Eiji. Excellent point.
+Pie Number of slices to send: Send
Why would you use Random, then keep track of what has been used... this really sounds like a lot of work. Couldn't you have a static int in your LockManager class which incremented in the LockRecord() method, and handed back in a thread-safe way? (I didn't use locking cookies in my assignment, so I could be way off base here.)

Lara
+Pie Number of slices to send: Send
Yes, you could do that also.

The random solution, on the other hand, is more complete, since it is safer. It is a real life solution, so it is not mandatory (the increment is enough), but since it is not that difficult to do, I see no problem.

Just imagine if your web server used an incremental session ID instead of a random one. If you just saw your session ID is 1234, you could probably use 1233 and get a valid session, luckly with credit card information or other confidential information already inputed.

Also if you want to increase performance, the random solution allows you to generate id's without synchronization. Off course, having the possiblity of collision (larger or smalled depending on keeping tracks or not).
+Pie Number of slices to send: Send
 

Originally posted by Lara McCarver:
Why would you use Random, then keep track of what has been used... this really sounds like a lot of work. Couldn't you have a static int in your LockManager class which incremented in the LockRecord() method, and handed back in a thread-safe way? (I didn't use locking cookies in my assignment, so I could be way off base here.)

Lara



Hi Lara
Here's a solution of generating it randomly using the full long range without lot of work

Long value;
Map lockedRecs;
do {
long lRandom = (long)((Math.random() * 2 -1) * Long.MAX_VALUE);
value = new Long(lRandom);
}
while(lockedRecs.containsValue(value));

Yupp
Danger, 10,000 volts, very electic .... tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 833 times.
Similar Threads
Cookie value used in DBAccess Interface
Part II: Integratting the Application Client with the legacy CGI interface
Interview Experience - Logical Test
ORB Outgoint socket port
Just Passed SCJP
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:24:46.