title: Correct way to retry with delay on Couchbase getAndLock if lock was already held?
body: <p>I am using the new Couchbase Java Client API 2.1.1 and therefore JavaRx to access my Couchbase cluster.<BR>
  When using asynchronous <code>getAndLock</code> on an already locked document, getAndLock fails with a <code>TemporaryLockFailureException</code>. In another SO question (<a href="https://stackoverflow.com/questions/22066481/rxjava-can-i-use-retry-but-with-delay?rq=1">rxjava: Can I use retry() but with delay?</a>) I found out how to retry with delay.</p>
  
  <p>Here is my adopted code:</p>
  
  <pre><code>    CountDownLatchWithResultData&lt;JsonDocument&gt; resultCdl = new CountDownLatchWithResultData&lt;&gt;(1);
  
      couchbaseBucket.async().getAndLock(key, LOCK_TIME).retryWhen((errorObserver) -&gt; {
          return errorObserver.flatMap((Throwable t) -&gt; {
              if (t instanceof TemporaryLockFailureException) {
                  return Observable.timer(RETRY_DELAY_MS, TimeUnit.MILLISECONDS);
              }
              return Observable.error(t);
          });
      }).subscribe(new Subscriber&lt;JsonDocument&gt;() {
  
          @Override
          public void onCompleted() {
              resultCdl.countDown();
          }
  
          @Override
          public void onError(Throwable e) {
              resultCdl.countDown();
          }
  
          @Override
          public void onNext(JsonDocument t) {
              resultCdl.setInformation(t);
          }
  
      });
  
      ........
  
      resultCdl.await();
  
      if (resultCdl.getInformation() == null) {
          //do stuff
      } else ....
  </code></pre>
  
  <p>(<code>CountDownLatchWithResultData</code> simply extends a normal <code>CountDownLatch</code> and adds two methods to store some information before the count has reached 0 and retrieve it afterwards)</p>
  
  <p>So basically I'd like this code to</p>
  
  <ul>
  <li>try to get the lock infinitely once every <code>RETRY_DELAY_MS</code> milliseconds if a <code>TemporaryLockFailureException</code> occured and then call <code>onNext</code></li>
  <li>or to fail completely on other exceptions</li>
  <li>or to directly call <code>onNext</code> if there is no exception at all</li>
  </ul>
  
  <p>The problem now is that when retrying, it only retries once and the JsonDocument from <code>resultCdl.getInformation()</code> is always null in this case even though the document exists. It seems <code>onNext</code> is never called.<BR>
  If there is no exception, the code works fine.<BR>
  So apparently I am doing something wrong here but I have no clue as to where the problem might be. Does returning <code>Observable.timer</code> imply that with this new Obervable also the previously associated <code>retryWhen</code> is executed again? Is it the <code>CountDownLatch</code> with count 1 getting in the way?</p>