This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes XML Read/Write Concurrency Prevention. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "XML Read/Write Concurrency Prevention." Watch "XML Read/Write Concurrency Prevention." New topic
Author

XML Read/Write Concurrency Prevention.

Agniva Sengupta
Greenhorn

Joined: Jul 26, 2011
Posts: 16

I've got a XML which is being accessed by two different java applications simultaneously.

I need to ensure that concurrent access is not granted to the two applications, i.e one application can't read or write before the other has finished working with the XML. Upon completion of read/write operation, one application needs to have a mechanism to signal the other application about it's status.

What is the standard procedure of implementing such a process? I understand that this basically boils down to manually implementing a monitor lock on the file, but what's the best method to go about doing this?
Tom Farrell
Greenhorn

Joined: Jun 18, 2008
Posts: 3
Synchronized methods for reading and writing the data.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35241
    
    7
Java's built-in facilities (like synchronization) won't help if the two apps are running on different JVMs.

Lock files are one way to go about it, or storing the status in a shared database.


Android appsImageJ pluginsJava web charts
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8435

In case you are planning to use the FileChannel/FileLock, this recent discussion will be worth a read http://www.coderanch.com/t/551144/java/java/File-lock-doesn-prevent-threads


[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
Agniva Sengupta
Greenhorn

Joined: Jul 26, 2011
Posts: 16

@Tom Farrell : Synchronization doesn't work !

@Maneesh Godbole : The FileChannel/FileLock mechanism was new, I'll try and see what comes out of this. But the file happens to be in UNIX system. Don't know if the File Lock mechanism can be trusted for safety-critical applications!

@Ulf Dittmer : Storing the status in shared database was what came to my mind initially. Sharing the status and implementing mutex algorithm like Dekker's algorithm/Peterson's algorithm is what I was going to do. But I wanted to make sure that I'm not missing out on some sweeter (read simpler) solution to this problem.
Besides, dedicating a DB table for maintaining status of 1 file doesn't sound nice to me! To my DB admin, it sounds like a trap ! :P
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 8435

Agniva Sengupta wrote:
@Maneesh Godbole : The FileChannel/FileLock mechanism was new, I'll try and see what comes out of this. But the file happens to be in UNIX system. Don't know if the File Lock mechanism can be trusted for safety-critical applications!

Did you even read the discussion in the link? If you had, it would have answered your question already.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32668
    
    4
Too difficult a question for "beginning". Moving, not sure where the best location is, but let's try "synchronization".
Agniva Sengupta
Greenhorn

Joined: Jul 26, 2011
Posts: 16

Maneesh Godbole wrote:
Agniva Sengupta wrote:
@Maneesh Godbole : The FileChannel/FileLock mechanism was new, I'll try and see what comes out of this. But the file happens to be in UNIX system. Don't know if the File Lock mechanism can be trusted for safety-critical applications!

Did you even read the discussion in the link? If you had, it would have answered your question already.


Yes I did, but it seems like the File Lock mechanism doesn't work well in non-Windows OS. Correct me if I'm wrong, but in that same thread, a person named 'john sal' said , "@Rob yes I tried the same on both windows and unix...it is giving exception on windows whereas allowing to write in unix" which gave me the impression that it's not suitable for UNIX.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: XML Read/Write Concurrency Prevention.
 
Similar Threads
SSO + JAAS
HttpSession vs Stateful Session EJB
Web service security and Authentication
What is the best way to generate XML Response
URLyBird RAF multiple instances