com.unboundid.ldap.sdk.extensions
Class StartTransactionExtendedRequest

java.lang.Object
  extended by com.unboundid.ldap.sdk.LDAPRequest
      extended by com.unboundid.ldap.sdk.ExtendedRequest
          extended by com.unboundid.ldap.sdk.extensions.StartTransactionExtendedRequest
All Implemented Interfaces:
ProtocolOp, ReadOnlyLDAPRequest, java.io.Serializable

@NotMutable
@ThreadSafety(level=NOT_THREADSAFE)
public final class StartTransactionExtendedRequest
extends ExtendedRequest

This class provides an implementation of the start transaction extended request as defined in RFC 5805. It may be used to begin a transaction that allows multiple write operations to be processed as a single atomic unit. The StartTransactionExtendedResult that is returned will include a transaction ID. For each operation that is performed as part of the transaction, this transaction ID should be included in the corresponding request through the TransactionSpecificationRequestControl. Finally, after all requests for the transaction have been submitted to the server, the EndTransactionExtendedRequest should be used to commit that transaction, or it may also be used to abort the transaction if it is decided that it is no longer needed.

Example

The following example demonstrates the process for using LDAP transactions. It will modify two different entries as a single atomic unit. In each case, it will use the post-read control to retrieve a copy of the updated entry.
   // Send the start transaction operation and get the transaction ID.
   StartTransactionExtendedRequest startTxnRequest =
        new StartTransactionExtendedRequest();
   StartTransactionExtendedResult startTxnResult =
        (StartTransactionExtendedResult)
        connection.processExtendedOperation(startTxnRequest);
   if (startTxnResult.getResultCode() != ResultCode.SUCCESS)
   {
     throw new LDAPException(startTxnResult);
   }
   ASN1OctetString txnID = startTxnResult.getTransactionID();

   // At this point, we have a transaction available for use.  If any error
   // occurs, we will want to make sure that the transaction is aborted, so
   // use a try/finally block to handle that.
   boolean shouldAbort = true;
   try
   {
     // Create and send the first modify request as part of the transaction.
     // Make sure to include the transaction specification control and the
     // post-read request control in the modify request.
     ModifyRequest modifyRequest1 = new ModifyRequest(
          "cn=first,dc=example,dc=com",
          new Modification(ModificationType.REPLACE, "description", "first");
     modifyRequest1.addControl(new TransactionSpecificationControl(txnID));
     modifyRequest1.addControl(new PostReadRequestControl());
     LDAPResult modifyResult1 = connection.modify(modifyRequest1);

     // Create and send the second modify request as part of the transaction.
     // Again, make sure to include the appropriate controls in the request.
     ModifyRequest modifyRequest2 = new ModifyRequest(
          "cn=second,dc=example,dc=com",
          new Modification(ModificationType.REPLACE, "description", "second");
     modifyRequest2.addControl(new TransactionSpecificationControl(txnID));
     modifyRequest2.addControl(new PostReadRequestControl());
     LDAPResult modifyResult2 = connection.modify(modifyRequest1);

     // Now we're ready to commit, which we can do with the end transaction
     // request with the commit flag set to true.
     EndTransactionExtendedRequest commitRequest =
          new EndTransactionExtendedRequest(txnID, true);
     EndTransactionExtendedResult commitResult =
          (EndTransactionExtendedResult)
          connection.processExtendedOperation(commitRequest);
     if (commitResult.getResultCode() == ResultCode.SUCCESS)
     {
       System.out.println("The transaction was committed successfully.");

       // Everything was successful, so we don't need to abort anything.
       shouldAbort = false;

       // Get the post-read response control for the first modify operation.
       // It's the same process for the second, but this example is already
       // long enough so we'll skip it.
       Control[] controls = commitResult.getOperationResponseControls(
            modifyResult1.getMessageID());
       if (controls != null)
       {
         for (Control c : controls)
         {
           if (c instanceof PostReadResponseControl)
           {
             PostReadResponseControl postReadResponse =
                  (PostReadResponseControl) c;
             System.out.println("First entry after the modification:");
             System.out.println(postReadResponse.getEntry().toLDIFString());
           }
         }
       }
     }
     else
     {
       // The transaction failed for some reason.  The response should tell us
       // whether it failed because of one of the operations.
       int failedOpMessageID = commitResult.getFailedOpMessageID();
       if (failedOpMessageID == modifyResult1.getMessageID())
       {
         System.err.println("The transaction failed because of a failure " +
              "encountered while processing the first modification.");
       }
       else if (failedOpMessageID == modifyResult2.getMessageID())
       {
         System.err.println("The transaction failed because of a failure " +
              "encountered while processing the second modification.");
       }
       else
       {
         System.err.println("The transaction failed for some reason other " +
              "than either of the modify operations.");
       }

       throw new LDAPException(commitResult);
     }
   }
   finally
   {
     if (shouldAbort)
     {
       // Setting the commit flag to false in the end transaction request will
       // will cause the transaction to be aborted rather than committed.
       EndTransactionExtendedRequest abortRequest =
             new EndTransactionExtendedRequest(txnID, false);
       connection.processExtendedOperation(abortRequest);
     }
   }
 

See Also:
Serialized Form

Field Summary
static java.lang.String START_TRANSACTION_REQUEST_OID
          The OID (1.3.6.1.1.21.1) for the start transaction extended request.
 
Fields inherited from class com.unboundid.ldap.sdk.ExtendedRequest
TYPE_EXTENDED_REQUEST_OID, TYPE_EXTENDED_REQUEST_VALUE
 
Constructor Summary
StartTransactionExtendedRequest()
          Creates a new start transaction extended request.
StartTransactionExtendedRequest(Control[] controls)
          Creates a new start transaction extended request.
StartTransactionExtendedRequest(ExtendedRequest extendedRequest)
          Creates a new start transaction extended request from the provided generic extended request.
 
Method Summary
 StartTransactionExtendedRequest duplicate()
          Creates a new instance of this LDAP request that may be modified without impacting this request.
 StartTransactionExtendedRequest duplicate(Control[] controls)
          Creates a new instance of this LDAP request that may be modified without impacting this request.
 java.lang.String getExtendedRequestName()
          Retrieves the user-friendly name for the extended request, if available.
 StartTransactionExtendedResult process(LDAPConnection connection, int depth)
          Sends this extended request to the directory server over the provided connection and returns the associated response.
 void toString(java.lang.StringBuilder buffer)
          Appends a string representation of this request to the provided buffer.
 
Methods inherited from class com.unboundid.ldap.sdk.ExtendedRequest
encodeProtocolOp, getLastMessageID, getOID, getOperationType, getProtocolOpType, getValue, hasValue, responseReceived, writeTo
 
Methods inherited from class com.unboundid.ldap.sdk.LDAPRequest
followReferrals, getControl, getControlList, getControls, getIntermediateResponseListener, getResponseTimeoutMillis, hasControl, hasControl, setFollowReferrals, setIntermediateResponseListener, setResponseTimeoutMillis, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

START_TRANSACTION_REQUEST_OID

public static final java.lang.String START_TRANSACTION_REQUEST_OID
The OID (1.3.6.1.1.21.1) for the start transaction extended request.

See Also:
Constant Field Values
Constructor Detail

StartTransactionExtendedRequest

public StartTransactionExtendedRequest()
Creates a new start transaction extended request.


StartTransactionExtendedRequest

public StartTransactionExtendedRequest(Control[] controls)
Creates a new start transaction extended request.

Parameters:
controls - The set of controls to include in the request.

StartTransactionExtendedRequest

public StartTransactionExtendedRequest(ExtendedRequest extendedRequest)
                                throws LDAPException
Creates a new start transaction extended request from the provided generic extended request.

Parameters:
extendedRequest - The generic extended request to use to create this start transaction extended request.
Throws:
LDAPException - If a problem occurs while decoding the request.
Method Detail

process

public StartTransactionExtendedResult process(LDAPConnection connection,
                                              int depth)
                                       throws LDAPException
Sends this extended request to the directory server over the provided connection and returns the associated response.

Overrides:
process in class ExtendedRequest
Parameters:
connection - The connection to use to communicate with the directory server.
depth - The current referral depth for this request. It should always be one for the initial request, and should only be incremented when following referrals.
Returns:
An LDAP result object that provides information about the result of the extended operation processing.
Throws:
LDAPException - If a problem occurs while sending the request or reading the response.

duplicate

public StartTransactionExtendedRequest duplicate()
Creates a new instance of this LDAP request that may be modified without impacting this request.. Subclasses should override this method to return a duplicate of the appropriate type.

Specified by:
duplicate in interface ReadOnlyLDAPRequest
Overrides:
duplicate in class ExtendedRequest
Returns:
A new instance of this LDAP request that may be modified without impacting this request.

duplicate

public StartTransactionExtendedRequest duplicate(Control[] controls)
Creates a new instance of this LDAP request that may be modified without impacting this request. The provided controls will be used for the new request instead of duplicating the controls from this request.. Subclasses should override this method to return a duplicate of the appropriate type.

Specified by:
duplicate in interface ReadOnlyLDAPRequest
Overrides:
duplicate in class ExtendedRequest
Parameters:
controls - The set of controls to include in the duplicate request.
Returns:
A new instance of this LDAP request that may be modified without impacting this request.

getExtendedRequestName

public java.lang.String getExtendedRequestName()
Retrieves the user-friendly name for the extended request, if available. If no user-friendly name has been defined, then the OID will be returned.

Overrides:
getExtendedRequestName in class ExtendedRequest
Returns:
The user-friendly name for this extended request, or the OID if no user-friendly name is available.

toString

public void toString(java.lang.StringBuilder buffer)
Appends a string representation of this request to the provided buffer.

Specified by:
toString in interface ProtocolOp
Specified by:
toString in interface ReadOnlyLDAPRequest
Overrides:
toString in class ExtendedRequest
Parameters:
buffer - The buffer to which to append a string representation of this request.