This section focuses on the other facilities available for managing transactions. It includes information on using the VisiTransact Transaction Service interfaces—TransactionFactory, Control, Coordinator, and Terminator.Although typically you will use the Current interface to manage transactions, there are several other approaches to transaction management you can use:
{
interface TransactionFactory
{
Control create(in unsigned long time_out);
Control recreate(in PropagationContext ctx);
};
};VisiTransact also supplies an extension to the TransactionFactory interface that allows a transaction to be created using a specific name—create_with_name(). Naming a transaction is useful for tracking the progress of a particular transaction, as well as debugging its execution.module VISTransactions
{
// TransactionFactory
// This extends the CosTransactions::TransactionFactory by
// allowing someone to create a transaction with a user-defined
// name that can be used for debugging, error reporting, etc.
interface TransactionFactory : CosTransactions::TransactionFactory
{
CosTransactions::Control create_with_name(in unsigned long time_out,
in string userTransactionName);
};
};The following table defines the methods for creating transactions with TransactionFactory.
CosTransactions::TransactionFactory_var txnFactory;
CosTransactions::Control_var control;
control = txnFactory->create_with_name
(0,"BankTransfer#1");
//use default
//timeout value
...The PropagationContext can be obtained from an existing transaction using the CosTransactions:Coordinator::get_txcontext() method described in “Obtaining transaction information”.The Control interface allows an application to obtain the Terminator and Coordinator object references in order to explicitly manage or propagate a transaction context. An object supporting the Control interface is associated with one specific transaction.The following example shows the Control interface.module CosTransactions
{
interface Control
{
Terminator get_terminator()
raises(Unavailable);
Coordinator get_coordinator()
raises(Unavailable);
};
};The table below defines the methods for the Control interface.
CosTransactions::Control_var control
CosTransactions::Terminator_var newTranTerminator;
CosTransactions::Coordinator_var newTranCoordinator;
newTranTerminator = control->get_terminator();
newTranCoordinator = control->get_coordinator();
...With transactions originated using the TransactionFactory, the transaction originator handles transactions using several VisiTransact Transaction Service interfaces. Through these interfaces, more than one transaction may be managed at a time by the transaction originator.With transactions originated using the TransactionFactory, you can use implicit propagation. See “Changing from explicit propagation to implicit”.
CosTransactions::Control_var control;
CORBA:Boolean didSucceed;
didSucceed=bank->withdraw(10, 444, control) // invoke a CORBA request
...You may want to start a transaction with explicit propagation and then switch to implicit. To set up your implicit transaction context, pass the Control object into Current::resume(). See “Using multiple transactions within a context or thread” for details on using Current::resume() and Current::suspend().If you start a transaction with implicit propagation and later want to get the transaction context explicitly, use Current::get_control().The Terminator interface supports operations to commit or rollback a transaction. Typically, these operations are used by the transaction originator. The following example shows the Terminator interface.module CosTransactions
{
interface Terminator
{
void commit(in boolean report_heuristics)
raises (HeuristicMixed, HeuristicHazard);
void rollback();
};
};The following table defines the methods provided by the Terminator interface.
CORBA::Boolean didSucceed;
...
CosTransactions::Terminator_var
txnTerminator=control->get_terminator();
if(didSucceed)
{ // invoke a CORBA request
try
{
txnTerminator->commit(1);
}
catch(CORBA::TRANSACTION_ROLLEDBACK&)
{
// Return failure.
}
}
else
{
txnTerminator->rollback();
}
...See “Heuristic completion” for details about heuristic completion when committing a transaction.If the participant does not want the transaction to commit, it can use the rollback_only() method from the Coordinator interface. When the rollback_only() method is called by a participant, the transaction associated with the current thread is modified so that the only possible outcome is to rollback the transaction. The CosTransactions::Inactive exception is raised if the transaction has already been prepared. The example below shows how a participant would use the rollback_only() method....
CosTransactions::Coordinator_var coord;
coord->rollback_only();
...A participant can obtain information about a transaction such as the transaction name or transaction status, or obtain the transaction context for a transaction using methods in the Coordinator interface. The following table describes these methods.