Note that there are some explanatory texts on larger screens.

plurals
  1. POWould transactions/spring Transaction propagation solve this concurrency issue?
    primarykey
    data
    text
    <p>I have a couple of questions about Transactions in Spring if you may. Let's suppose i have this DAO class :</p> <pre><code>public class MyDAO { /** * verifies if a certain record in DB contains 'True' in a certain Column named publishFlag */ @Transactional public bloolean isBeingPublished(Long recordID){ ... } /** * sets the record's publishFlag column to true indicating that it's being published */ @Transactional public boolean setBeingPublished(Long recordID){ ... } } </code></pre> <p>And the following class using it :</p> <pre><code>public class MyClass { @Autowired MyDAO dao; public void publishRecords(List&lt;Long&gt; ids){ for(Long id : ids){ if(!dao.isBeingPublished(id)){ dao.setBeingPublished(id); //do something to publish the record } } } } </code></pre> <p>My questions are :</p> <ul> <li><p>First of all, will the <code>!dao.isBeingPublished(id)</code> and <code>dao.setBeingPublished(id)</code> be executed in the same transaction or in separate ones?</p></li> <li><p>Second question's about concurrency, Multiple <code>MyClass</code> instances can be created and concurrent calls to the <code>publishRecord</code> method can occur, so two concurrent calls to <code>!dao.isBeingPublished(id)</code> might both give the same result and thus making the record <strong>published twice!</strong> I would consider making the <code>publishRecords</code> synchronized but the application may be deployed on multiple servers which renders the synchronized declaration useless, hence my question about transactions since the database is the only shared resource between the apps deployed on those servers.</p></li> </ul> <p>What would be the solution to my problem exactly? I read about spring's transaction propagation and found out that <code>REQUIRES_NEW</code> would create a new transaction even if one is currently being executed, but still, I just can't see how that's going to be a solution to my problem.</p> <p>Thank you in advance for your help.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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