Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I synchronize over Java aspects defined around a method?
    primarykey
    data
    text
    <p>I have a Hibernate transactional method "doImportImpl" which runs multi-threaded. Certain records however need to be imported in sequence, so the code structure is roughly like this:</p> <pre><code>public RecordResult doImportImpl(String data) { RecordResult result = new RecordResult(); .. do some data processing .. String recordIdentifier = getIdentifier(data); synchronized(recordIdentifier) { process record; } return result; } </code></pre> <p>There is a different transaction method which also has the record identifier and cannot run in parallel to the record import. It therefore is also synchronized on 'record identifier'. </p> <pre><code>public void autoProcess(String data) { String recordIdentifier = getIdentifier(data); synchronized(recordIdentifier) { List&lt;Record&gt; records = dao.queryDatabase(recordIdentifier); for (Record r : records) { autoprocess record; } } } </code></pre> <p>Here's the problem: it seems that sometimes that the method <code>autoProcess()</code> starts to run AFTER the synchronized block from <code>doImportImpl()</code> is finished, but BEFORE the transaction is committed. So, due to tx isolation level, the call to <code>dao.queryDatabase()</code> does not yet see the imported records on the database.</p> <p>How do I make sure the synchronized lock is held all the way through the "return statement" including all aspects around the method call (which handle Hibernate transaction management)? Is it sufficient to put the return statement into the synchronized block?</p> <p>Thanks Simon Niederberger</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload