Reputation: 99
I'm looking for a synchronisation method to ensure my back-end data (xml file) is kept intact during multiple client access.
I'm using jaxb to read and write to and from the file. I want to make sure that when a client reads or writes information to the xml file that no other clients are accessing it at the same time.
I've had a read about a ReadWriteLock
and wondering if this is the best solution? Obviously clients should run some sort of query to see if anything is currently being writtern to the file, and if not then go ahead with the read/write?
I'm kind of new to java to excuse my ignorance!
Thanks
Upvotes: 3
Views: 940
Reputation: 719229
Your problem involves different "clients" - presumably applications running in different JVMs.
If this is the case, the ReadWriteLock
class is not going to help. That class is for doing synchronization of different threads within a single JVM.
What you actually want is the FileLock
class. This locks a file or region of a file against access by another JVM. You need to do something like this:
FileInputStream fis = new FileInputStream("someFile");
FileChannel fc = fis.getChannel();
FileLock fl = fc.lock(); // or tryLock(), or etc
// do stuff
fl.release();
Upvotes: 2
Reputation: 325
You can use the ReadWriteLock. Basicly it works like this:
When reading the file use this block of code:
Lock l = readwriteLock.readLock();
l.lock();
try {
// access the xml
} finally {
l.unlock();
}
when writing use this:
Lock l = readwriteLock.writeLock();
l.lock();
try {
// access the xml
} finally {
l.unlock();
}
Multiple threads can read the file at the same time. Only one thread can write at the same time. Whenever you call l.lock() on a read lock, it checks if the writelock is currently locked. If it is, it waits for the unlock() on the writelock.
Whenever you call l.lock() on a write lock, it checks if there are one (or more) currently locked readlocks. If so, it waits for all locks to be unlocked.
This way you can never write when another thread is reading. And you can never read when another thread is writing (but reading from multiple threads at the same time is working).
Upvotes: 1