public class DSTransaction
extends java.lang.Object
DSRequest
or RPCRequest
objects. DSTransaction tracks a queue of requests and provides APIs for commiting, rolling back, managing error status, freeing resources, and enabling features like "Transaction Chaining" (see client-side docs).
For an overview of usage, see the client-side documentation overview "Standalone DataSource Usage".
Modifier and Type | Method and Description |
---|---|
void | addToTemplateContext(java.lang.Object key, java.lang.Object value) Same as the three-argument version , but the third argument (isSnippet) is always false. |
void | addToTemplateContext(java.lang.String name, java.lang.Object value, boolean isSnippet) Adds an attribute to the template context which makes it available when using Velocity templates. |
void | applyEarlierResponseValues(DSRequest dsRequest) Applies values from earlier responses in this transaction to the supplied DSRequest, in accordance with the |
void | commit() Commits the current transaction. |
void | complete() Commits or rolls back the current transaction and frees the DSTransaction's resources. |
BaseResponse | findFirstResponse(java.lang.String dsName, java.lang.String opType) Returns the response for the first request where the DataSource and operation type match the parameter values (null parameters match any DataSource / operation type). |
BaseResponse | findLastResponse(java.lang.String dsName, java.lang.String opType) Returns the response for the request most immediately prior to the current request, where the DataSource and operation type match the parameter values. |
void | freeAllResources() Frees all shared resources (for example, DataSource instances and database connections) used by this DSTransaction, including any DSRequest-level resources that have not already been freed automatically at the end of request processing. |
void | freeQueueResources() Frees shared resources (for example, DataSource instances and database connections) used by this DSTransaction and associated with the overall queue, as opposed to a particular DSRequest within that queue. |
java.lang.Boolean | getAuthenticated() Returns true if we have an authenticated user for this request. |
DataSource | getDataSource(java.lang.String dsName) Returns an instance of the DataSource named in the "dsName" parameter. |
java.util.Set | getRequests() Returns a list of the requests that are registered with this transaction. |
BaseResponse | getResponse(BaseRequest baseRequest) Retrieves a response for a specific request which has been registered with this transaction. |
int | getTransactionPolicy() Gets the TransactionPolicy currently in place for this transaction. |
java.lang.String | getUserId() Returns the user ID associated with the queue of requests being managed by this RPCManager. |
java.util.List | getUserRoles() Returns a list of the roles associated with the user who is authenticated for this request. |
java.lang.Boolean | isClientRequest() Returns this transactions default value for clientRequest. |
boolean | isSkipRemainingQueue() Check if the remaining requests in the queue will be skipped. |
java.util.Map | processQueue() Mainly for standalone usage of DSTransaction, this method will go through each request that has been registered with the DSTransaction and pass it to the request handler. |
java.util.Map | processQueue(BaseRequestHandler requestHandler) Mainly for standalone usage of DSTransaction, this method will go through each request that has been registered with the DSTransaction and pass it to the request handler. |
boolean | queueHasFailures() Returns true if any request in the current queue failed. |
void | registerRequest(BaseRequest request) Registers a request with the transaction. |
void | registerResponse(BaseRequest request, BaseResponse response) Registers a response for a request with the transaction. |
void | rollback() Rolls back the current transaction. |
void | setAuthenticated(boolean authenticated) Pass true to this method to indicate that every request in the queue is associated with an authenticated user. |
void | setClientRequest(java.lang.Boolean clientRequest) Sets the default clientRequest value for any request included in this transaction. |
void | setTransactionPolicy(int transactionPolicy) Sets the TransactionPolicy to use for this transaction, by default this will be fetched from server.properties "RPCManager.transactionPolicy" and if not found will be NOT_SET. |
void | setUserId(java.lang.String userId) Set the user ID associated with the queue of requests being managed by this RPCManager. |
void | setUserRoles(java.util.List rolesList) Accepts a List of roles associated with the user who is authenticated for this request. |
void | setUserRoles(java.lang.String rolesString) Accepts a comma-separated String representing the list of roles associated with the user who is authenticated for this request. |
void | setUserRoles(java.lang.String[] roles) Accepts a List of roles associated with the user who is authenticated for this request. |
void | skipRemainingQueue() Calling this tells the DSTransaction that all the remaining requests in the queue should be skipped. |
public boolean queueHasFailures()
public void skipRemainingQueue()
DSTransaction
that all the remaining requests in the queue should be skipped. Those requests will instead return a response with a status of BaseResponse.STATUS_PROCESSING_SKIPPED
. This ensures that the remaining requests don't actually do any logic processing. A typical use-case for this is when you manually handle a transaction queue and after executing a request, use DSTransaction.queueHasFailures()
to check if there is a failure, you can tell the remaining queue to skip processing, giving you consistent responses for those requests that were skipped.
public boolean isSkipRemainingQueue()
public void registerRequest(BaseRequest request)
request
- the request to add to the transaction.public void registerResponse(BaseRequest request, BaseResponse response)
request
- the request for which the response belongs to.response
- the response.public java.util.Set getRequests()
public void commit() throws java.lang.Exception
DSTransaction.complete()
, if all the DSRequests associated with this DSTransaction completed successfully. Therefore, this method is only necessary if you elect to manually manage the transaction. Note, this method does not free the database connection, so if you do manage the transaction manually, you are responsible for freeing resources - see DSTransaction.freeQueueResources()
. In general, use this API only if you are sure you have a good reason for not using complete()
.java.lang.Exception
public void rollback() throws java.lang.Exception
DSTransaction.complete()
, if one or more of the DSRequests associated with this DSTransaction failed. Therefore, this method is only necessary if you elect to manually manage the transaction. Note, this method does not free the database connection, so if you do manage the transaction manually, you are responsible for freeing resources - see DSTransaction.freeQueueResources()
. In general, use this API only if you are sure you have a good reason for not using complete()
.java.lang.Exception
public int getTransactionPolicy()
public void setTransactionPolicy(int transactionPolicy) throws QueueAlreadyStartedException
TransactionPolicy
to use for this transaction, by default this will be fetched from server.properties "RPCManager.transactionPolicy" and if not found will be NOT_SET.transactionPolicy
- the transaction policy to set.QueueAlreadyStartedException
public void complete()
DSTransaction.freeQueueResources()
).public void freeQueueResources()
freeOnExecute
, in which case its resources are considered to be queue-level resources and will be freed by this method. If you have a need to free all resources associated with a DSTransaction, see DSTransaction.freeAllResources()
. Note, any DSRequest targeting @link{com.isomorphic.hibernate.HibernateDataSource} or JPADataSource
is freeOnExecute:false
by default in order to allow traversing lazy entity associations. Typically, there is no need to free resources manually because it is done automatically when you call DSTransaction.complete()
. Therefore, this method is only necessary if you elect to manually manage the transaction by calling DSTransaction.commit()
or DSTransaction.rollback()
instead of complete()
. Note that the resources freed by this API are made immediately available to other threads: do not cache DataSource instances or other resources and attempt to reuse them after calling this API. In general, use this API only if you are sure you have to.public void freeAllResources()
DSTransaction.freeQueueResources()
. Typically, there is no need to free resources manually because it is done automatically when you call DSTransaction.complete()
. Therefore, this method is only necessary if you elect to manually manage the transaction by calling DSTransaction.commit()
or DSTransaction.rollback()
instead of complete()
. Note that the resources freed by this API are made immediately available to other threads: do not cache DataSource instances or other resources and attempt to reuse them after calling this API. In general, use this API only if you are sure you have to.public BaseResponse getResponse(BaseRequest baseRequest)
baseRequest
- the request for which to get the response.public java.util.Map processQueue()
BaseRequestHandler
and setting it on the DSTransaction. NOTE: this feature is supported only in Power Edition or above.
public java.util.Map processQueue(BaseRequestHandler requestHandler)
BaseRequestHandler
and setting it on the DSTransaction. NOTE: this feature is supported only in Power Edition or above.
requestHandler
- a custom request handler which will deal with individual request execution and error handling.public void applyEarlierResponseValues(DSRequest dsRequest) throws java.lang.Exception
dsRequest
- The DSRequest to apply values tojava.lang.Exception
public BaseResponse findLastResponse(java.lang.String dsName, java.lang.String opType)
response
for the request most immediately prior to the current request, where the DataSource and operation type match the parameter values. For example:DSResponse resp = dsTransaction.findLastResponse("customer", "update");would return the response to the most recent update request on the "customer" DataSource prior to the request currently being processed (ie, the first one in the queue with no response)
This method is just one way to access the response to a request earlier in the queue - it is a programmatic equivalent of using $responseData.last()
in a Velocity expression. Scan the client-side documentation for "transaction chaining" for a full discussion of the various options available when using the Transaction Chaining approach.
dsName
- The name of the DataSource to find a response for (null means any DataSource)opType
- The operation type to find a response for (null means any operation type)public BaseResponse findFirstResponse(java.lang.String dsName, java.lang.String opType)
response
for the first request where the DataSource and operation type match the parameter values (null parameters match any DataSource / operation type). For example:DSResponse resp = dsTransaction.findFirstResponse("customer", "update"); would return the response to the first update request on the "customer" DataSource.
This method is just one way to access the response to a request earlier in the queue - it is a programmatic equivalent of using
$responseData.first()
in a Velocity expression. Scan the client-side documentation for "transaction chaining" for a full discussion of the various options available when using the Transaction Chaining approach.
dsName
- The name of the DataSource to find a response for (null means any DataSource)opType
- The operation type to find a response for (null means any operation type)public void addToTemplateContext(java.lang.Object key, java.lang.Object value)
three-argument version
, but the third argument (isSnippet) is always false.key
- the attribute key.value
- the attribute value, this can be a fully nested Java object.public void addToTemplateContext(java.lang.String name, java.lang.Object value, boolean isSnippet)
The Note, context variables added via this method will be overridden by context variables of the same name added via the com.isomorphic.datasource.DSRequest#addToTemplateContext(java.lang.Object, java.lang.Object, boolean) DSRequest.addToTemplateContext() method}isSnippet
parameter controls whether the framework treats the context object as a regular context variable, or a user-defined snippet of text. If
name
- the name of the new object as it should appear in the Velocity namespacevalue
- arbitrary Java objectisSnippet
- Is the context object a user-defined snippet?public DataSource getDataSource(java.lang.String dsName) throws java.lang.Exception
DSRequest.getDataSource()
instead. Also, if you are trying to access some arbitrary DataSource purely because you need to run a DSRequest on it (another very common use case), consider just creating the DSRequest instead, using one of the constructors that accepts a DataSource name. If you still end up using this method you must make sure you call DSTransaction.freeDataSources()
when you are finished, otherwise the DataSources will still be lingering around and become unavailable for anything else that might need them inevitably causing a memory leak.dsName
- The name of the DataSource to returnjava.lang.Exception
DSRequest.getDataSource()
, DSRequest(java.util.Map, com.isomorphic.servlet.RequestContext, com.isomorphic.rpc.RPCManager)
public void setUserId(java.lang.String userId)
HttpServletRequest.getRemoteUser()
. Calling this API automatically sets the authentication status of the RPCManager. If you pass a non-null value, the authentication status is set to true. If you pass null, the authentication status is also set to null.
userId
- The user ID to associate with the queue of requests being managed by this RPCManagerRPCManager.getUserId()
, RPCManager.setAuthenticated(boolean)
public java.lang.String getUserId()
RPCManager.setUserId(String)
; if that method has not been called and we are running in the context of the servlet API, we call the servlet API's getRemoteUser()
method.RPCManager.setUserId(String)
public java.lang.Boolean getAuthenticated()
RPCManager.setAuthenticated(boolean)
public void setAuthenticated(boolean authenticated)
authenticated
- if true we have an authenticated userRPCManager.getAuthenticated()
, RPCManager.setUserRoles(String)
, RPCManager.setUserRoles(java.util.List)
public java.util.List getUserRoles()
RPCManager.getAuthenticated()
, RPCManager.setUserRoles(java.util.List)
, RPCManager.setUserRoles(String)
public void setUserRoles(java.lang.String rolesString)
rolesString
- A comma-separated String representing the list of roles associated with the authenticated userRPCManager.setAuthenticated(boolean)
, RPCManager.getUserRoles()
public void setUserRoles(java.lang.String[] roles)
This method allows you to implement a role-based security system other than the one built in to the servlet API. Please see the client-side documentation for OperationBinding.requiresRole for details of how the authentication/security system works
roles
- The list of roles associated with the authenticated userRPCManager.setAuthenticated(boolean)
, RPCManager.getUserRoles()
public void setUserRoles(java.util.List rolesList)
This method allows you to implement a role-based security system other than the one built in to the servlet API. Please see the client-side documentation for OperationBinding.requiresRole for details of how the authentication/security system works
rolesList
- The list of roles associated with the authenticated userRPCManager.setAuthenticated(boolean)
, RPCManager.getUserRoles()
public java.lang.Boolean isClientRequest()
public void setClientRequest(java.lang.Boolean clientRequest)
clientRequest
- true if this is a clientRequest transaction.BaseRequest.setClientRequest(Boolean)