Couchbase java-client IllegalReferenceCountException

369 views Asked by At

I was getting this error on java-client-2.1.0 while trying to work with AsyncBucket:

com.couchbase.client.deps.io.netty.util.IllegalReferenceCountException: refCnt: 0, decrement: 1

After a while I figured it out. Couchbase completely ignores this scenario so there's no proper feedback from the library and the Netty internals aren't helping.

1

There are 1 answers

0
Yuri Geinish On

Turns out you'll get that error when consuming CB's items more than once. For example,

val getObs = asyncBucket.get("blah")
val emptyObs = getObs.isEmpty.doOnEach(...).subscribe()
val docObs = getObs.doOnEach(...).subscribe()

That is the only way I could come up with to handle a "document not found" scenario.

To work around this, use cache():

val getObs = asyncBucket.get("blah").cache()

The caching observer will consume the Couchbase's item and then the multiple subscribers can safely consume from the cache.

If you're using rxscala, the fix is the same.

getObs.singleOption.foreach { ... }

Would fail without the cache.