ODAC (V9.5.15) study notes (17) master-slave mode

Source: Internet
Author: User

Master-Slave mode (master/detail modes) is a collection of multiple datasets that establish a primary table and a relationship from a table.

1. Relationship Settings

To set the master-slave mode, you must have a primary table DataSet (Tdataset) and one from the table DataSet (Tdataset), and the main table dataset is associated with a data source component (Tdatasource), and the Mastersource from the table dataset points to the data source component. Finally, set the associated field. There are 2 ways to set the associated fields between master and slave tables:

1. Set the foreign key from the SQL of the table dataset, and point to the fields of the main table by the parameters, such as:

// Main Table DataSet

Master: = Toraquery.create (self);

Master.SQL.Text: = ' SELECT * from Department ';

// from a table data set

Detail: = Toraquery.create (self);

Detail.SQL.Text: = ' SELECT * from Employee WHERE dept_no =:D ept_no ' ;

// set up a data source

Mastersource: = Tdatasource.create (self);

Mastersource.dataset: = Master;

Detail.mastersource: = Mastersource;

The colon (:) in SQL represents a variable followed by the name of the variable that is consistent with the primary key field of the primary table.

2. Set the Masterfields and Detailfields properties from the table, such as:

// Main Table DataSet

Master: = Toraquery.create (self);

Master.SQL.Text: = ' SELECT * from Department ';

// from a table data set

Detail: = Toraquery.create (self);

Detail.SQL.Text: = ' SELECT * from Employee ';

// Set the associated field

Detail.masterfields: = ' Dept_no '     ; // primary table primary key

Detail.detailfields: = ' Dept_no ' ; // field that corresponds to primary table primary key from table

// set up a data source

Mastersource: = Tdatasource.create (self);

Mastersource.dataset: = Master;

Detail.mastersource: = Mastersource;

2. Data acquisition

There are 2 modes of master-slave mode data acquisition:

1. Traditional mode

In the traditional mode, when the master-slave data set gets the data, the main data set is opened, and the corresponding data from the data set is obtained from the dataset according to the associated fields for each record of the main data set. The biggest criticism is how many times the primary data set is recorded, and how many queries from the dataset need to be executed, very inefficient.

2. Local mode (Localmasterdetail)

ODAC's native master-slave relationship model, which fetches the primary table dataset and all records from the table DataSet once from the server, and then filters the records from the table dataset locally based on the master-slave relationship settings. The greatest benefit of this is to reduce the overhead on the server, especially when the number of primary table records is very large, and the SQL execution of the database is very small and efficient.

The traditional model and the local model each have advantages and disadvantages, is really "bear paw and fish" relationship. The setting of 2 modes depends on the options.localmasterdetail of the primary dataset, if True indicates local mode, if False is the traditional mode.

3. Update

If the master-slave relational data set is packaged to TClientDataSet processing by Tdatasetprovider one time, the server can be submitted to save the data at once, regardless of the master-slave relationship and transaction processing. However, if you use ODAC for processing, there are 2 aspects to the update of the master-slave relational dataset:

1, local update, that is, the local data processing.

In buffered mode, the Cachedupdates property from the table is valid only if the primary table's option Localmasterdetail is set to true. Whether buffered or traditional, local update support automatically fills in the values of the associated fields in the corresponding main table dataset when inserted from the table, with the disadvantage that cascading deletions and edits are not supported, that is, when deleting a primary table record, the table record does not follow cascading deletions, and the value of the corresponding field from the table changes when the Primary Table association field value is changed All this requires the code to control itself.

2, remote update, will be the local update commit database saved.

Whether buffered or traditional, ODAC does not support a tclientdataset-like unified data submission feature, which must be opened by code to open a connected transaction, commit the main table dataset in turn, and modify from the table DataSet, and the commit order is controlled by the code. This has one of the biggest problems: when inserting a new record, you need to submit the primary table DataSet first, and then commit the FOREIGN KEY constraint error from the table dataset by committing the table dataset and then committing the primary table dataset when you delete the record from the tables data set. If the insert and delete actions exist at the same time, a contradiction arises. This is also the problem with unified submissions through the Applyupdates method of connecting components.

The solution can only be controlled by the client, modifying a master record and how it must be saved from the record before other master record operations can be performed. Fortunately, the master-slave data set needs to operate and save multiple master records is very rare, so this problem is not very large, but the client control to do a good job.

ODAC (V9.5.15) study notes (17) master-slave mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.