Providing support for transactions
If a component supports transactions, MTS ensures that the
component’s database operations execute as part of a transaction.
Using the transaction service context object
PowerBuilder components running in MTS can use a transaction
context service to control transactions. A PowerBuilder COM object
creates an MTS context object that enables the component to take
advantage of Microsoft DTC support. The TransactionServer object
enables a COM object running in MTS to access the context associated
with the object, giving it tighter control of transactions and activation.
It also provides some control of the security context.
For more information about the TransactionServer object and
its methods, see Objects and Controls
and the PowerScript
Reference
.
Before you can use the transaction context service, you need
to declare a variable of type TransactionServer and call the GetContextService
function to create an instance of the service:
1 |
TransactionServer txninfo_base |
1 |
this.GetContextService("TransactionServer", & |
1 |
txninfo_base) |
To access information about the component’s transaction
context and control the transaction, call methods on the instance
of the TransactionServer object–for example:
1 |
IF txninfo_base.IsInTransaction() THEN |
1 |
txninfo_base.DisableCommit() |
1 |
END IF |
1 |
... |
1 |
txninfo_base.SetComplete() |
When you no longer need the reference to the service, you
should destroy it:
1 |
DESTROY txninfo_base |
Behavior of COMMIT and ROLLBACK
When a PowerBuilder component is running in MTS, the TransactionServer interface
is used to control transactions when the UseContextObject DBParm parameter
is set to Yes. When the UseContextObject DBParm
parameter is set to Yes, COMMIT and ROLLBACK statements are ignored.
The transaction remains active until SetComplete or SetAbort is
issued via an instance of the TransactionServer context object.
Migrating PowerBuilder 6 components Components built with PowerBuilder 6 relied on the PowerBuilder database driver
to issue SetComplete or SetAbort calls to MTS. If you are migrating these
components to the current version of PowerBuilder, you must modify your
code to use the TransactionServer interface. Alternatively, you
can set the UseContextObject DBParm to No. In this case, COMMIT
is equivalent to SetComplete in MTS and ROLLBACK is equivalent to
SetAbort. This approach is recommended only when you want to migrate PowerBuilder 6 objects
to MTS without modifying the code.
Specifying whether a component
supports transactions
Each MTS component has a transaction property that indicates
how the component participates in MTS transactions.PowerBuilder COM
objects create a new MTS context object when the component transaction
property in MTS is set to Requires a new transaction
.
A PowerBuilder COM object whose component transaction property is
set to either Requires a transaction
or Supports
transactions
will either inherit a transaction from an
existing object or create a new transaction.
You set this property in the COM/MTS Project wizard
or the Project painter:
Transaction type | Description |
---|---|
Not supported | The component never executes as part of a transaction. If the component is activated by another component that is executing within a transaction, the new instance’s work is performed outside the existing transaction |
Supports Transaction | The component can execute in the context of an MTS transaction, but a connection is not required to execute the component’s methods. If the component is instantiated directly by a client, MTS does not begin a transaction. If component A is instantiated by component B and component B is executing within a transaction, component A executes in the same transaction |
Requires Transaction | The component always executes in a transaction. When the component is instantiated directly by a client, a new transaction begins. If component A is activated by component B and B is executing within a transaction, A executes within the same transaction; if B is not executing in a transaction, A executes in a new transaction |
Requires New Transaction | Whenever the component is instantiated, a new transaction begins. If component A is activated by component and B is executing within a transaction, then A begins a new transaction that is unaffected by the outcome of B’s transaction; if B is not executing in a transaction, A executes in a new transaction |
For more information about how MTS transactions work, see
the MTS Programmer’s Guide.