|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.unboundid.ldap.sdk.LDAPRequest
com.unboundid.ldap.sdk.ExtendedRequest
com.unboundid.ldap.sdk.extensions.StartTransactionExtendedRequest
@NotMutable @ThreadSafety(level=NOT_THREADSAFE) public final class StartTransactionExtendedRequest
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.
// 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);
}
}
| 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 |
|---|
public static final java.lang.String START_TRANSACTION_REQUEST_OID
| Constructor Detail |
|---|
public StartTransactionExtendedRequest()
public StartTransactionExtendedRequest(Control[] controls)
controls - The set of controls to include in the request.
public StartTransactionExtendedRequest(ExtendedRequest extendedRequest)
throws LDAPException
extendedRequest - The generic extended request to use to create this
start transaction extended request.
LDAPException - If a problem occurs while decoding the request.| Method Detail |
|---|
public StartTransactionExtendedResult process(LDAPConnection connection,
int depth)
throws LDAPException
process in class ExtendedRequestconnection - 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.
LDAPException - If a problem occurs while sending the request or
reading the response.public StartTransactionExtendedRequest duplicate()
duplicate in interface ReadOnlyLDAPRequestduplicate in class ExtendedRequestpublic StartTransactionExtendedRequest duplicate(Control[] controls)
duplicate in interface ReadOnlyLDAPRequestduplicate in class ExtendedRequestcontrols - The set of controls to include in the duplicate request.
public java.lang.String getExtendedRequestName()
getExtendedRequestName in class ExtendedRequestpublic void toString(java.lang.StringBuilder buffer)
toString in interface ProtocolOptoString in interface ReadOnlyLDAPRequesttoString in class ExtendedRequestbuffer - The buffer to which to append a string representation of
this request.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||