File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Give me some advice to my LOCK & UNLOCK Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Give me some advice to my LOCK & UNLOCK" Watch "Give me some advice to my LOCK & UNLOCK" New topic
Author

Give me some advice to my LOCK & UNLOCK

Vince Cheung
Greenhorn

Joined: Dec 06, 2004
Posts: 16
hi, everyone. i'm a new one here. Now i'm taking a exam about SCJD, my friend tell me that this place is good. Many kind men will give me help and advice.
my question is:



Please give me some advice, thanks

[Andrew: Removed some code and put remaing code between [code] and [/code] UBB Tags]
[ December 12, 2004: Message edited by: Andrew Monkhouse ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11278
    
  59

Hi Vince,

Welcome to JavaRanch and this forum.

As you will have noted, I have put your source code between [code] and [/code] UBB Tags. This ensures that indenting is preserved, making your code easier to read and comment upon. When you create or edit a post there are buttons just below the text window which will help insert the tags for you so that you don't have to remember them.

I have also removed a considerable amount of your code. Please read the JavaRanch SCJD FAQ, in particular the section titled "What is the policy on posting questions I saw on the exam / details of how to do the assignment?" which explains why posting locking solutions is frowned upon.

Why are you specifying that the lockedRecords collection is final?



This does not really need to be inside your "try" block. The error message used does not match the test - the test is for the record number being "greater than or equal to zero", not larger than one.



You probably don't want to seek to the recNo - you probably need to calculate the position in the file where that record is located .

It appears that if your record is deleted, then you will throw a RecordNotFoundException with no other data than the FinalCommonKey.UNEXPECTED constant.

This constant is also used when trying to handle IOExceptions and InterruptedExceptions - you might want to consider chaining exceptions for these. Alternatively you might want to search in this forum to see other ways of handling the IOException since not all methods in Data class throw RecordNotFoundException.

Since you have a collection of locked records in memory, you don't really need to write the locked status to file. In fact I suspect that there is a logic hole here, if one client tried to lock a record at the same time as another was deleting it:
  • Client A locks record 5
  • Client B attempts to lock record 5 - goes into wait state
  • Client A deletes record 5
  • Client B locks record 5

  • In this case client B managed to change the status from deleted to locked

    That should give some thoughts to get you started.

    Regards, Andrew


    The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
    Vince Cheung
    Greenhorn

    Joined: Dec 06, 2004
    Posts: 16
    Thank you, Andrew.
    You give me a lot of help not only about java but also about javaranch. i will try my best to do everything well. )
    Mihai Radulescu
    Ranch Hand

    Joined: Sep 18, 2003
    Posts: 916

    Hallo Vince !

    A small note, why you don't use a switch case flow statement instead of the
    this ugly :


    SCJP, SCJD, SCWCD, OCPJBCD
    Isaac Shabtay
    Greenhorn

    Joined: Oct 31, 2004
    Posts: 10
    Hmmmm.

    It appears to me (unless I'm missing something) that you are writing an indication about the record being locked - to the database itself. I have never seen such an approach taken - usually, all locks are managed in memory.

    But this is quite an interesting approach, now that I think of it. Anybody else here is doing the same?


    Isaac


    "Large Vehicles Require More Room" - Highway 69 en route to Sudbury, Ontario
    Oricio Ocle
    Ranch Hand

    Joined: Nov 30, 2004
    Posts: 284

    Hello Vince,
    This approach is your own? or yours specs tell you to write a locking flag to the database?
    My specs literally:
    ...the new system must reimplement the database code from scratch without altering the data file format.
    ....
    Repeat to end of file:
    1 byte "deleted" flag. 0 implies valid record, 1 implies deleted record


    Regards


    SCJP, OCMJD, OCMJEA
    Vince Cheung
    Greenhorn

    Joined: Dec 06, 2004
    Posts: 16
    hi, Ignacio
    i think mine is the same to you.

    ...the new system must reimplement the database code from scratch without altering the data file format.

    i think i don't alter the data file format, only add a flag. but now i don't need use this flag, Andrew told me that when useing Map, i can omit this.

    Perhaps i understood wrongly before.
    David Li
    Ranch Hand

    Joined: Aug 12, 2002
    Posts: 41
    Originally posted by Vince Cheung:
    [QB]...



    ...QB]



    Hi Vince,
    The above code is absolutely wrong.
    Please try the following code:



    Please read the document of java.util.Random class:

    An instance of this class is used to generate a stream of pseudorandom numbers.


    David,<br />SCJP - 94% (2002-7-6)
    Joe Borderi
    Ranch Hand

    Joined: Oct 23, 2004
    Posts: 151
    "It appears to me (unless I'm missing something) that you are writing an indication about the record being locked - to the database itself. I have never seen such an approach taken - usually, all locks are managed in memory."

    I was thinking about doing it this way. There are some advantages. For example, scalability in allowing multiple machines access to the db file. Better transaction control in that multiple records can now be locked as part of a single transaction. By using a user ID in the field to lock the record, you know who has the lock on the record. In a power failure or the like the db engine on startup has an indication that the db may be in an inconsistent state.

    I know these suggestions may be beyond the scope of the assignment. My point is, however, that my initial instinct (before I started reading this forum!) on how to implememnt locking was to lock the record in the file. Also, I don't know whether using values other than -1, 0, and 1 is considered altering the db format.

    Nonetheless, I am glad you raised the question because I wanted to discuss the pros and cons. I am still undecided as to whether I'll physically mark the record in the db file.

    My leanings are that for the assignment I'll take the conventional route by mapping the recnos to weak references. If I were to program this in real life (in absence of a DBMS), I'd mark the record in the db file.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Give me some advice to my LOCK & UNLOCK
     
    Similar Threads
    my question about LockManager.java
    SecurityException
    Lock on reading records
    nx:All of URLy Bird1.1.3 about record lock
    All of URLy Bird 1.1.3 about Junit test