Class TransactionAttemptContext
- java.lang.Object
-
- com.couchbase.client.java.transactions.TransactionAttemptContext
-
public class TransactionAttemptContext extends Object
Provides methods to allow an application's transaction logic to read, mutate, insert and delete documents, as well as commit or rollback the transaction.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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TransactionGetResult
get(Collection collection, String id)
Gets a document from the specified Couchbasebucket
matching the specifiedid
.TransactionGetResult
insert(Collection collection, String id, Object content)
Inserts a new document into the specified Couchbasecollection
.TransactionQueryResult
query(Scope scope, String statement)
Calls query() with default options.TransactionQueryResult
query(Scope scope, String statement, TransactionQueryOptions options)
Runs a N1QL query and returns the result.TransactionQueryResult
query(String statement)
Calls query() with default options.TransactionQueryResult
query(String statement, TransactionQueryOptions options)
Runs a N1QL query and returns the result.void
remove(TransactionGetResult doc)
Removes the specifieddoc
.TransactionGetResult
replace(TransactionGetResult doc, Object content)
Mutates the specifieddoc
with new content.
-
-
-
Method Detail
-
get
public TransactionGetResult get(Collection collection, String id)
Gets a document from the specified Couchbasebucket
matching the specifiedid
. If the document is not found, aDocumentNotFoundException
is thrown. If not caught inside the transaction logic, this particular exception will cause the overall transaction to abort with a thrownTransactionFailed
.- Parameters:
collection
- the Couchbase collection the document exists onid
- the document's ID- Returns:
- a
TransactionGetResult
containing the document
-
replace
public TransactionGetResult replace(TransactionGetResult doc, Object content)
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.
-
insert
public TransactionGetResult insert(Collection collection, String id, Object content)
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. Due to technical limitations it is not as possible to completely hide the staged data from the rest of the Couchbase platform, as an empty document must be created.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
-
remove
public void remove(TransactionGetResult doc)
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
public TransactionQueryResult query(String statement, TransactionQueryOptions options)
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
public TransactionQueryResult query(String statement)
Calls query() with default options.
-
query
public TransactionQueryResult query(Scope scope, String statement, TransactionQueryOptions options)
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
public TransactionQueryResult query(Scope scope, String statement)
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.
-
-