Use the scenario in this chapter to get some experience using the DB2 Control Center and the Capture and Apply programs. Follow the steps in this simple scenario to copy changes from a DB2 replication source to a target table in a database on DB2 for Windows NT Enterprise Edition (EE) or Workgroup Edition (WE).
The scenario consists of the following parts:
If you want to work through this scenario on your computer, set up your system using these steps:
The steps in this chapter use the data in the DEPARTMENT table from the
SAMPLE database. The fully qualified name is
userID.Department; where userID is the user
ID that created the table. Table 1 shows the DEPARTMENT table.
DEPTNO | DEPTNAME | MGRNO | ADMRDEPT | LOCATION |
---|---|---|---|---|
A00 | SPIFFY COMPUTER SERVICE | 000010 | A00 | - |
B01 | PLANNING | 000020 | A00 | - |
C01 | INFORMATION CENTER | 000030 | A00 | - |
D01 | DEVELOPMENT CENTER | - | A00 | - |
D11 | MANUFACTURING SYSTEMS | 000060 | D01 | - |
D21 | ADMINISTRATION SYSTEMS | 000070 | D01 | - |
E01 | SUPPORT SERVICES | 000050 | A00 | - |
E11 | OPERATIONS | 000090 | E01 | - |
E21 | SOFTWARE SUPPORT | 000100 | E01 | - |
For the remainder of this exercise, use the user ID with which you created the SAMPLE and COPYDB databases. Because you created the databases, you have the authority (DBADM or SYSADM) to perform replication tasks.
Assume that an application that generates reports needs information that exists in the DEPARTMENT table of the SAMPLE database. Instead of using the data directly from the source table, you want to copy the changes to a target table that can be read only by the report-generating application. For ease of administration, you want to keep the target table on the same machine as the source server.
You require a simple data distribution configuration, with changes from one replication source being replicated to a single read-only copy. This section describes the design and planning issues that you need to consider before you perform any replication tasks.
You already know that the replication source is the userID.DEPARTMENT table in the SAMPLE database. Before you set up your environment, you must decide what you want to replicate from that table. You decide to make all columns available for replication; and you want to save before-image values for each of them so that you can see what is changed.
Tip: | You might want to always include before-image values when you define a replication source. If you later change an update-anywhere configuration, you won't have to redefine your replication source. |
You decide that you want your replication target to be the COPYDB database, which you created using DB2 for Windows NT earlier in this chapter. Currently there is no target table in that database; you want the Control Center to create the target table according to your specifications.
Using existing target tables: When you use the Control Center, the target table is created if it doesn't exist. This method of automatically generating a target table is preferred because it ensures correct mapping to the replication source. You can use existing target tables if they were created by any DB2 product.
Assume that you want the target table in COPYDB to contain the following columns of information:
Because the columns in the target table simply reflect the data from the source table, and there is to be only one record in the target table for each record in the source table, you can use a user copy type of target table.
For the purpose of this exercise, you decide to store the target table
and the replication control tables in the default table space,
USERSPACE1.
Logical server | Table space | Contents |
Source server: SAMPLE | USERSPACE1 | Source replication control tables, including the CD table |
Control server and target server: COPYDB | USERSPACE1 | Replication control tables and the target table |
Typically you will want to put the UOW table and the CD tables (and CCD tables if you are using them) in their own table spaces, with table or table space locking. You can put all other replication control tables together in one table space with row-level locking.
For scheduling replication, assume that you want DB2 replication to check for any changes from the source table every minute and replicate them to the target table. Although a report-generating application doesn't require that kind of turnaround, you want to test the replication environment that you set up to make sure that everything is working correctly.
Also, you decide that after each replication cycle, you want to delete any records from the Apply audit trail table that are older than one week (seven days). This pruning prevents the table from growing too large.
You won't need to set constraints because you have a read-only target. Constraints are needed only when applications are updating a target table. In this scenario, the updates are committed at the replication source, and they must satisfy the constraints defined on that system. There is no reason for you to reevaluate the same constraints at the target.
After planning the replication model, you are ready to set up the replication environment.
The Control Center automatically creates control tables at the source server and at the target server. By default, it builds the control tables with default settings (locking, table space) that are suitable for testing purposes but not for production environments. To customize the control tables for your production environment, you must edit and run the dpcntl.udb file before you perform any other replication task.
To customize control tables:
After you customize the control tables, go to the Control Center to define the DEPARTMENT table as a replication source.
To define a replication source:
Tip: | Most of the time you will want to use the default. By saving the SQL to a file, you can look at the SQL to understand what it will do, make any modifications that you require, save the file, and run it after you are confident that it will do what you expect it to do. |
Tip: | By default, the SQL file is saved in the sqllib directory. When you work in your own replication environment, you will want to keep all the files in a separate directory instead of storing them in sqllib. |
Tip: | When you set up your own replication environment, be careful how you edit this file. If you change the name of the CD table or the table space in which the CD table will be put, you must also modify the CREATE INDEX statement for the CD table and update the CD table entry in the ASN.IBMSNAP_REGISTER row. |
The table DEPARTMENT is now defined as a replication source. When you ran the SQL file, the Control Center created the change data (CD) table for this replication source and it created the replication control tables in the default table space (USERSPACE1) for the SAMPLE database.
After you define the source, you need to define a subscription set. A subscription set defines a relationship between the replication source (DEPARTMENT in this scenario) and a target table (that you will call DEPTCOPY in this scenario). It also defines some replication parameters.
To define a subscription set and a subscription-set member:
Tip: | The Apply qualifier is case-sensitive. If you want the Apply qualifier to be in lowercase characters, you must delimit it when you type it; for example "deptqual". If you simply type deptqual, the Control Center converts the value to uppercase characters by default. |
Tip: | You might want to expand the window to view all of the columns. Also, some rows have names beginning with the letter X (for example, XDEPTNO). These rows store the before-image column values that you requested. |
DEPTNO >='A00'
DELETE FROM ASN.IBMSNAP_APPLYTRAIL WHERE LASTRUN < (CURRENT TIMESTAMP - 7 DAYS)
Tip: | You can define up to ten SQL states that you want to ignore for this subscription. |
Tip: | The value that you set for data blocking depends on how much free space you have on the workstation that runs the Apply program. Typically, you would use a number from 5 to 20. If you want to be very conservative, use 1 minute. |
Tip: | If your source server were on another machine, you would need to log on to the source server over the network. You would use a user ID that has DBADM or SYSADM authority for the source server. However, because the source server for this exercise is on your local machine, you don't need to log on again. |
To configure the Capture program:
Tip: | You must perform the backup action to make the database accessible. The database was put in backup pending mode when you specified that you want to retain log files for roll-forward recovery. |
Tip: | For the purpose of this exercise, you will manually create and bind the Capture and Apply program packages. However, DB2 DataPropagator for all supported UNIX, Windows, and OS/2 operating systems can automatically create and bind the packages for you. |
To manually bind the Capture program
DB2 CONNECT TO SAMPLE
DB2 BIND @CAPTURE.LST ISOLATION UR BLOCKING ALL
The capture.lst file contains a list of the packages created.
Continue with the steps listed below.
To manually bind the Apply program:
DB2 BIND @APPLYUR.LST ISOLATION UR BLOCKING ALL
DB2 BIND @APPLYCS.LST ISOLATION CS BLOCKING ALL
The applyur.lst and applycs.lst files contain a list of the packages that were created.
DB2 CONNECT TO COPYDB
DB2 BIND @APPLYUR.LST ISOLATION UR BLOCKING ALL
DB2 BIND @APPLYCS.LST ISOLATION CS BLOCKING ALL
The applyur.lst and applycs.lst files contain a list of the packages that were created.
For end-user authentication to occur at the source server, you must create a password file with an AUTH=SERVER scheme. The Apply program uses this file when connecting to the source server. Make sure that the user ID that will run the Apply program can read the password file.
To create a password file:
SERVER=SAMPLE USER=userid PWD=password SERVER=COPYDB USER=userid PWD=passwordWhere:
Password file format: Do not put blank lines or comment lines in this file. Add only the server-name, user ID, and password information. This information enables you to use different passwords or the same password for each server.
Password file naming convention:
The password file name is applyqual.pwd; where applyqual is a case-sensitive string that must match the case and value of the Apply qualifier (APPLY_QUAL) in the subscription set table. The file naming convention from Version 5 of DB2 DataPropagator is also supported: ApplyqualInstance_nameControl_server.pwd; which includes the case-sensitive Apply qualifier, the instance name that the Apply program runs under (the default name is DB2, in uppercase), and the name of the control server in uppercase (for example, COPYDB).
For more information about authentication and security, refer to the IBM DB2 Administration Guide.
After defining the replication source and the subscription set, you can submit the copy request by starting the Capture and Apply programs.
To start the Capture program:
ASNCCP SAMPLE COLD NOPRUNE
Tip: | Usually you would not specify the cold start option; you would let the Capture program determine whether it should cold start or warm start. For this exercise, you are forcing the Capture program to cold start to clean up the records in the CD and UOW tables. |
The Capture program starts running but no new command prompt appears. This action creates a *.ccp file. The Capture program is initialized but it does not start capturing changes for the defined replication source until you start the Apply program and it completes its initial full-refresh copy.
To start the Apply program:
Tip: | You must start the Apply program in the same directory in which you stored the password file. If you try to start the Apply program in another directory, you will get an error message. |
ASNAPPLY DEPTQUAL COPYDB
Tip: | The Apply qualifier, DEPTQUAL, is case-sensitive and must be entered in uppercase for this exercise. |
Tip: | You can use the LOADX invocation parameter to call the ASNLOAD program. Type the LOADX parameter after the database name (COPYDB) in the command statement above. The default ASNLOAD program uses the EXPORT utility to export data from the source table and uses the LOAD utility to fully refresh the target table. You can modify ASNLOAD to call any IBM or vendor utility. |
The Apply program starts running, but no new command prompt appears. You can check the Apply trail table (ASN.IBMSNAP_APPLYTRAIL) in COPYDB for status information.
If you view the DEPTCOPY target table after one replication cycle, you
should see results that match the data shown in Table 2.
DEPTNO | DEPTNAME | MGRNO | ADMRDEPT | LOCATION |
---|---|---|---|---|
A00 | SPIFFY COMPUTER SERVICE | 000010 | A00 | - |
B01 | PLANNING | 000020 | A00 | - |
C01 | INFORMATION CENTER | 000030 | A00 | - |
D01 | DEVELOPMENT CENTER | - | A00 | - |
D11 | MANUFACTURING SYSTEMS | 000060 | D01 | - |
D21 | ADMINISTRATION SYSTEMS | 000070 | D01 | - |
E01 | SUPPORT SERVICES | 000050 | A00 | - |
E11 | OPERATIONS | 000090 | E01 | - |
E21 | SOFTWARE SUPPORT | 000100 | E01 | - |
After the replication environment is up and running, changes that are made to the replication source will be replicated to the target table. You must prune the control tables periodically to prevent them from growing too large. Although the Capture and Apply programs can run continuously, there are times when you will want to stop them (for example, to run utilities that use the table spaces that contain the control tables).
Assume that two new departments were created at the Spiffy Computer Service: a technical writing department and a public relations department.
To update the source table:
DB2 CONNECT TO SAMPLE
DB2 INSERT INTO DEPARTMENT VALUES ('F01','TECHNICAL WRITING','000110','F01',NULL) DB2 INSERT INTO DEPARTMENT VALUES ('G01','PUBLIC RELATIONS','000120','G01',NULL)
DB2 CONNECT TO COPYDB
DB2 SELECT * FROM DEPTCOPY
Tip: | The replication process does not occur immediately. You should wait approximately five minutes before checking the table. |
Table 3 shows the results of the replication, with two new rows
appended to the table.
Table 3. DEPTCOPY table after changes are replicated
DEPTNO | DEPTNAME | MGRNO | ADMRDEPT | LOCATION |
---|---|---|---|---|
F01 | TECHNICAL WRITING | 000110 | F01 | - |
G01 | PUBLIC RELATIONS | 000120 | G01 | - |
A00 | SPIFFY COMPUTER SERVICE | 000010 | A00 | - |
B01 | PLANNING | 000020 | A00 | - |
C01 | INFORMATION CENTER | 000030 | A00 | - |
D01 | DEVELOPMENT CENTER | - | A00 | - |
D11 | MANUFACTURING SYSTEMS | 000060 | D01 | - |
D21 | ADMINISTRATION SYSTEMS | 000070 | D01 | - |
E01 | SUPPORT SERVICES | 000050 | A00 | - |
E11 | OPERATIONS | 000090 | E01 | - |
E21 | SOFTWARE SUPPORT | 000100 | E01 | - |
The following steps assume that the Capture program is running. If the Capture program is not running, the prune command will not work.
Tip: | On Windows NT, you can use the Task Manager to determine if the Capture program (ASNCCP) is running. |
To verify that there is something to prune:
DB2 CONNECT TO SAMPLE
DB2 SELECT COUNT(*) FROM ASN.IBMSNAP_UOW
There should be two rows in the unit-of-work table from the previous replication.
To run the prune command:
ASNCMD SAMPLE PRUNE
Tip: | Usually you should prune during off-peak hours. |
DB2 SELECT COUNT(*) FROM ASN.IBMSNAP_UOW
There should be no rows in the table.
An important part of maintaining your replication environment is regular database maintenance. Sometimes that maintenance will require you to stop the Capture and Apply programs. For example, you must stop the Capture and Apply programs before you run utilities that directly use the table spaces that are used by these programs.
Perform the following steps in the DB2 command window that you opened in the previous step.
To stop the Capture program:
Type the following command: ASNCMD SAMPLE STOP
To stop the Apply program:
Type the following command: ASNASTOP DEPTQUAL (where DEPTQUAL is the case-sensitive Apply qualifier).
You can run DB2 utilities on your database now that you stopped the Capture and Apply programs. (Running the utilities is beyond the scope of this exercise.)