Aborting a Transaction

When you abort a transaction, all database modifications performed under the protection of the transaction are discarded, and all locks currently held by the transaction are released. In this event, your data is simply left in the state that it was in before the transaction began performing data modifications.

Once you have aborted a transaction, the transaction handle that you used for the transaction is no longer valid. To perform database activities under the control of a new transaction, you must obtain a fresh transactional handle.

To abort a transaction, call XmlTransaction::abort().

Note that if you are using a retry block, then in your exception handling you should suspect any object used in an update operation. You should not trust the object because the operqtion can be destructive. For example, in the following code fragment you must init a new XmlDocument object in your retry block:

const int maxRetry = 50;

for (int retry = 0; ; retry++) {

    //Create String contents for documents.
    std::string xmlString = "<aDoc>green</aDoc>;";

    //Get an update context
    XmlUpdateContext updateContext = mgr.createUpdateContext();

    //Start a transaction
    XmlTransaction txn = mgr.createTransaction();

    try {
        //Declare an xml document
        XmlDocument xmlDoc = mgr.createDocument();
        
        //Set the xml document's content
        xmlDoc.setContent(xmlString);
        
        //Put the document, asking DB XML to generate a name
        container.putDocument(txn, xmlDoc, updateContext,
                              DBXML_GEN_NAME);
        
        txn.commit();
        break;
    } catch (std::exception &e) {
        if (txn != NULL)
            txn.abort();
        if (retry &gt;= maxRetry) {
            std::cerr << "Message: " << e.what() << "\n";
            throw e;
        }
    }
}