Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a number of problems with this approach. </p> <ol> <li><p>Unless you've called String.intern then your from string is probably not the same from as the other one you are calling. Relying on the behaviour of the internal java string cache is not very robust.</p></li> <li><p>you aren't properly disposing of your XMLDecoder in a finally block, any exception thrown during that call will leak the file description associated with that FileInputStream.</p></li> <li><p>You don't need to wrap e in another Exception(e), you can just throw e as you have declared the enclosing method also throws Exception</p></li> <li><p>Catching/Throwing exception is a code smell. Yes, it is a super class of IOException, and whatever XML decoding exception might be thrown, but its also a superclass of a bunch of other things you probably didn't want to catch, NullPointerException for instance.</p></li> </ol> <p>To answer your question, how can you serialize access to a shared file to ensure its not being used by more than one thread, is tricky. FileChannel.lock() doesn't work inside the JVM, they just lock the file from modification by other processes in the machine.</p> <p>My approach would be to strip any locking out of this class and wrap it in something that is aware of the threading issues of your code.</p> <p>I'd also not pass a String as the filename, but a File, which gives you the ability to use File.createTempFile(2) to create opaque filenames between the thing writing xml and the thing reading xml.</p> <p>Finally, do you want to synchronise access to a shared file, or fail when you detect multiple access to the same file? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload