Oracle Performance and locking

An important consideration when designing a database application is deciding when CONNECT and COMMIT statements should occur to maximize performance and limit locking and resource use. A CONNECT takes a certain amount of time and can tie up resources during the life of the connection. If this time is significant, then limiting the number of CONNECTs is desirable.

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:

Example 1

This script uses embedded SQL to connect to a database and insert a row in the ORDER_HEADER table and a row in the ORDER_ITEM table. Depending on the success of the statements in the script, the script executes a COMMIT or ROLLBACK.

// Set the SQLCA connection properties.
SQLCA.DBMS = "O73"
SQLCA.servername = "@TNS:SHOPFLR"
SQLCA.logid = "JPL"
SQLCA.logpass = "STUMP"
// Connect to the database.
CONNECT USING SQLCA;
// Insert a row into the ORDER_HEADER table.
// A ROLLBACK is required only if the first row
// was inserted successfully.
INSERT INTO ORDER_HEADER (ORDER_ID, CUSTOMER_ID)
	VALUES ( 7891, 129 );
// Test return code for ORDER_HEADER insertion.
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
// The insert failed.
// Roll back insertion of ORDER_HEADER.
		ROLLBACK USING SQLCA;
	End If
End If
COMMIT USING SQLCA;
// Disconnect from the database.
DISCONNECT USING SQLCA;

NoteError 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 the scripts for the Open and Close events in a window and the Clicked event in a CommandButton to illustrate how you can manage transactions in a DataWindow control. Assume the window contains a DataWindow control dw_1 and the user enters data in dw_1 and then clicks the Cb_Update button to send the data to the database.

Since this script uses SetTransObject to connect to the database, the programmer is responsible for managing the transaction.

The window Open event script:

// Set the transaction object properties
// and connect to the database.
// Set the SQLCA connection properties.
SQLCA.DBMS = "O73"
SQLCA.servername = "@TNS:SHOPFLR"
SQLCA.logid = "JPL"
SQLCA.logpass = "STUMP"
// 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
// Update dw_1.
ReturnValue = dw_1.Update( )
// Test to see whether the updates were successful.
If ReturnValue = -1 then
// The updates were not successful.
// Roll back any changes made to the database.
	ROLLBACK USING SQLCA;
Else
// The updates were successful.
// Commit any changes made to the database.
	COMMIT USING SQLCA;
End If

The window Close event script:

// Since we used SetTransObject,
// disconnect from the database.
DISCONNECT USING SQLCA;