After a connection is established, SQL statements can cause locks to be placed on database entities. The more locks there are in place at a given moment in time, the more likely it is that the locks will hold up another transaction.
Rules
No set of rules for designing a database application is totally comprehensive. However, when you design a PowerBuilder application, you should do the following:
-
Long-running connections
Determine whether you can afford to have long-running connections. If not, your application should connect to the database only when absolutely necessary. After all the work for that connection is complete, the transaction should be disconnected.
If long-running connections are acceptable, then COMMITs should be issued as often as possible to guarantee that all changes do in fact occur. More importantly, COMMITs should be issued to release any locks that may have been placed on database entities as a result of the statements executed using the connection.
-
SetTrans or SetTransObject function
Determine whether you want to use default DataWindow transaction processing (the SetTrans function) or control the transaction in a script (the SetTransObject function).
If you cannot afford to have long-running connections and therefore have many short-lived transactions, use the default DataWindow transaction processing. If you want to keep connections open and issue periodic COMMITs, use the SetTransObject function and control the transaction yourself.
Switching during a connection
To switch between transaction processing and AutoCommit during a connection, change the setting of AutoCommit in the transaction object.
Isolation feature
ODBC uses the isolation feature to support assorted database lock options. In PowerBuilder, you can use the Lock property of the transaction object to set the isolation level when you connect to the database.
The following example shows how to set the Lock property to RU (Read uncommitted):
// Set the lock property to read uncommitted // in the default transaction object SQLCA. SQLCA.Lock = "RU"
PowerBuilder uses the ODBC API call SQ2.SetConnectOption (SetIsolationLevel) to set the isolation level. The lock value is passed to the function as a 32-bit mask.
Example 1
This script uses embedded SQL to connect to a database and attempts to insert a row in the ORDER_HEADER table and a row in the ORDER_ITEM table. The script then executes a COMMIT or ROLLBACK depending on the success of all statements in the script.
// Set the SQLCA connection properties. SQLCA.DBMS = "ODBC" SQLCA.DBParm = "connectstring = 'DSN = orders'" // Connect to the database. CONNECT USING SQLCA; // Insert a row into the ORDER_HEADER table. INSERT INTO ORDER_HEADER (ORDER_ID,CUSTOMER_ID) VALUES (7891, 129); // Test return code for ORDER_HEADER insertion. // A ROLLBACK is required only if the first row // was inserted successfully. if SQLCA.sqlcode = 0 then // Since the ORDER_HEADER is inserted, // try to insert ORDER_ITEM. INSERT INTO ORDER_ITEM (ORDER_ID, ITEM_NBR, PART_NBR, QTY) VALUES (7891, 1, '991PLS', 456); // Test return code for ORDER_ITEM insertion. if SQLCA.sqlcode = -1 then // Disconnect from the database. DISCONNECT USING SQLCA;
Error checking
Although you should test the SQLCode after every SQL statement, these examples show statements to test the SQLCode only to illustrate a specific point.
Example 2
This example uses scripts for the Open and Close events for a window and the Clicked event for a CommandButton to illustrate how you can manage transactions for a DataWindow control. Assume a window contains a DataWindow control dw_1 and a CommandButton Cb_Update. Also assume the user enters data in dw_1 and then clicks the Cb_Update button to update the database with the data.
The window OPEN event script:
// Set the transaction object properties // and connect to the database. // Set the SQLCA connection properties. SQLCA.DBMS = "ODBC" SQLCA.DBParm = "connectstring = 'DSN = orders'" // Connect to the database. CONNECT USING SQLCA; // Tell the DataWindow which transaction object to use. dw_1.SetTransObject(sqlca)
The CommandButton CLICKED event script:
// Declare ReturnValue an integer. integer ReturnValue ReturnValue = dw_1.Update( ) // Test to see if updates were successful. if ReturnValue = -1 then // Updates were not successful. Since we used // SetTransObject, roll back any changes made // to the database. ROLLBACK USING SQLCA; else // Updates were successful. Since we used // SetTransObject, commit any changes made // to the database. COMMIT USING SQLCA; end if
The window CLOSE event script:
// Disconnect from the database. DISCONNECT USING SQLCA;