With VisiBroker for .NET-managed transactions you are using the Current interface for all transaction management. You are beginning transactions using
Current and you are using
Current for the implicit transaction propagation. This means that you will always originate your transactions using
Current.Begin().
Current is an object that is valid for the entire process and manages the association of each thread's transaction context. Each thread has its own independent, isolated association with a transaction context.
To gain access to a VisiBroker for .NET-managed transaction, you must obtain an object reference to the Current object. The
Current object reference is valid throughout the process. The following steps describe the general process for obtaining a reference to a
Current object, and are include code examples.
1
|
Call the orb.ResolveInitialReferences() method. This method obtains a reference to the Current object.
|
When you narrow to CosTransactions.Current, you specify your use of the original set of methods provided by the
CosTransactions module.
If this transaction has been marked for rollback, or any Resource votes for Rollback, this call raises
CORBA.TRANSACTION_ROLLEDBACK. If there is no current transaction, a
CosTransactions.NoTransaction exception is raised. If the caller is not the transaction originator,
Commit raises the exception
CORBA.NO_PERMISSION.
On return from this method, the client thread is no longer associated with a transaction. Any attempt to use Current, as if there were a transaction, will raise an exception, such as
NoTransaction or
CORBA.TRANSACTION_REQUIRED, or will return a null object reference.
This method returns a Control object reference that represents the transaction context currently associated with the client thread.
This method returns an enumerated value (enum Status) that represents the status of the transaction associated with the client thread.
Calling this method is equivalent to calling the GetStatus method on the corresponding
Coordinator object. If there is no transaction associated with the current thread, then the method returns
CosTransactions.StatusNoTransaction.
The effect of this method is equivalent to calling the GetTransactionName method on the corresponding
Coordinator object. If there is no transaction associated with the client thread, an empty string is returned.
Any transaction context you set via Resume is propagated back to the invoking object.
Rolls back the transaction associated with the client thread. This is equivalent to calling the Rollback method on the corresponding
Terminator object. This method does not return until the transaction is complete, and all related
Synchronization objects have been notified. On return from this method, the client thread is no longer associated with a transaction. Any attempt to use
Current, as if there were a transaction, will raise an exception, such as
CosTransactions.NoTransaction or
CORBA.TRANSACTION_REQUIRED, or return a null object reference. If a heuristic occurs, this method will not throw a heuristic-related exception.
If the caller is not the transaction originator, Rollback raises the exception
CORBA.NO_PERMISSION.
When a transaction, created by a subsequent call to Begin in any thread in the process, takes longer to start transaction completion than the established time-out, it will be rolled back. If the time-out occurs before the transaction enters the completion stage (begins two-phase or one-phase processing) the transaction will be rolled back. Otherwise, the time-out is ignored.
This method suspends the transaction currently associated with the client thread and returns a Control object for that transaction. If the client thread is not associated with a transaction, a null object reference is returned.
The Control object can be passed to the
Resume method to reestablish this context in the same thread or a different thread.
After the call to Suspend, no transaction is associated with the client thread. Any attempt to use
Current, as if there were a transaction, will raise an exception, such as
CosTransactions.NoTransaction or
CORBA.TRANSACTION_REQUIRED, or return a null object reference.
This method accepts a time-out parameter (time_out) and creates a new transaction. It returns a Control object. The Control object can be used to manage or to control participation in the new transaction. The Control object can be used by any thread and passed around explicitly, just like any other CORBA object.
To get a transaction's PropagationContext, invoke the CosTransactions.CoordinatorOperations.GetTxcontext method on the transaction's Coordinator object.
The Control interface defines two methods: GetCoordinator and
GetTerminator. The
GetCoordinator method returns a Coordinator object, which supports methods used by participants in the transaction. The
GetTerminator method returns a Terminator object, which supports methods to complete the transaction. The Terminator and Coordinator objects support methods that are typically performed by different parties. Providing two objects enables each set of methods to be made available only to the parties that require those methods.
You can obtain a Control object by using one of the methods of the TransactionFactory (see “TransactionFactory interface”). You can also obtain a Control object for the current transaction (associated with a thread) through methods of the Current object. See descriptions of the
GetControl or
Suspend methods in
“Current interface”.
This method returns a Terminator object. The Terminator can be used to rollback or commit the transaction associated with the Control. The CosTransactions.Unavailable exception is raised if the Control cannot provide the requested object due to the inability of the Terminator object to be transmitted to or be used in other execution environments.
If the report_heuristics parameter is true, the Transaction Service will report inconsistent or possibly inconsistent outcomes using the
CosTransactions.HeuristicMixed and
CosTransactions.HeuristicHazard exceptions when appropriate. Information about the Resources involved in a heuristic outcome will be written to a heuristic log file corresponding to the instance of the Transaction Service.
Similarly, certain methods return TRUE only when the target object and the parameter refer to the same Coordinator object. Therefore, the following methods are also equivalent:
This method returns the status of the transaction associated with the target object, as an enumerated value (enum Status). If there is no transaction associated with the target object, then the method returns the value
StatusNoTransaction.
Because VisiBroker for .NET does not support nested transactions, the GetStatus,
GetTopLevelStatus and
GetParentStatus methods return the same result.
The GetTxcontext method returns a PropagationContext, which can be used by one Transaction Service domain to export a transaction to a new Transaction Service domain.
This method attempts to commit all changes associated with the Resource. If a heuristic outcome exception is raised, the Resource must keep the heuristic decision in persistent storage until the Forget method is performed so that it can return the same outcome in case
Commit is invoked again during recovery. Otherwise, the Resource can immediately forget all knowledge of the transaction.
If a heuristic outcome exception is raised, the Resource must keep the heuristic decision in persistent storage until the Forget method is performed. This enables the Resource to return the same outcome in case
CommitOnePhase is performed again during recovery. Otherwise, the Resource immediately forgets all knowledge of the transaction.
If a failure occurs during CommitOnePhase, it is called again when the failure is repaired. Since there is only a single Resource, the
HeuristicHazard exception is used to report heuristic decisions related to that Resource.
When VisiBroker for .NET receives a heuristic exception, it records the exception. The Transaction Service will ultimately call Forget on the Resource. This means that the Resource can discard all information about the transaction that raised the heuristic exception. This method is called only if a heuristic exception was raised from
Rollback,
Commit, or
CommitOnePhase.
If a heuristic outcome exception is raised, the Resource must save the heuristic decision in persistent storage until the Forget method is invoked. This enables the Resource to return the same outcome in case
Rollback is called again during recovery. Otherwise, the Resource immediately forgets all knowledge of the transaction.
•
|
BeforeCompletion—Ensures that BeforeCompletion is invoked before starting to commit a transaction.
|
•
|
AfterCompletion—Ensures a transactional object is notified after the transaction has been completed. This applies to all transactions whether they were committed or rolled back.
|
•
|
In certain cases, AfterCompletion is called when BeforeCompletion was not called. BeforeCompletion is called only if a transaction is still continuing towards a commit at the outset of completion. AfterCompletion is always called (unless the Transaction Service crashes before the transaction completes).
|
The AfterCompletion method is always invoked during normal processing.
If AfterCompletion is to be called in processing a particular transaction, the following actions must be taken:
The Transaction Service calls this method after the two-phase commit protocol completes. As an example of its use, AfterCompletion can be used by a transactional object to discover the outcome of the transaction. This is particularly useful for transactional objects that are not also recoverable objects, and so are not automatically notified of the outcome.
You can call GetStatus to see whether or not the transaction has been marked for rollback.
Note: The BeforeCompletion method is invoked after the application invokes commit, but before the Transaction Service begins transaction completion. The
BeforeCompletion method is not invoked for a rollback request.
If BeforeCompletion is to be called when processing a particular transaction, the Synchronization object must be registered using the
RegisterSynchronization method in the Coordinator interface. Register the Synchronization object from your transactional object or recoverable server. See the description for the
RegisterSynchronization method in
“Coordinator interface”. Registration must be done after the transaction is created and before the start of the two-phase commit.
The Transaction Service calls this method after the transaction work has been done but before the two-phase commit protocol starts; that is, before Prepare is called on the participating Resource. The transaction service calls
BeforeCompletion only if a transaction is still continuing towards a commit at the outset of completion. This means that
Terminator.Commit was called and the transaction has not been marked for rollback. If
Terminator.Rollback was called, or the first of several Synchronization objects marked the transaction for rollback, or the transaction was already marked for rollback,
BeforeCompletion calls will not be called again for this transaction.
Within this method, you can ensure the transaction will be rolled back by calling the RollbackOnly method. You can also call
GetStatus to see whether or not the transaction has been marked for rollback. At the time the method is called, however, you cannot rely upon the status to indicate whether or not the transaction will actually be committed.
The transaction context is always passed implicitly to an object that inherits from CosTransactions.TransactionalObject. In addition, a program may be passed a transaction context explicitly, as a parameter.