tags:

views:

33

answers:

2

In the application Im writting the server will have information abuot the users, using a XML databse. The admin user will be able to write/read information on those files too. How can I deal with concurrent access to those files? The only way users/admin can read/write to those files is by requesting to the server(Sockets, TCP connection), so the server will have to handle this. What can I do? I could synchronize server methods, but I dont want to avoid USER A to access his files while the admin is writing on USER B files.

+1  A: 

Use a database instead of files is my first suggestion, they handle locks already.

You should post an example of file structure. It could be done if User A has his data in fileA.xml and user b has his in fileB.xml by locking the given file and synchronizing based on that.

Jes
USER A has his own file usera.xmland USER B has his oen file too userb.xml...How can I do the lock by file?
fredcrs
You hold a map of locks that point to a file. Everytime a user logs on and requests a file, he gets the lock to that file (this is all synchronized). Another user can now log on and request the lock for another file, and it shouldn't interfere with the first user.
Jes
Can I lock to a FileChannel instread of implement a map of locks?
fredcrs
You can lock to any object, but I am not sure how your server handles connections/reading/writing files, therefore it's hard to say what to lock where.The easiest way is to get a database that handles it. There are even xml databases out there, I just can't remember any of them. I think IBM's DB2 has a XML datatype, and it's free for non-commercial use (as far as I remember from a database course in 2008).
Jes
+1  A: 

As Jes says use a database.
MySQL Supports XML: http://dev.mysql.com/tech-resources/articles/xml-in-mysql5.1-6.0.html
Most databases support XML, or you could simply use a VARCHAR that is long enough and get and put the data in there. If that is your plan then maybe a NoSQL Solution would work also, it is just a persistent HashMap that supports record locking as well as other features.

It sounds like there is no conflict between users, what you could also do is have an area for the admins to modify the files, which you would copy daily to where the data is read from for the users.

Romain Hippeau