Couchbase Timing Problems

Hi I am new to couchbase . I am using the spring framework (5.2.8) and spring data couchbase (4.0.3) with Couchbase Java SDK 3.0.7. I am writing an integration test that tests my service layer which interacts with couchbase. Before every test I populate my couchbase database with a few documents. After every test I delete everything from the database before the next test. I am running in to timing problems causing spurious exceptions in my tests ranging from DuplicateKeyException to assertion errors reflecting invalid state of my database. It seems that I am sending data requests too quickly and couchbase seems to have a delay before the data becomes available. Putting the thread to sleep for a second before each test causes all my tests to pass as normal. I tried overriding the spring AbstractCouchbaseConfiguration.getDefaultConsistency to set the consistency to Consistency.STRONGLY_CONSISTENT but this method is no longer in the parent class. Is there any other configuration I can change? Please could someone help because I am really stuck. Thanks.

Hi @lowie68
The Couchbase architecture is that each document has an active node where both reads and writes are sent. This is an easy model to work with as you don’t have to worry about eventual consistency or writes going to different nodes. So if you delete a document, wait for the SDK to report that is complete, and then insert it - you should have no duplicate key issues. So the first thing to check I think is that your code is definitely not interleaving the two operations?

Hi Graham Thanks for getting back to me. How do I wait until the SDK reports it is complete? I delete all my documents in the bucket by executing a n1ql query ‘delete from spring-test’. Thanks. Stephen.

Annotate your repository query methods with @ScanConsistency(query=QueryScanConsistency.REQUEST_PLUS), likewise on template calls, use withConsistency(QueryScanConsistency.REQUEST_PLUS)
For the duplicate key on insert - after the delete, do a query with scan consistency as above. When that query completes, the documents will be gone.

That worked. Thank you. I am generating my own keys because I could’nt get the spring data auto-generation to work. Thanks Again. Stephen.

@lowie68 - to use auto=key generation, annotate your id field with GeneratedValue (ignore any back-slashes in the post)

@\GeneratedValue(strategy = GenerationStrategy.UNIQUE)
String id;

hi @mreiche. My keys are complex they have a prefix and count up from 100 e.g. member::100, household::100, household::101 etc. If you could tell me how to generate these automatically it would save me some code. Thanks.