3

I need to implement some kind of inter-process mutex in Java. I'm considering using the FileLock API as recommended in this thread. I'll basically be using a dummy file and locking it in each process.

Is this the best approach? Or is something like this built in the standard API (I can't find it).

For more details see below:

I have written an application which reads some input files and updates some database tables according to what it finds in them (it's more complex, but business logic is irrelevant here).

I need to ensure mutual exclusion between multiple database updates. I tried to implement this with LOCK TABLE, but this is unsupported by the engine I'm using. So, I want to implement the locking support in the application code.

Community
  • 1
  • 1
Filip
  • 1,451
  • 1
  • 11
  • 19

2 Answers2

5

I went for the FileLock API approach and implemented a simple mutex based on:

All the processes use the same dummy file for acquiring locks.

Filip
  • 1,451
  • 1
  • 11
  • 19
  • how is your program now, is it still running, does it need to update to more global lock? just curious after years – Thang Hoang Feb 01 '23 at 09:03
2

Why bother using files, if you have database on your hands? Try using database locking like this (https://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html).

By the way, what database engine do you use? It might help if you list it.

Denis Kulagin
  • 8,472
  • 17
  • 60
  • 129
  • The engine will be Brighthouse. But it doesn't support any type of locking, as mentioned in the question. – Filip Apr 14 '14 at 09:53
  • 1
    May be unrelated to the question, but what is the reason to use database engine which doesn't allow any tools for concurrent data modification? – Denis Kulagin Apr 14 '14 at 10:08
  • It's very good for high performance queries with lots of data. But it has some limitations compared to standard MySQL engines. – Filip Apr 14 '14 at 12:14
  • Got it. But when using MySQL or alike engines concurrency and it's tuning was the most headache-causing issue (deadlocks and so on). If I knew it is bound to a single client/connection I would most definetely increased it performance quite a lot (as I do for some local installations when no AJAX meaning parallel queries is involved). – Denis Kulagin Apr 14 '14 at 12:19