title: Hibernate and infinite loop - duplicate entries
body: <p>I'm having some trouble with work-related issue:</p>
  
  <p>There's a business requirement to actively receive datagram packets via multicast (which in this case means we're using while (true) loop).</p>
  
  <p>The pseudocode looks like this:</p>
  
  <pre><code>while (true) {
    receivePacket();
    parsePacket();
    saveParsedPacketToDatabase();
  }
  </code></pre>
  
  <p>We're using Spring Data JPA to save entities to database - this is the component making inserts:</p>
  
  <pre><code>@Service
  @Transactional
  public class InsertService {
  
      @Inject
      private DataRepository dataRepository;
  
      @Transactional(propagation = Propagation.NEVER)
      public void saveParsedPacketToDatabase(List&lt;Data&gt; data) {
          dataRepository.save(data);
          dataRepository.flush();
      }
  }
  </code></pre>
  
  <p>The Data object containts Date field, let's say insertTime set on <strong>@PrePersist/Update</strong>:</p>
  
  <pre><code>@PreUpdate
  @PrePersist
  void preSave() {
      insertTime = DateTime.now(DateTimeZone.UTC).toDate();
  }
  </code></pre>
  
  <p>Now the problem is we are getting 2 identical objects (all fields are the same <strong>BUT</strong> insertTime and generated id. <strong>Important thing - insertTime differences are milliseconds, no more than 100</strong>) for every inserted entity.</p>
  
  <p>I've been trying to debug this thing and find what the issue is but I have no clue.. We definitely are not getting duplicate data via multicast and I think there might be something wrong with the inserting techinique.. Skipping infinite loop is not an option - maybe there's a need to queue things somehow..</p>
  
  <p>I'd definitely appreciate any help,</p>
  
  <p>Cheers,
  Marcin</p>