Defining Transaction objects for multiple database connections
Use one Transaction object per connection
To perform operations in multiple databases at the same time,
you need to use multiple Transaction objects, one for each database
connection. You must declare and create the additional Transaction
objects before referencing them, and you must destroy these Transaction
objects when they are no longer needed.
PowerBuilder creates and destroys SQLCA automatically.
Do not attempt to create or destroy it.
Creating the nondefault Transaction object
To create a Transaction object other than SQLCA, you first declare a variable of
type transaction:
1 |
transaction <span>TransactionObjectName</span> |
You then instantiate the object:
1 |
<span>TransactionObjectName</span> = CREATE transaction |
For example, to create a Transaction object named DBTrans,
code:
1 |
transaction DBTrans |
1 |
DBTrans = CREATE transaction |
1 |
// You can now assign property values to DBTrans. |
1 |
DBTrans.DBMS = "ODBC" |
1 |
... |
Assigning property values
When you assign values to properties of a Transaction object
that you declare and create in a PowerBuilder script, you must assign
the values one property at a time, like this:
1 |
// This code produces correct results. |
1 |
transaction ASETrans |
1 |
ASETrans = CREATE TRANSACTION |
1 |
ASETrans.DBMS = "SYC" |
1 |
ASETrans.Database = "Personnel" |
You cannot assign values by setting the
nondefault Transaction object equal to SQLCA,
like this:
1 |
// This code produces incorrect results. |
1 |
transaction ASETrans |
1 |
ASETrans = CREATE TRANSACTION |
1 |
ASETrans = SQLCA <span>// ERROR!</span> |
Specifying the Transaction object in SQL statements
When a database statement requires a Transaction object, PowerBuilder assumes
the Transaction object is SQLCA unless
you specify otherwise. These CONNECT statements
are equivalent:
1 |
CONNECT; |
1 |
CONNECT USING SQLCA; |
However, when you use a Transaction object other than SQLCA, you must specify
the Transaction object in the SQL statements
in Table 12-3 with
the USING TransactionObject clause.
COMMIT |
INSERT |
CONNECT |
PREPARE (dynamic SQL) |
DELETE |
ROLLBACK |
DECLARE |
SELECT |
DECLARE |
SELECTBLOB |
DISCONNECT |
UPDATEBLOB |
EXECUTE (dynamic SQL) |
UPDATE |
To specify a user-defined Transaction object in SQL statements:
-
Add the following clause to the end of
any of the SQL statements in
the preceding list:1USING <span>TransactionObject</span>For example, this statement uses a Transaction object named ASETrans to connect
to the database:1CONNECT USING ASETrans;
Although specifying the USING TransactionObject clause
in SQL statements is optional
when you use SQLCA and required
when you define your own Transaction object, it is good practice
to code it for any Transaction object, including SQLCA. This avoids confusion
and ensures that you supply USING TransactionObject when
it is required.
Example
The following statements use the default Transaction object
(SQLCA) to communicate with
a SQL Anywhere database and a nondefault Transaction object named ASETrans to
communicate with an Adaptive Server Enterprise database:
1 |
// Set the default Transaction object properties. |
1 |
SQLCA.DBMS = "ODBC" |
1 |
SQLCA.DBParm = "ConnectString='DSN=Sample'" |
1 |
// Connect to the SQL Anywhere database. |
1 |
CONNECT USING SQLCA; |
1 |
// Declare the ASE Transaction object. |
1 |
transaction ASETrans |
1 |
// Create the ASE Transaction object. |
1 |
ASETrans = CREATE TRANSACTION |
1 |
// Set the ASE Transaction object properties. |
1 |
ASETrans.DBMS = "SYC" |
1 |
ASETrans.Database = "Personnel" |
1 |
ASETrans.LogID = "JPL" |
1 |
ASETrans.LogPass = "JPLPASS" |
1 |
ASETrans.ServerName = "SERVER2" |
1 |
1 |
// Connect to the ASE database. |
1 |
CONNECT USING ASETrans; |
1 |
1 |
// Insert a row into the SQL Anywhere database |
1 |
INSERT INTO CUSTOMER |
1 |
VALUES ( 'CUST789', 'BOSTON' ) |
1 |
USING SQLCA; |
1 |
// Insert a row into the ASE database. |
1 |
INSERT INTO EMPLOYEE |
1 |
VALUES ( "Peter Smith", "New York" ) |
1 |
USING ASETrans; |
1 |
1 |
// Disconnect from the SQL Anywhere database |
1 |
DISCONNECT USING SQLCA; |
1 |
// Disconnect from the ASE database. |
1 |
DISCONNECT USING ASETrans; |
1 |
// Destroy the ASE Transaction object. |
1 |
DESTROY ASETrans |
An actual script would include error checking after the CONNECT, INSERT, and DISCONNECT statements.
For details, see “Error handling after
a SQL statement”.