Oracle® Streams Replication Administrator's Guide 10g Release 1 (10.1) Part Number B10728-01 |
|
|
View PDF |
This chapter illustrates an example of a simple single source replication environment that can be constructed using Streams.
This chapter contains these topics:
The example in this chapter illustrates using Streams to replicate data in one table between two databases. A capture process captures data manipulation language (DML) and data definition language (DDL) changes made to the jobs
table in the hr
schema at the str1.net
Oracle database, and a propagation propagates these changes to the str2.net
Oracle database. Next, an apply process applies these changes at the str2.net
database. This example assumes that the hr.jobs
table is read-only at the str2.net
database.
Figure 12-1 provides an overview of the environment.
Text description of the illustration strep035.gif
The following prerequisites must be completed before you begin the example in this chapter.
GLOBAL_NAMES
: This parameter must be set to true
at each database that is participating in your Streams environment.JOB_QUEUE_PROCESSES
: This parameter must be set to at least 2
at each database that is propagating events in your Streams environment. It should be set to the same value as the maximum number of jobs that can run simultaneously plus one. In this example, str1.net
propagates events. So, JOB_QUEUE_PROCESSES
must be set to at least 2
at str1.net
.COMPATIBLE
: This parameter must be set to 10.1.0
or higher at each database that is participating in your Streams environment.STREAMS_POOL_SIZE
: Optionally set this parameter to an appropriate value for each database in the environment. This parameter specifies the size of the Streams pool. The Streams pool contains captured events. In addition, the Streams pool is used for internal communications during parallel capture and apply. If STREAMS_POOL_SIZE
is set to zero (the default), then Streams uses the shared pool. In this case, you may need to increase the size of the shared pool at each database.
See Also:
Oracle Streams Concepts and Administration for information about other initialization parameters that are important in a Streams environment |
ARCHIVELOG
mode. In this example, changes are produced at str1.net
, and so str1.net
must be running in ARCHIVELOG
mode.
See Also:
Oracle Database Administrator's Guide for information about running a database in |
str1.net
database can communicate with the str2.net
database.
strmadmin
) at each database and prompts you for the tablespace you want to use for this user's data. Before you start this example, either create a new tablespace or identify an existing tablespace for the Streams administrator to use at each database. The Streams administrator should not use the SYSTEM
tablespace.Complete the following steps to set up users and create queues and database links for a Streams replication environment that includes two Oracle databases.
/************************* BEGINNING OF SCRIPT ******************************
Run SET
ECHO
ON
and specify the spool file for the script. Check the spool file for errors after you run this script.
*/ SET ECHO ON SPOOL streams_setup_simple.out /*
Connect to str1.net
as SYS
user.
*/ CONNECT SYS/CHANGE_ON_INSTALL@str1.net AS SYSDBA /*
Create the Streams administrator named strmadmin
and grant this user the necessary privileges. These privileges enable the user to manage queues, execute subprograms in packages related to Streams, create rule sets, create rules, and monitor the Streams environment by querying data dictionary views and queue tables. You may choose a different name for this user.
See Also:
Oracle Streams Concepts and Administration for more information about configuring a Streams administrator |
*/ GRANT CONNECT, RESOURCE, DBA, SELECT_CATALOG_ROLE TO strmadmin IDENTIFIED BY strmadminpw; ACCEPT streams_tbs PROMPT 'Enter Streams administrator tablespace on str1.net: ' ALTER USER strmadmin DEFAULT TABLESPACE &streams_tbs QUOTA UNLIMITED ON &streams_tbs;
/*
Connect as the Streams administrator at the database where you want to capture changes. In this example, that database is str1.net
.
*/ CONNECT strmadmin/strmadminpw@str1.net /*
Run the SET_UP_QUEUE
procedure to create a queue named streams_queue
at str1.net
. This queue will function as the SYS.AnyData
queue by holding the captured changes that will be propagated to other databases.
Running the SET_UP_QUEUE
procedure performs the following actions:
streams_queue_table
. This queue table is owned by the Streams administrator (strmadmin
) and uses the default storage of this user.streams_queue
owned by the Streams administrator (strmadmin
).*/ EXEC DBMS_STREAMS_ADM.SET_UP_QUEUE(); /*
Create the database link from the database where changes are captured to the database where changes are propagated. In this example, the database where changes are captured is str1.net
, and these changes are propagated to str2.net
.
*/ CREATE DATABASE LINK str2.net CONNECT TO strmadmin IDENTIFIED BY strmadminpw USING 'str2.net'; /*
Connect to str2.net
as SYS
user.
*/ CONNECT SYS/CHANGE_ON_INSTALL@str2.net AS SYSDBA /*
Create the Streams administrator named strmadmin
and grant this user the necessary privileges. These privileges enable the user to manage queues, execute subprograms in packages related to Streams, create rule sets, create rules, and monitor the Streams environment by querying data dictionary views and queue tables. In this example, the Streams administrator will be the apply user for the apply process and must be able to apply changes to the hr.jobs
table at str2.net
. Therefore, the Streams administrator is granted ALL
privileges on this table. You may choose a different name for the Streams administrator.
See Also:
Oracle Streams Concepts and Administration for more information about configuring a Streams administrator |
*/ GRANT CONNECT, RESOURCE, DBA, SELECT_CATALOG_ROLE TO strmadmin IDENTIFIED BY strmadminpw; ACCEPT streams_tbs PROMPT 'Enter Streams administrator tablespace on str2.net: ' ALTER USER strmadmin DEFAULT TABLESPACE &streams_tbs QUOTA UNLIMITED ON &streams_tbs; GRANT ALL ON hr.jobs TO strmadmin;
/*
Connect as the Streams administrator at str2.net
.
*/ CONNECT strmadmin/strmadminpw@str2.net /*
Run the SET_UP_QUEUE
procedure to create a queue named streams_queue
at str2.net
. This queue will function as the SYS.AnyData
queue by holding the changes that will be applied at this database.
Running the SET_UP_QUEUE
procedure performs the following actions:
streams_queue_table
. This queue table is owned by the Streams administrator (strmadmin
) and uses the default storage of this user.streams_queue
owned by the Streams administrator (strmadmin
).*/ EXEC DBMS_STREAMS_ADM.SET_UP_QUEUE(); /*
Check the streams_setup_simple.out
spool file to ensure that all actions finished successfully after this script is completed.
*/ SET ECHO OFF SPOOL OFF /*************************** END OF SCRIPT ******************************/
Complete the following steps to specify the capture, propagation, and apply definitions for the hr.jobs
table using the DBMS_STEAMS_ADM
package.
/************************* BEGINNING OF SCRIPT ******************************
Run SET
ECHO
ON
and specify the spool file for the script. Check the spool file for errors after you run this script.
*/ SET ECHO ON SPOOL streams_share_jobs.out /*
Connect to str1.net
as SYS
user.
*/ CONNECT SYS/CHANGE_ON_INSTALL@str1.net AS SYSDBA /*
Supplemental logging places additional information in the redo log for changes made to tables. The apply process needs this extra information to perform certain operations, such as unique row identification and conflict resolution. Because str1.net
is the only database where changes are captured in this environment, it is the only database where you must specify supplemental logging for the hr.jobs
table.
The following statement specifies an unconditional supplemental log group for the primary key column in the hr.jobs
table.
*/ ALTER TABLE hr.jobs ADD SUPPLEMENTAL LOG DATA (PRIMARY KEY) COLUMNS; /*
Connect to str1.net
as the strmadmin
user.
*/ CONNECT strmadmin/strmadminpw@str1.net /*
Configure and schedule propagation of DML and DDL changes to the hr.jobs
table from the queue at str1.net
to the queue at str2.net
.
*/ BEGIN DBMS_STREAMS_ADM.ADD_TABLE_PROPAGATION_RULES( table_name => 'hr.jobs', streams_name => 'str1_to_str2', source_queue_name => 'strmadmin.streams_queue', destination_queue_name => 'strmadmin.streams_queue@str2.net', include_dml => true, include_ddl => true, source_database => 'str1.net', inclusion_rule => true); END; / /*
Configure the capture process to capture changes to the hr.jobs
table at str1.net
. This step specifies that changes to this table are captured by the capture process and enqueued into the specified queue.
*/ BEGIN DBMS_STREAMS_ADM.ADD_TABLE_RULES( table_name => 'hr.jobs', streams_type => 'capture', streams_name => 'capture_simp', queue_name => 'strmadmin.streams_queue', include_dml => true, include_ddl => true, inclusion_rule => true); END; / /*
This example assumes that the hr.jobs
table exists at both the str1.net
database and the str2.net
database, and that this table is synchronized at these databases. Because the hr.jobs
table already exists at str2.net
, this example uses the GET_SYSTEM_CHANGE_NUMBER
function in the DBMS_FLASHBACK
package at str1.net
to obtain the current SCN for the source database. This SCN is used at str2.net
to run the SET_TABLE_INSTANTIATION_SCN
procedure in the DBMS_APPLY_ADM
package. Running this procedure sets the instantiation SCN for the hr.jobs
table at str2.net
.
The SET_TABLE_INSTANTIATION_SCN
procedure controls which LCRs for a table are ignored by an apply process and which LCRs for a table are applied by an apply process. If the commit SCN of an LCR for a table from a source database is less than or equal to the instantiation SCN for that table at a destination database, then the apply process at the destination database discards the LCR. Otherwise, the apply process applies the LCR.
In this example, both of the apply process at str2.net
will apply transactions to the hr.jobs
table with SCNs that were committed after SCN obtained in this step.
*/ DECLARE iscn NUMBER; -- Variable to hold instantiation SCN value BEGIN iscn := DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER(); DBMS_APPLY_ADM.SET_TABLE_INSTANTIATION_SCN@STR2.NET( source_object_name => 'hr.jobs', source_database_name => 'str1.net', instantiation_scn => iscn); END; / /*
Connect to str2.net
as the strmadmin
user.
*/ CONNECT strmadmin/strmadminpw@str2.net /*
Configure str2.net
to apply changes to the hr.jobs
table.
*/ BEGIN DBMS_STREAMS_ADM.ADD_TABLE_RULES( table_name => 'hr.jobs', streams_type => 'apply', streams_name => 'apply_simp', queue_name => 'strmadmin.streams_queue', include_dml => true, include_ddl => true, source_database => 'str1.net', inclusion_rule => true); END; / /*
Set the disable_on_error
parameter to n
so that the apply process will not be disabled if it encounters an error, and start the apply process at str2.net
.
*/ BEGIN DBMS_APPLY_ADM.SET_PARAMETER( apply_name => 'apply_simp', parameter => 'disable_on_error', value => 'n'); END; / BEGIN DBMS_APPLY_ADM.START_APPLY( apply_name => 'apply_simp'); END; / /*
Connect to str1.net
as the strmadmin
user.
*/ CONNECT strmadmin/strmadminpw@str1.net /*
Start the capture process at str1.net
.
*/ BEGIN DBMS_CAPTURE_ADM.START_CAPTURE( capture_name => 'capture_simp'); END; / /*
Check the streams_share_jobs.out
spool file to ensure that all actions finished successfully after this script is completed.
*/ SET ECHO OFF SPOOL OFF /*************************** END OF SCRIPT ******************************/
Complete the following steps to make DML and DDL changes to the hr.jobs
table at str1.net
and then confirm that the changes were captured at str1.net
, propagated from str1.net
to str2.net
, and applied to the hr.jobs
table at str2.net
.
Make the following changes to the hr.jobs
table.
CONNECT hr/hr@str1.net UPDATE hr.jobs SET max_salary=9545 WHERE job_id='PR_REP'; COMMIT; ALTER TABLE hr.jobs ADD(duties VARCHAR2(4000));
After some time passes to allow for capture, propagation, and apply of the changes performed in the previous step, run the following query to confirm that the UPDATE
change was propagated and applied at str2.net
:
CONNECT hr/hr@str2.net SELECT * FROM hr.jobs WHERE job_id='PR_REP';
The value in the max_salary
column should be 9545
.
Next, describe the hr.jobs
table to confirm that the ALTER
TABLE
change was propagated and applied at str2.net
:
DESC hr.jobs
The duties
column should be the last column.