Transaction in 'replace' method with 'cas'


#1

Hi.

I have a question about the ‘replace’ method with ‘cas’.
Is there any option to success all or fail all if one of cas is invalid when update multiple documents?

Here is a sample code. (tested on SDK 2.0)
$cluster = new CouchbaseCluster(‘http://*****’);
$bucket = $cluster->openBucket(‘mybucket’);
$res = $bucket->get(array(‘test:1’, ‘test:2’, ‘test:3’));
……
$up = $bucket->replace(array(
‘test:1’ => array(‘value’ => $value1, ‘cas’ => $res[“test:1”]->cas),
‘test:2’ => array(‘value’ => $value2, ‘cas’ => $res[“test:2”]->cas),
‘test:3’ => array(‘value’ => $value3, ‘cas’ => $res[“test:3”]->cas)
)
);

This is works fine with cas.
If ‘test:1’ cas is invalid, there is error only ‘test:1’ and others are update.

But, I wanna update all or rollback all if one of cas is invalid from them.
Is it possible? Could you please provide this option?

Thanks


#2

Hey ronpark,

Due to the distributed nature of Couchbase Server, performing atomic updates across multiple keys is not directly possible. It is however possible to use CAS to perform your updates one at a time, and then when you receive an error you can attempt to ‘roll back’ previous updated, however this is slow and prone to errors should you’re rollback attempt fail for any reason.

Cheers, Brett


#3

Hi. Brett.

Thanks for replay answer.

I understand it will be slow.
But, there is already support multiple key update as array.
So, it may useful in case if support an option for this.

Could you please consider this support? :smile:

Thanks again.


#4

Hey ronpark,

The reason that I do not provide support for this by default within the library is that the failure handling for each individual operation that may fail (include rollback attempts) is not something that can be predetermined and would be application specific.

Cheers, Brett


#5

Thanks Brett for answer.

Ok, I understand that.
It may need to build in application level in case then.

Thanks a lot.