File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes B&S: Lock file with Channel 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 "B&S: Lock file with Channel" Watch "B&S: Lock file with Channel" New topic

B&S: Lock file with Channel

Laura Pecoraro
Ranch Hand

Joined: Mar 31, 2009
Posts: 45
Hi all

So, I'm writing my choices about the Lock and I have a doubt...

My solution use the Channel to Lock the database file to restrict at one access for time... I specifys...

When application run as Server & Client (run the server and run some client), the Server lock the database, and, if I start the application in Standalone mode too, I throws an Exception with "File alredy open..." (my choice)

The Server can receive any number of Client's connection... The Standalone can execute only one for time...

Its a BAD???

Hi Laura
Roel De Nijs

Joined: Jul 19, 2004
Posts: 8411

Hi Laura,

My assignment clearly says (and i guess yours too):

You may assume that at any moment, at most one program is accessing the database file; therefore your locking system only needs to be concerned with multiple concurrent clients of your server.

So using a Channel to restrict access to database file is not necessary and you are doing more than you should do. You will nog get any extra points for doing so, only get points deducted if you made a mistake or if you have very complicated code for doing that.

And it's totally ok for your standalone application to be executed only one time, because there is at most 1 program accessing the db-file. So it's your standalone application or your server application or another application that's using that database-file.


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
K. Tsang

Joined: Sep 13, 2007
Posts: 3134

Hi Laura, if I am correct the Channel you are referring to is in the java.nio package which is not allowed for the SCJD. And the instructions clearly stated "assume only one program assess the database file at any moment".

All I have said in my choices.txt in my assumptions is something on the line:
* The stand-alone version and networked version will not run at the same time on the same computer

The only lock issue you do need to worry is the record locking (eg those lock, unlock, isLocked methods). What you are doing with Channel is more of file locking which can be assumed.

K. Tsang JavaRanch SCJP5 SCJD OCPJP7 OCPWCD5 OCPBCD5 OCPWSD5 OCMJEA5 part 1 part 2/3
Roel De Nijs

Joined: Jul 19, 2004
Posts: 8411

K. Tsang,

It's clearly in the assignment, so i guess you don't have to mention anything about it in choices.txt

K. Tsang wrote:The stand-alone version and networked version will not run at the same time on the same computer

And if i am really picky: if they both use a db-file that's on the same network as the both computers, you still would have a problem

I agree. Here's the link:
subject: B&S: Lock file with Channel
jQuery in Action, 3rd edition