I have several processes running on different machines that are necessary for reading / writing to a common XML file, for this I use DOM with Javaand FileLocks(Although I know that a database will be a more efficient approach, this is impractical due to project limitations).
To make changes to the XML file, the corresponding process first creates an exclusively locked channel that is used to read the file, then it tries to reuse the same channel to write a new version before closing the channel; thus the castle never falls. However, the problem is what I get java.nio.channels.ClosedChannelExceptionwhen I try to record the result, although I never explicitly close the channel. I have a suspicion that the line of code is:
doc = dBuilder.parse(Channels.newInputStream(channel));
closes the channel. If so, how can I make the channel stay open? My code can be seen below:
[remote code after update]
UPDATE: Placing System.out.println(channel.isOpen())before and after the suspicious line of code confirms that the channel is closed here.
UPDATE: By asking a separate question , the code below prevents the channel from closing during the parsing operation. The problem is that instead of replacing the original XML file, the transformer adds the modified document to the original. In the documentation, I cannot find any related options to indicate the output Transformer.transform(I was looking for Transformer/ Transformer factory/ StreamResult). Am I missing something? Do I need to somehow clear the channel before writing? Thank.
: , , 0. @JLRishe . .