Document is not created due to upsert options for expiry on the document

When we are creating/upserting the document we are setting the expiry using upsertoptions as shown below:

Temporal startTm = Instant.now();
Temporal endTm = Instant.now().plus(Period.ofDays(365));
try {
			collection.upsert(id, JsonObject.fromJson(payload), UpsertOptions.upsertOptions().expiry(Duration.between(startTm, endTm)));
		} catch (Exception e) {
			throw e;
		}

So when we run this the data is not saved in couchbase. But if we change the code to :

    try {
    			collection.upsert(id, JsonObject.fromJson(payload));
    		} catch (Exception e) {
    			throw e;
    		}

We are able to save the data successfully.

So please let us know why its not saving data in the first case.

Hi,

The reason the document is not being stored is because any time duration longer than 30 days is considered as a Unix timestamp by the Couchbase server. Therefore the expiry you provide in this case is considered a Unix timestamp in the past, so the document expiries immediately.

To achieve this, you can use the upsertOptions().expiry(Instant) overload that was included in the SDK3.1.0 release. So in your case, you can then just pass endTm into the expiry to use the absolute Unix timestamp. More details can found in the documentation: Key Value Operations | Couchbase Docs

If you are only using the 3.0 SDK, you can do a workaround where you create a Duration that is equal to the timestamp 365 days from now. EDIT: There is a bit more nuance to this; refer to David’s comment below for a more accurate description :slight_smile:

Hope this helps.
Thanks - Will

1 Like

Therefore the expiry you provide in this case is considered a Unix timestamp in the past, so the document expiries immediately.

Agreed, this sounds like SDK bug JCBC-1645 which was fixed in version 3.0.6.

Will is correct, expiry(Instant) has been part of the “committed” public API since version 3.1.0. If you’re not ready to upgrade to 3.1, this method is also present as an “uncommitted” feature in any version after version 3.0.7.

If you are only using the 3.0 SDK, you can do a workaround where you create a Duration that is equal to the timestamp 365 days from now.

This will work in the old SDKs, but if you’re using 3.0.6 or later I’d strongly recommend just passing in the natural duration.

TLDR; Upgrade to Java SDK version 3.0.6 (or later) and just write:

....upsertOptions().expiry(Duration.ofDays(365));

Thanks,
David

1 Like