Read/Modify/Write

If you are retrieving a document from the container for the purpose of modifying or deleting it, you should declare a read-modify-write cycle at the time that you read the document. Doing so causes BDB XML to obtain write locks (instead of a read locks) at the time of the read. This helps to prevent deadlocks by preventing another transaction from acquiring a read lock on the same record while the read-modify-write cycle is in progress.

Note that declaring a read-modify-write cycle may actually increase the amount of blocking that your application sees, because readers immediately obtain write locks and write locks cannot be shared. For this reason, you should use read-modify-write cycles only if you are seeing a large amount of deadlocking occurring in your application.

In order to declare a read/modify/write cycle when you perform a read operation, pass the DB_RMW flag to the XmlQueryExpression::execute() or XmlManager::query() method.

For example:

XmlTransaction txn;
try {
    txn = myManager.createTransaction();

    // Get a query context
    XmlQueryContext context = myManager.createQueryContext();
    // Declare a namespace
    context.setNamespace("fruits", "http://groceryItem.dbxml/fruits");

    // Declare the query string. Find all the product documents
    // in the fruits namespace.
    std::string myQuery = "collection('exampleData.dbxml')/fruits:product";

    // Perform the query. Declare a read/modify/write cycle
    XmlResults results = myManager.query(txn, myQuery, context, DB_RMW);

    // Delete everything in the results set
    XmlUpdateContext uc = myManager.createUpdateContext();
    XmlDocument theDoc = myManager.createDocument();
    while (results.next(theDoc)) {
        myContainer.deleteDocument(txn, theDoc, uc);
    } 

    txn.commit();
} catch (XmlException &e) { 
    // Perform exception handling as is normal
}
return (EXIT_SUCCESS);