I'm writing a Java app to update a relational database (currently H2). I have a process which does the following:
This process might be run concurrently in different threads. I'd like to avoid the problem where two threads might both check the import log, find nothing there, and then both attempt to insert the data items.
What might be a good approach? I've been considering:
None of the above seem very appealing, for various reasons -- is there another approach that might work better?
The SERIALIZABLE transaction isolation certainly is the most certain way to achieve your goal but it could mean that performance will suffer.
There is one option you have not considered and that is to build your own semaphore.
You could create a static ConcurrentHashMap of items currently being processed and (at the start of each insert process - put a record and when done delete it.
Then each Thread process could consult this semaphore before starting inserts.