Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DvdDatabase synchronization issue

 
Mike Bailey
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the book SCJD Exam with J2SE 5 by Andrew Monkouse the DvdDatabse class (p. 135) has a static member database which is initialized in the constructor:

private static DvdFileAccess database = null;

public DvdDatabase(String dbPath)
throws FileNotFoundException, IOException {
database = new DvdFileAccess(dbPath);
}

other methods in the class use the database member as well:

public Collection<DVD> findDVD(String query)
throws IOException, PatternSyntaxException {
return database.find(query);
}

Looking at both the RMI and socket implementations different threads will create a DvdDatabase and use it. Isn't it possible that as one thread is executing:
database = new DvdFileAccess(dbPath);
another thread may slice in and call:
database.find(query);
which will be excuting on a partially initialized object?
 
Mike Bailey
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The if condition in second code block above is useless. And all the synchronized(database) code in DvdFileAccess is useless too, as by design, each client runs in its own thread and gets a different instance of DvdFileAccess.


I don't think each client gets a different instance of DvdFileAccess, the only access clients have to DvdFileAccess is through DvdDatabase, and since DvdFileAccess is static, all clients will share the same instance of DvdFileAccess.


And, this will effectively limit the number of concurrent users to the maximum number of file handles allowed to be opened by the OS.


That sounds right, but how does address the issue of the database static member potentially being accessed in an unsafe manner?
 
Mike Bailey
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So, the static database variable is initialized each time a client connects and each client will get a different instance of DvdFileAccess.


I'm not seeing how each client gets a different instance of DvdFileAccess when it is declared static in DvdDatabase.



The above code prints out "Using same database". I still think the scenario I pointed out in my original post is possible (even likely under heavy code).
 
Frederic Thierry
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,

I have had long think about your current topic. AJay is right about the following:

each new client gets a "new" instance of DvdFileAccess

that is the static "database" variable points to a new Instance of DvdFileAccess when a DvdDatabase instance is created.

Now Lets look at the DvdFileAcces class on page 138 and 139. You will notice that there is a difference between the code in the book and the code you download from the APress website. In the book the "RandomAccess database" variable is static whereas in the source code it is an instance variable. Also in the source code Andrew uses the database variable for the conditional block of code inside the DvdFileAccess Constructor whereas in his book he uses the dbPath varaible("static String dbPath").

So as you can see those are two errors either on the source code side or in the bok.

OK lets assume the "RandomAccess database" varaible is static and the varaible used inside the DvdFileAccess constructor used is the database variable, well the setting of the database will only occur once. If dbPath is used then the database will only be set once too.

Andrew if you pass by this forum can you please clarify the inconsistency between your source code and the book code on page 138-139.

Thats as much I can bring to the table.

Oh by the way, I think the constructor in the DvdDatabase File will have no effect since the import variables in the DvdFileAccess class are mostly static plus the database setting will only take place once.

Hope I was clear enough.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic