Class TransactionAttemptContext
These methods are blocking/synchronous. See CoreTransactionAttemptContext
for the asynchronous version (which is
the
preferred option, and which this class largely simply wraps).
-
Method Summary
Modifier and TypeMethodDescriptionget
(Collection collection, String id) Gets a document from the specified Couchbasecollection
matching the specifiedid
.get
(Collection collection, String id, TransactionGetOptions options) Gets a document from the specified Couchbasecollection
matching the specifiedid
.getReplicaFromPreferredServerGroup
(Collection collection, String id) A convenience wrapper aroundgetReplicaFromPreferredServerGroup(Collection, String, TransactionGetReplicaFromPreferredServerGroupOptions)
using default options.getReplicaFromPreferredServerGroup
(Collection collection, String id, TransactionGetReplicaFromPreferredServerGroupOptions options) Gets a document from the specified Couchbasecollection
matching the specifiedid
.insert
(Collection collection, String id, Object content) Inserts a new document into the specified Couchbasecollection
.insert
(Collection collection, String id, Object content, TransactionInsertOptions options) Inserts a new document into the specified Couchbasecollection
.Calls query() with default options.query
(Scope scope, String statement, TransactionQueryOptions options) Runs a N1QL query and returns the result.Calls query() with default options.query
(String statement, TransactionQueryOptions options) Runs a N1QL query and returns the result.void
Removes the specifieddoc
.replace
(TransactionGetResult doc, Object content) Mutates the specifieddoc
with new content.replace
(TransactionGetResult doc, Object content, TransactionReplaceOptions options) Mutates the specifieddoc
with new content.
-
Method Details
-
get
Gets a document from the specified Couchbasecollection
matching the specifiedid
. If the document is not found, aDocumentNotFoundException
is thrown.- Parameters:
collection
- the Couchbase collection the document exists onid
- the document's ID- Returns:
- a
TransactionGetResult
containing the document
-
get
Gets a document from the specified Couchbasecollection
matching the specifiedid
. If the document is not found, aDocumentNotFoundException
is thrown.- Parameters:
collection
- the Couchbase collection the document exists onid
- the document's IDoptions
- options controlling the operation- Returns:
- a
TransactionGetResult
containing the document
-
getReplicaFromPreferredServerGroup
A convenience wrapper aroundgetReplicaFromPreferredServerGroup(Collection, String, TransactionGetReplicaFromPreferredServerGroupOptions)
using default options. -
getReplicaFromPreferredServerGroup
public TransactionGetResult getReplicaFromPreferredServerGroup(Collection collection, String id, TransactionGetReplicaFromPreferredServerGroupOptions options) Gets a document from the specified Couchbasecollection
matching the specifiedid
.It will be fetched only from document copies that on nodes in the preferred server group, which can be configured with
CoreEnvironment.Builder.preferredServerGroup(String)
.If no replica can be retrieved, which can include for reasons such as this preferredServerGroup not being set, and misconfigured server groups, then
DocumentUnretrievableException
can be raised. It is strongly recommended that this method always be used with a fallback strategy, such as:try { var result = ctx.getReplicaFromPreferredServerGroup(collection, id); } catch (DocumentUnretrievableException err) { var result = ctx.get(collection, id); }
- Parameters:
collection
- the Couchbase collection the document exists onid
- the document's IDoptions
- options controlling the operation- Returns:
- a
TransactionGetResult
containing the document
-
replace
Mutates the specifieddoc
with new content.The mutation is staged until the transaction is committed. That is, any read of the document by any Couchbase component will see the document's current value, rather than this staged or 'dirty' data. If the attempt is rolled back, the staged mutation will be removed.
This staged data effectively locks the document from other transactional writes until the attempt completes (commits or rolls back).
If the mutation fails with a
CasMismatchException
, or any other exception, the transaction will automatically rollback this attempt, then retry.- Parameters:
doc
- the doc to be updatedcontent
- the content to replace the doc with. This will normally be aJsonObject
.- Returns:
- the doc, updated with its new CAS value. For performance a copy is not created and the original doc object is modified.
-
replace
public TransactionGetResult replace(TransactionGetResult doc, Object content, TransactionReplaceOptions options) Mutates the specifieddoc
with new content.The mutation is staged until the transaction is committed. That is, any read of the document by any Couchbase component will see the document's current value, rather than this staged or 'dirty' data. If the attempt is rolled back, the staged mutation will be removed.
This staged data effectively locks the document from other transactional writes until the attempt completes (commits or rolls back).
If the mutation fails with a
CasMismatchException
, or any other exception, the transaction will automatically rollback this attempt, then retry.- Parameters:
doc
- the doc to be updatedcontent
- the content to replace the doc with. This will normally be aJsonObject
.options
- options controlling the operation- Returns:
- the doc, updated with its new CAS value. For performance a copy is not created and the original doc object is modified.
-
insert
Inserts a new document into the specified Couchbasecollection
.As with
replace(com.couchbase.client.java.transactions.TransactionGetResult, java.lang.Object)
, the insert is staged until the transaction is committed.This staged data effectively locks the document from other transactional writes until the attempt completes (commits or rolls back).
- Parameters:
collection
- the Couchbase collection in which to insert the docid
- the document's unique IDcontent
- the content to insert. Generally this will be aJsonObject
- Returns:
- the doc, updated with its new CAS value and ID, and converted to a
TransactionGetResult
-
insert
public TransactionGetResult insert(Collection collection, String id, Object content, TransactionInsertOptions options) Inserts a new document into the specified Couchbasecollection
.As with
replace(com.couchbase.client.java.transactions.TransactionGetResult, java.lang.Object)
, the insert is staged until the transaction is committed.This staged data effectively locks the document from other transactional writes until the attempt completes (commits or rolls back).
- Parameters:
collection
- the Couchbase collection in which to insert the docid
- the document's unique IDcontent
- the content to insert. Generally this will be aJsonObject
options
- options controlling the operation- Returns:
- the doc, updated with its new CAS value and ID, and converted to a
TransactionGetResult
-
remove
Removes the specifieddoc
.As with
replace(com.couchbase.client.java.transactions.TransactionGetResult, java.lang.Object)
, the remove is staged until the transaction is committed. That is, the document will continue to exist, and the rest of the Couchbase platform will continue to see it.This staged data effectively locks the document from other transactional writes until the attempt completes (commits or rolls back).
Note that a
remove(String id)
method is not possible, as it's necessary to check a providedTransactionGetResult
to determine if the document is involved in another transaction.- Parameters:
doc
- the doc to be removed
-
query
Runs a N1QL query and returns the result.All rows are buffered in-memory.
- Throws:
CouchbaseException
- or an error derived from it on failure. The application can choose to catch and ignore this error, and the transaction attempt is allowed to continue. This differs from Key-Value operations, whose failure will cause the attempt to fail.
-
query
Calls query() with default options. -
query
Runs a N1QL query and returns the result.All rows are buffered in-memory.
This overload performs a 'scope-level query': that is, one in which a collection may be referenced by name in the query statement, without needing to specify the full bucket.scope.collection syntax.
- Throws:
CouchbaseException
- or an error derived from it on failure. The application can choose to catch and ignore this error, and the transaction attempt is allowed to continue. This differs from Key-Value operations, whose failure will cause the attempt to fail.
-
query
Calls query() with default options.This overload performs a 'scope-level query': that is, one in which a collection may be referenced by name in the query statement, without needing to specify the full bucket.scope.collection syntax.
-