3

There is a feature on the CouchbaseTemplate class in Spring Data Couchbase to set how exceptions are handled on writes, using the setWriteResultChecking method.

There doesn't seem to be any information on this in the documentation.

If you dig into the source code, the default setting is NONE. This means that exceptions get logged but don't get passed back up to your calling code.

Please does anyone know why this is the default behaviour? Wouldn't it make more sense to use EXCEPTION, so at least your calling code knows that something has gone wrong?

I've not got a lot of experience with Spring data for Couchbase, or indeed Couchbase, so I feel like I'm missing a fundamental point here.

Thanks.


For future reference, this is how I set the write checking to EXCEPTION:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.config.BeanNames;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import org.springframework.data.couchbase.core.WriteResultChecking;

@Configuration
public class CouchbaseConfig extends AbstractCouchbaseConfiguration {

    @Override
    @Bean(name = BeanNames.COUCHBASE_TEMPLATE)
    public CouchbaseTemplate couchbaseTemplate() throws Exception {
        CouchbaseTemplate couchbaseTemplate = super.couchbaseTemplate();
        couchbaseTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION);
        return couchbaseTemplate;
    }
}

I didn't include this as an answer, because it isn't answering the question. The quesstion was about why NONE is the default.

A_M
  • 7,693
  • 6
  • 33
  • 37
  • 1
    Yes, the documentation can be improved. The default behavior is to fail silently rather than throwing exception. It is consistent with other spring data projects having the option to configure setWriteResultChecking (https://github.com/spring-projects/spring-data-mongodb/blob/1b7678a6af8c21e2584143ae215b18e50bcc6b7f/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java#L180) – subhashni Jul 03 '18 at 07:40
  • 1
    Thanks. I'd seen that Spring Data for MongoDB did the same, via this unanswered question: https://stackoverflow.com/questions/35132938 - I was wondering what the thinking is behind all of this - why is it better to fail silently? Is there an alternative way of knowing if a write succeeded or not so that you don't have to rely upon a thrown exception? Currently, my calls to "save" on a couchbase repository return OK whether the write worked or failed. – A_M Jul 03 '18 at 08:07
  • CrudRepository/Operations across these data projects are similar and don't define exceptions. Currently they don't return a value, exceptions are the only way to tell if the write failed, the common exceptions on a valid write failures are already thrown without changing the default setWriteResultChecking (DocumentAlreadyExistsException, CASMismatchException as OptimisticLockingFailureException), others like Timeout, Auth failure, Backpressure are not thrown unless setWriteResultChecking is set. – subhashni Jul 03 '18 at 16:11
  • Hi. We've faced with same problem. Exceptions are just muted. Did you find the way to change writeResultChecking parameter? – SoulCub Oct 02 '18 at 14:58
  • 1
    @SoulCub - I've added some code on how we did that above – A_M Oct 04 '18 at 15:49

0 Answers0