Provides methods to perform transactional operations.
More...
#include <attempt_context.hxx>
Provides methods to perform transactional operations.
An attempt_context object makes all the transactional kv operations available. Note they can throw a transaction_operation_failed exception, which needs to NOT be caught, or if caught, rethrown, for transactions to work properly.
- Examples:
- examples/game_server.cxx.
◆ commit()
virtual void couchbase::transactions::attempt_context::commit |
( |
| ) |
|
|
pure virtual |
Commits the transaction. All staged replaces, inserts and removals will be written.
After this, no further operations are permitted on this instance, and they will result in an exception that will, if not caught in the transaction logic, cause the transaction to fail.
- Exceptions
-
transaction_operation_failed | which either should not be caught by the lambda, or rethrown if it is caught. |
◆ get()
Gets a document from the specified Couchbase collection matching the specified id.
- Parameters
-
collection | the Couchbase collection the document exists on |
id | the document's ID |
- Returns
- an TransactionDocument containing the document
- Exceptions
-
transaction_operation_failed | which either should not be caught by the lambda, or rethrown if it is caught. |
- Examples:
- examples/game_server.cxx.
◆ get_optional()
virtual boost::optional<transaction_get_result> couchbase::transactions::attempt_context::get_optional |
( |
std::shared_ptr< collection > |
collection, |
|
|
const std::string & |
id |
|
) |
| |
|
pure virtual |
Gets a document from the specified Couchbase collection matching the specified id.
- Parameters
-
collection | the Couchbase collection the document exists on |
id | the document's ID |
- Returns
- a TransactionDocument containing the document, if it exists.
- Exceptions
-
transaction_operation_failed | which either should not be caught by the lambda, or rethrown if it is caught. |
◆ insert()
template<typename Content >
transaction_get_result couchbase::transactions::attempt_context::insert |
( |
std::shared_ptr< collection > |
collection, |
|
|
const std::string & |
id, |
|
|
const Content & |
content |
|
) |
| |
|
inline |
Inserts a new document into the specified Couchbase collection.
As with replace, 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 doc |
id | the document's unique ID |
content | the content to insert |
- Returns
- the doc, updated with its new CAS value and ID, and converted to a TransactionDocument
- Exceptions
-
transaction_operation_failed | which either should not be caught by the lambda, or rethrown if it is caught. |
◆ insert_raw()
virtual transaction_get_result couchbase::transactions::attempt_context::insert_raw |
( |
std::shared_ptr< collection > |
collection, |
|
|
const std::string & |
id, |
|
|
const nlohmann::json & |
content |
|
) |
| |
|
protectedpure virtual |
◆ remove()
Removes the specified document, using the document's last TransactionDocument::cas
As with replace, 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).
- Parameters
-
document | the document to be removed |
- Exceptions
-
transaction_operation_failed | which either should not be caught by the lambda, or rethrown if it is caught. |
- Examples:
- examples/game_server.cxx.
◆ replace()
template<typename Content >
Mutates the specified document with new content, using the document's last TransactionDocument::cas().
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, the transaction will automatically rollback this attempt, then retry.
- Parameters
-
document | the doc to be updated |
content | the content to replace the doc with. |
- Returns
- the document, updated with its new CAS value.
- Exceptions
-
transaction_operation_failed | which either should not be caught by the lambda, or rethrown if it is caught. |
- Examples:
- examples/game_server.cxx.
◆ replace_raw()
◆ rollback()
virtual void couchbase::transactions::attempt_context::rollback |
( |
| ) |
|
|
pure virtual |
Rollback the transaction. All staged mutations will be unstaged.
Typically, this is called internally to rollback transaction when errors occur in the lambda. Though it can be called explicitly from the app logic within the transaction as well, perhaps that is better modeled as a custom exception that you raise instead.
- Exceptions
-
transaction_operation_failed | which either should not be caught by the lambda, or rethrown if it is caught. |
The documentation for this class was generated from the following file: