Data update technology with cache in Delphi TYPE: Database class Time: 2003-4-10author: None URL: http://www.91delphi.comhit: 412 DayHit: 2 WeekHit: 57 Delphi with cache data update technology 1. Concept In a network environment, the database application is a mode of C / S or a multi-layer structure. In this environment, the development of database applications should consider reducing network data transmission and as much as possible. Based on this purpose, the cached data update technology came into being, and its approach process is: the application extracts data into the client's buffer, completing the data modification, update, and new data inserts in the buffer. After the operation is completed, in a suitable time, the one-time will submit data to the database, thereby greatly reduces network traffic, reducing the load of the database server, and increasing concurrency performance. It should be said that this is not too new technique. In the early versions of the Delphi, there are earlier versions, this technology has been supported. However, the author found that some programmers did not pay attention to the reasonable application of this technology, and still stayed under the idea of stand-alone applications, resulting in low programs or potential errors. It is therefore necessary to make a summary of the advantages, principles of the technology, and the use of methods (with Delphi as an example). Two. The data update technology of advantages and shortcomings with cache mainly has the following advantages: (1) Maximum reduction of network traffic, reducing data access time. Improve the efficiency of client database operators. (2) Reduce the burden of the database server, because many repeated updates, modifications, and delete operations can be completed in the client's buffer, and finally submit the results to the server. (3) Time to effectively reduce the processing of transactions, reducing the throughput of concurrent transactions. Thereby also ensuring the consistency of the database. We can give an example to explain its superiority: The database operator inserts a data record into the database, but immediately discovers that the record is not required, so it is deleted. This process, if the server is used without buffered data update technology, the server side will perform an insertion operation, a delete operation, a transcend with a climate and a server side, and if this data is used with other database tables Classified relationships must also take into account the level of data of the data level update, deletion, recovery, etc. If you use a cache data update method, you can complete these two-step inverse operations on the client's data buffer, without bringing any action to the server side, and does not generate any network data transfer and cascade update. operating. This can be seen that the huge advantage of buffered data update technology. A disabled data update has a disadvantage because the data is stored in the client, so if the data is changed by other users, it will generate loss modifications and other inconsistent conditions. It is necessary to consider the details of their concurrent control, and take advantage of those variables Data may appear inconsistent condition. This technology is used to change with certain techniques. three. Application principles Any technique of any technology is reflected in a certain environment, with buffered database update techniques use more advantages in the following cases: (1) C / S or multi-layer database applications. In this case, the network traffic can be effectively reduced. This technology does not make sense in the case of standing. (2) In the data update occasion of multi table.
For example, in the update of the primary / DETAIL structure, the output of two tables is often influential, then after all updates of the two tables are completed, they can finalize a transaction to commit Update operation. This makes it possible to shorten the time of the transaction, better guarantee the consistency of the data. (3) Operation of limited server load capacity. Today, with the speed of the PC and the price reduction, the ability of clients and servers is getting smaller and smaller, the service capability of the server has dropped relatively. Objectively demanding the perspective of software to reduce the burden of the server, and buffering data updates reduces the burden on the server through the client to partially update the task. (4) The data is relatively low in the data simultaneously updated. If the same data in the database is likely to be updated by multiple users at the same time, then this situation is not suitable for a cache update, because in this case, it is easy to generate an error override, resulting in inconsistencies of data. Four. Control Method in Delphi Overview Delphi as a popular database development tool with rich database manipulation. Delphi made a very comprehensive support in the buffered data access technology. Typically, Delphi provides users with several data set controls such as TTable and TQuery; provide TDBnevigator controls to increase, delete, change, and query the data sets. There is a cachedupdate option in the attribute control of the data set, which is set to true, and Delphi is allowed to access the dataset in the form of buffer, which means that the update operation made by the data set will not automatically reflect. Go to the database server, and only the actual submission method (such as applyupdates (), etc.), Delphi reflects the actual submitted data to the database, and the data set is used to define when the database table is actually updated. A simultaneous operation (such as cascading deletion, etc.). This provides convenience for our own steps to control data, although this makes programming have increased, but we need this flexibility in some occasions. Moreover, through this model, it is indeed greatly reduced the length of the transaction, and the network traffic is reduced, and the reliability of the application is added. Let's take a specific application module to explain how to use this programming mode. Fives. Example of Delphi Program (1) Application Background Description Suppose We do a commodity order processing module. In this module involves three database tables: Order Table ORDER (Order No. ORDERID, amount Summoney, Date Date, customer name COSTOMER) , Quantity Amount, unit price Price and other fields), inventory table Storage (with product number commit, existing stocks, etc.). Among them, the order and the ordering schedule are a pair of relationships to the order number OrderId as the connection field. Whenever a new order is added, the inventory table must be modified, and the number of corresponding items sold will be subtracted from inventory. (2) Program Framework Description The following is a framework of a Delphi program, which has roughly explained how to use cache updates programming mode. The reader can further improve the features of this program.
unit Order; {modules referenced} {unit name} interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Grids, DBGrids, ExtCtrls, DBCtrls, ToolWin, ActnMan, ActnCtrls, ActnMenus, DB, dBTables; {variable declarations, control and method definitions and during the addition} Type TOrderForm = class (TForm) TBOrder: TTable; TBDetail: TTable; OrderDB: TDatabase; ActionMainMenuBar1: TActionMainMenuBar; DBNavigator1: TDBNavigator; DBGrid1: TDBGrid; procedure TBOrderAfterPost (DataSet: TDataSet); procedure TBDetailNewRecord (DataSet: TDataSet); procedure TBDetailUpdateRecord (DataSet: TDataSet; UpdateKind: TUpdateKind; var updateAction: TUpdateAction); procedure TBDetailAfterPost (DataSet: TDataSet); procedure FormCreate (Sender: TObject); private {Private Declarations} public {public Declarations}}}}}} {$ r * .dfm} {The following content is the main program framework} procedure torderform.form.formcreate (Sender: Tobject); {Target The cache update option is set to true} begin tborder.cachedupdates: = true; tbdetail.cach Edupdates: = true; end; procedure torderform.tborderafterpost (DataSet: TDataSet); {After the update of the Order table, perform this procedure content, this process implements the actual submitted transaction to the primary table and the schedule. Note: If the cachedupdates property of a dataset is true, the POST action is only a submission action in the client buffer, not the real submit to the actual database. To achieve a true commit, you need to use the Applyupdates statement. } Begin orderdb.startTransaction; // Update transaction starts executing try tborder.applyupdates; // The actual update of TBDETAIL.ApplyUpdates; // is actually updated to the schedule, and if there is an accident, So roll back this business, exit the process exit; end; ordb.commit; // If there is no accident, complete the transaction submit TBorder.commitupdates; // Clear the TBorder table TBDetail.commitupdates; // Clear TBDetail table The customer buffer end; procedure torderform.tbdetailnewrecord; {The action completed when a day table record is added.