Microsoft SQL Server 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 CONNECT
statements is desirable.
In addition, after a connection is established, SQL statements
can cause locks to be placed on database entities. The more locks
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.
Isolation feature
SQL Server 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 a SQL Server database.
The following example shows how to set the Lock property to
RU (Read uncommitted):
1 |
// Set the lock property to read uncommitted<br>// in the default transaction object SQLCA.<br>SQLCA.Lock = "RU" |
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 either a COMMIT or a ROLLBACK.
1 |
// Set the SQLCA connection properties.<br>SQLCA.DBMS = "SQLServer"<br>SQLCA.servername = "SERVER24"<br>SQLCA.database = "ORDERS"<br>SQLCA.logid = "JPL"<br>SQLCA.logpass = "TREESTUMP" |
1 |
// Connect to the database.<br>CONNECT USING SQLCA; |
1 |
// Insert a row into the ORDER_HEADER table.<br>// A ROLLBACK is required only if the first row<br>// was inserted successfully.<br>INSERT INTO ORDER_HEADER (ORDER_ID,CUSTOMER_ID)<br> VALUES ( 7891, 129 ); |
1 |
// Test return code for ORDER_HEADER insertion.<br>if SQLCA.sqlcode = 0 then |
1 |
// Since the ORDER_HEADER is inserted,<br>// try to insert ORDER_ITEM.<br>INSERT INTO ORDER_ITEM(ORDER_ID, ITEM_NBR,<br> PART_NBR, QTY)<br> VALUES ( 7891, 1, '991PLS', 456 ); |
1 |
// Test return code for ORDER_ITEM insertion.<br> if SQLCA.sqlcode = -1 then |
1 |
// If insert failed.<br>// ROLLBACK insertion of ORDER_HEADER.<br> ROLLBACK USING SQLCA;<br> end if<br>end if |
1 |
// Commit changes and disconnect from the database.<br>DISCONNECT USING SQLCA; |

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
that the window contains a DataWindow control dw_1 and
that 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:
1 |
// Set the transaction object properties<br>// and connect to the database.<br>// Set the SQLCA connection properties.<br>SQLCA.DBMS = "SQLServer"<br>SQLCA.servername = "SERVER24"<br>SQLCA.database = "ORDERS"<br>SQLCA.logid = "JPL"<br>SQLCA.logpass = "TREESTUMP" |
1 |
// Connect to the database.<br>CONNECT USING SQLCA; |
1 |
// Tell the DataWindow which transaction object<br>// to use.<br>SetTransObject( dw_1, SQLCA ) |
The CommandButton CLICKED event script:
1 |
// Declare ReturnValue an integer.<br>integer ReturnValue<br>ReturnValue = Update( dw_1 ) |
1 |
// Test to see if updates were successful.<br>if ReturnValue = -1 then |
1 |
// Updates were not successful. Since we used<br>// SetTransObject, rollback any changes made<br>// to the database.<br> ROLLBACK USING SQLCA;<br>else |
1 |
// Updates were successful. Since we used<br>// SetTransObject, commit any changes made<br>// to the database.<br> COMMIT USING SQLCA;<br>end if |
The window CLOSE event script:
1 |
// Disconnect from the database.<br>DISCONNECT USING SQLCA; |