• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NX FileChannel & Thread Safety

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,
I have just started on the assignment and am new to Java. I have used this forum a lot and it has loads of really useful stuff. Thanks to everyone who posts.
I have decided to use FileChannel for I/O. I have multiple instances of the Data class (that implements DBAccess). Initially I load the file data into a static cache (so shared by all Data instances). When I need to update the file, I create a FileChannel just-in-time for the write to the file.
My logical locking ensures no two threads will try to write to the same portion of the file at the same time (for create, update and delete).
For the write I use:
fc.write(buffer, position);
I believe that the above is thread safe, but am not 100% sure. Can anyone confirm this? Or do I also synchronize around the write operation to ensure thread safety?
Thanks,
Alison Carter
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alison,
Welcome to the 'Ranch. Your design sounds fine: even elegent. But I'd test the living heck out of it anyway.
All best,
M
 
Alison Carter
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the feedback Max.
I just wanted to be sure about the use of FileChannel.
Regards,
Alison
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Allison,
I've got something very similar to your design, but right now I don't allow multiple Data instances for one database file. This is mostly because I'm not sure how writing to the same file using two different FileChannels would work.
I gather from your post that as long as two FileChannel instances don't write to an overlapping section of the file, having multiple FileChannels accessing the file at the same time is OK. Is this correct? If it is, I would definitely move to a multiple-instance design.
I also assume that in addition to having a static data cache you've got some kind of static locked record list or map. Is this correct?
Regards,
jb
 
Jay Bromley
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ooops.
I was almost convinced that allowing multiple instances of the Data class was the right way to go, but then I realized that having static cache and locked record list objects in Data would mean that the Data class could only be used for one database file (table).
On the other hand, only allowing single instances for a given database file and making the cache and lock list instance variables instead of statics means that Data instances could be instantiated for more than one data file (i.e. other data tables).
So the question is (I think): allow multiple instances for greater scalability and possibly greater server responsiveness, or only allow a single instance for each data file but allow Data to be used with muliple data files/tables? If you think growth in the number of users is more important than the first options seems right. If you think that B&S will want to add more tables (a customer table?), then the second option seems better.
I'm leaning back towards single instances and so allowing multiple tables in the future. I'm also just rambling now. Any opinions on this?
Regards,
jb
P.S. I noticed I'm now a "ranch hand" I was curious how this came about.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since multiple tables are not required in the assignment, I personaly don't prefer to touch that part of design
 
Alison Carter
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
thanks for all the opinions. I agree with Jonathan I believe efficiency of this implementation is best rather than extensibility for multiple tables given the spec says:
"The IT director does not anticipate much reuse of the first Java technology system ..."
As long as you document which choice you make and why, IMO either implementation is probably OK.
Alison Carter
 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic