jsondb-core icon indicating copy to clipboard operation
jsondb-core copied to clipboard

File Writes Are Not Thread Safe

Open RickeyWard opened this issue 4 years ago • 2 comments

Using this in a simple Servlet container application and ran into concurrency issues where multiple threads are trying to write to the file at the same time. Looked at the code the only write protection is a FileLock, and the java documentation states that

File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine. (reference)

It should be as simple as marking the io methods synchronized.

I'm happy to do a RP if its welcomed.

RickeyWard avatar Sep 15 '21 22:09 RickeyWard

@RickeyWard Please create a pull request i will be happy to merge. Please do create unit test cases, We need this to be really stable. Jsondb is used by a lot of people and I would hate to break it because I am unable to test thoroughly.

FarooqKhan avatar Sep 25 '21 23:09 FarooqKhan

I'd argue that a JVM-wide lock is actually desired here. It would prevent multiple instances of an app overwriting data (as opposed to lock only preventing multiple threads within a single app)

sebastian-mrozek avatar Apr 02 '22 22:04 sebastian-mrozek