Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lock across several jvm?

this is a bit related to this question.

I'm using make to extract some information concerning some C programs. I'm wrapping the compilation using a bash script that runs my java program and then gcc. Basically, i'm doing:

make CC=~/my_script.sh

I would like to use several jobs (-j option with make). It's running several processes according to the dependency rules.

If i understood well, I would have as many instances of the jvm as jobs, right ?

The thing is that i'm using sqlite-jdb to collect some info. So the problem is how to avoid several processes trying to modify the db at the same time ? It seems that the sqlite lock is jvm-dependant (i mean one lock can be "see" only inside the locking jvm), and that this is the same for RandomAccessFile.lock().

Do you have any idea how to do that ? (creating a tmp file and then looking if it exists or not seems to be one possibility but may be expensive. A locking table in the dB ? )

thanks

like image 508
LB40 Avatar asked Sep 21 '09 13:09

LB40


3 Answers

java.nio.channels.FileLock allows OS-level cross-process file locking.

However, using make to start a bash scripts that runs several JVMs in parallel before calling gcc sounds altogether too Rube-Goldbergian and brittle to me.

like image 160
Michael Borgwardt Avatar answered Nov 03 '22 13:11

Michael Borgwardt


there are several solutions for this. if your lock should be within the same machine, you can use a server socket to implement it (The process that manages to bind to the port first owns the lock, other processes waits for the port to become available).

if you need a lock that span across multiple machines you can use a memcached lock. this will require a memcached server running. I can paste some code if you are interested in this solution.

you can get Java library to connect to memcached here.

like image 4
Omry Yadan Avatar answered Nov 03 '22 13:11

Omry Yadan


You may try Terracotta for sharing objects between various JVM instances. It may appear as a too heavy solution for your needs, but at least worth considering.

like image 1
Grzegorz Oledzki Avatar answered Nov 03 '22 13:11

Grzegorz Oledzki