java - Concurrently write to XML file -



java - Concurrently write to XML file -

i have multiple processes running on different machines required read/write shared xml file, using dom java , filelocks (while know database more effective approach, not viable due project constraints) .

to create changes xml file, relevant process first creates exclusively locked channel used read file, attempts reuse same channel write new version before closing channel; way lock never down. issue getting java.nio.channels.closedchannelexception when attempting write result, though never explicitly close channel. have suspicions line of code:

doc = dbuilder.parse(channels.newinputstream(channel));

closes channel. if so, how forcefulness channel remain open? code can seen below:

[removed code after update]

update: placing system.out.println(channel.isopen()) before , after suspect line of code confirms channel closed.

update: having asked separate question code below prevents channel closing during parse operation. issue instead of replacing original xml file, transformer appends changed document original. in documentation cannot find related options specifying output of transformer.transform (i have searched transformer/transformer factory/streamresult). missing something? need somehow clear channel before writing? thanks.

update: solved append issue truncating channel size of 0. give thanks @jlrishe advice. have posted working code answer.

try design instead:

create new service (a process) opens socket , listens "update commands". all other processes don't write file straight instead send "update commands" new service

that way, never need worry locking. create whole thing more reliable, may want add together buffers sending processes can go on live while when service down.

with approach, never have deal file locks (which can unreliable depending on os). socket create sure can't start service twice.

java xml dom concurrency filelock

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -