Transaction Object
We know that transaction objects play a very important role in the power BUILDER, and the power BUILDER applications must be connected to various databases through transaction objects, so before we discuss the relationship between data windows and databases, we first introduce the basics of objects.
In power builder, to operate on a database, you must first establish a connection to the database. In a Power builder database connection, a transaction object is a special, Non-visual object that provides a communication area for communication between the Power Builder program and the database. The transaction object defines the parameters that the Power Builder uses to connect to the database. Before accessing the database, you must establish a transaction object and then assign the appropriate value to the property of the transaction object to connect to the database through this transaction object to complete the required database operations. When an application starts running, the power BUILDER automatically creates a default global transaction object named Sqlca. SQLCA is the abbreviation for SQL COMMUNICATIONS area SQL Communications. You can use this default transaction object in your application, or you can define your own transaction object.
Power Builder provides four statements for transaction management
Connect: A successful Connect statement marks the beginning of a transaction. Before executing the Connect statement, you must assign an appropriate value to an existing object's property that is used to connect to the DBMS.
DISCONNECT: This statement marks the abort of a transaction.
Commit: A commit commits any uncommitted update operations to the database that the current transaction has made since the start of the transaction. And start a new transaction.
ROLLBACK: After the ROLLBACK statement is executed, all updates that are made by the current transaction are undone and a new transaction is started.
Working with Transaction objects
There are two ways to set a transaction object for a data window.
Using the Settrans function: The Settrans function copies a particular transaction object to the internal transaction object of the Data window control. When the program uses the function, the Data window control uses its own internal object objects and automatically performs a connection and disconnect operation if an error occurs, automatically undoes all updates that have been made, and reverts to the state at the start of the transaction.
When the data window to access the database, if the implementation of a retrieve command or UPDATE command, the Data window will issue an internal connect command, processing the corresponding data access. After the data processing is finished, an internal disconnect command is issued and the connection to the database is disconnected. So if you use the Settrans function in your program, you don't need to write the Connect and disconnect commands separately.
From the above, we can see the flaw of the Settrans statement, that is, each time the database access, the database to establish a new connection, and this connection will occupy a considerable portion of the system resources. And in the database access, the user can not control the access process. If you use the Settrans statement, the efficiency of the program will be affected when the number of database connections increases.
Use the Settransobject command: Use this command to specify a transaction object for a data window. We should write scripts in the following order to access the database.
CONNECT
SETTRANSOBJECT
RETRIEVE或UPDATE
COMMIT或ROLLBACK
DISCONNECT
Here we see two new instructions: Retrieve and update.
The function of the retrieve instruction is to retrieve the contents of the database into the data window after using the Settransobject statement to connect a data window to a transactional object. Its invocation format is Datawindow.retrieve ()
If the data source to be accessed is generated using an SQL SELECT statement, the parameter value must be given when calling the RETRIEVE function, and the calling format is as follows: Datawindow.retrieve (ARG1,ARG2, ...) )
The order of the parameters in the retrieve function is consistent with the order of the SQL select parameters defined in the Data Window object. The Power Builder allows the retrieve function to have arguments with more than the number of original parameters defined in SQL Select for the Data Window object, and the extra arguments are ignored. This feature can be used to design a universal search that can manipulate different data window objects.
The function of the update instruction is to use the update function to write the changes back to the database after you have modified the data in the Data window.