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

B&S cookie value

 
Lek Olof
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

When using the

public long lock(int recNo) throws RecordNotFoundException {

method of B&S assignemnt the assignment says that it should return a cookie value.

Can the recNo be the cookievalue, and if not, why?

regards
Baran
 
Song Jing Lim
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also doing B&S assignmnet, but nothing related to cookies value (beside the magic cookies value on first 4 byte to identify it is a data file).

The locking method I need implement as instruct as below:
// Locks a record so that it can only be updated or deleted by this client.
// If the specified record is already locked, the current thread gives up
// the CPU and consumes no CPU cycles until the record is unlocked.
public void lock(int recNo) throws RecordNotFoundException;

So our requiremenet may be difference even both doing B&S
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,


I also don't have cookies, but as far as I understand the cookies are some form to identify the client. If a client looks a record it becomes a cookie (sugar free). Because if the looking methodology(a record can be releases only by the client which lock the record) you need to use this cookie to identify your client when you need to release the record.
If you use the record index like cookie, this will be the same for all the clients - and this collides with the looking methodology(explained above).

Regards M
 
Lek Olof
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Ok I understand.

So what are you guys mapping the recNo to?

I guess you have some kind of map where you map the recNo to some kind of clientnumber? Or what object to do map the recNo to?
 
Mark Smyth
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lek Olof:
hi,

Ok I understand.

So what are you guys mapping the recNo to?

I guess you have some kind of map where you map the recNo to some kind of clientnumber? Or what object to do map the recNo to?


My version did not have lock cookies for client identification so I don't know much about cookie generation I used thread references to identify the client. With regards to mapping the lock swhat you probably want is a HashMap where the key is an Integer object representing the record number and the value is a Long object representing the generated client cookie value.

Regards,
Mark
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lek,


So what are you guys mapping the recNo to?


If this question is in the cookies context then take care you don't need to care about the records here, what you need is a unique id for the your client(s). This id can be everything.

Regards M.
 
Song Jing Lim
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For locking related to client thread, you can refer to:
http://www.coderanch.com/t/184117/java-developer-SCJD/certification/Single-table-Simple-Locking-WeakHashMap
 
Song Jing Lim
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a seek method that pass in recNo and get the record from database using below fomula.

RandomAccessFile RAF = new RandomAccessFile(dbpath, "rw");

public void seek(recNo){
long filePositionToSeek = headerLen
+ (recordLen() * (recNo - 1));
RAF.seek(filePositionToSeek);
}


So no need map recNo to anything
 
Song Jing Lim
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with server side locking is 'diry read', eg.

User A: Read Rec1
User B: Edit and Update Rec1
User A: Edit and Update Rec1

Where b4 user A update the record, the contain already change by UserB without notice.

Still got problem to solve this... Anyone can help?
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you can build some checks and before you alter a record you check if its content is still the same(or was changed).
But I think that this has nothing to do with the thread topic "Regards MB&S cookie value".

Regards M.
 
Lek Olof
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the help, I think I understand how to solve it now!

regards
Baran
 
Song Jing Lim
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Discussion for 'dirty data', I post a new topic:
http://www.coderanch.com/t/188886/java-developer-SCJD/certification/timestamp-prevent-dirty-data
 
PaulSeldon07
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lek, I think I have the same assignment as you do.
I don't understand well about this cookie.

And in my database, if you print out, location and name,
has to be as a primary key. because the same name in the different location.
Please check.

Bye --

Paul
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic