Program: 112233
Project: 112233
Buggy source:
private void saveUnsynced(final Iterator<Long> theResultIter){
  TransactionTemplate txTemplate=new TransactionTemplate(myTxManager);
  txTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW);
  txTemplate.execute(new TransactionCallbackWithoutResult(){
    @Override protected void doInTransactionWithoutResult(    TransactionStatus theArg0){
      if (mySearch.getId() == null) {
        doSaveSearch();
      }
      List<SearchResult> resultsToSave=Lists.newArrayList();
      for (      Long nextPid : myUnsyncedPids) {
        SearchResult nextResult=new SearchResult(mySearch);
        nextResult.setResourcePid(nextPid);
        nextResult.setOrder(myCountSaved++);
        resultsToSave.add(nextResult);
      }
      mySearchResultDao.save(resultsToSave);
synchronized (mySyncedPids) {
        int numSyncedThisPass=myUnsyncedPids.size();
        ourLog.info("String_Node_Str",numSyncedThisPass);
        mySyncedPids.addAll(myUnsyncedPids);
        myUnsyncedPids.clear();
        if (theResultIter.hasNext() == false) {
          mySearch.setStatus(SearchStatusEnum.FINISHED);
          mySearch.setTotalCount(myCountSaved);
          for (          CountDownLatch next : myResultSizeLatch) {
            while (next.getCount() > 0) {
              next.countDown();
            }
          }
        }
 else {
          if (myResultSizeLatch.isEmpty() == false) {
            for (            CountDownLatch next : myResultSizeLatch) {
              for (int i=0; i < numSyncedThisPass; i++) {
                next.countDown();
              }
            }
          }
        }
      }
      mySearch.setNumFound(myCountSaved);
      doSaveSearch();
    }
  }
);
  myInitialCollectionLatch.countDown();
}

Fixed version (ground truth):
private void saveUnsynced(final Iterator<Long> theResultIter){
  TransactionTemplate txTemplate=new TransactionTemplate(myTxManager);
  txTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW);
  txTemplate.execute(new TransactionCallbackWithoutResult(){
    @Override protected void doInTransactionWithoutResult(    TransactionStatus theArg0){
      if (mySearch.getId() == null) {
        doSaveSearch();
      }
      List<SearchResult> resultsToSave=Lists.newArrayList();
      for (      Long nextPid : myUnsyncedPids) {
        SearchResult nextResult=new SearchResult(mySearch);
        nextResult.setResourcePid(nextPid);
        nextResult.setOrder(myCountSaved++);
        resultsToSave.add(nextResult);
      }
      mySearchResultDao.save(resultsToSave);
synchronized (mySyncedPids) {
        int numSyncedThisPass=myUnsyncedPids.size();
        ourLog.trace("String_Node_Str",numSyncedThisPass);
        mySyncedPids.addAll(myUnsyncedPids);
        myUnsyncedPids.clear();
        if (theResultIter.hasNext() == false) {
          mySearch.setStatus(SearchStatusEnum.FINISHED);
          mySearch.setTotalCount(myCountSaved);
          for (          CountDownLatch next : myResultSizeLatch) {
            while (next.getCount() > 0) {
              next.countDown();
            }
          }
        }
 else {
          if (myResultSizeLatch.isEmpty() == false) {
            for (            CountDownLatch next : myResultSizeLatch) {
              for (int i=0; i < numSyncedThisPass; i++) {
                next.countDown();
              }
            }
          }
        }
      }
      mySearch.setNumFound(myCountSaved);
      doSaveSearch();
    }
  }
);
  myInitialCollectionLatch.countDown();
}