| |
Using Oracle Streams to Integrate Your Data
Module Objectives
Purpose
In this module, you will learn how to use the Streams Wizard to replicate
between databases.
Objectives
After completing this module, you should be able to:
Prerequisites
Before starting this module, you should have:
Reference Material
The following is a list of useful reference material if you want additional
information about the topics in this module:
 |
Documentation:
|
| |
Streams |
| |
Supplied PL/SQL Packages and Types Reference |
| |
Oralce9i Reference |
NOTE: Before starting the lesson, please
verify that the Streams Patch for the OMS and GUI client, listed
in the prequisites, has been installed.
Overview
Oracle Streams enables the sharing of data and events in a data stream
either within a database, or from one database to another. The stream
routes published information to subscribed destinations. The result is
a new feature that provides greater functionality and flexibility than
traditional solutions for capturing and managing events, and sharing the
events with other databases an applications. With Oracle Streams, you
can build and operate distributed enterprises and applications, data warehouses,
and high availability solutions with a single unified solution, rather
than multiple special purpose technologies. As users' needs change, you
simply implement a new capability of Oracle Streams, without sacrificing
existing capabilities.
Oracle Streams provides a set of elements. Using these elements, users
control what information is put into a stream, how the stream flows or
is routed from node to node, what happens to events in the stream as they
flow into each node and how the stream terminates.
The three basic elements of Streams are:
Capture: To capture DML or DDL events from the redo log or user
messages via an explicit API
Stage: To store and propagate events in a single database or between
databases
Apply: To apply DML or DDL events to a destination database or
to pass to an application via an explicit API
These elements can be used within a single database or combined with
Streams elements in other databases to form a distributed environment.
By specifying the configuration of the elements acting on the stream,
users can address their individual business requirements.
WHAT CAN STREAMS DO?
Capture changes at a database
You can configure a background capture process to capture changes
made to tables, schemas, or the entire database. A capture process captures
changes from the redo log and formats each captured change into a logical
change record (LCR). The database where changes are generated in
the redo log is called the source database. You can capture changes
to data, for example - an insert into a table (DML), and database objects
such as adding a column to a table (DDL). The capture process can intelligently
filter LCRs based upon user-specified rules. Thus, only changes to desired
objects are captured.
Enqueue events into a queue.
Streams queues are queues of type SYS.AnyData. Two types of
events may be staged in a Streams queue: LCRs and user messages.
A capture process enqueues LCR events into a Streams queue that you
specify. The queue can then share the LCR events within the same database
or with other databases.
You can also enqueue user events explicitly with a user application,
also referred to as user messages or user-enqueued events.
These explicitly enqueued events can be LCRs or user messages.
Propagate events from one queue to another.
Queue propagation can be configured within the same database or in
different databases. Streams interoperates with Advanced Queuing (AQ),
which supports all the standard features of message queuing systems.
AQ queues, also referred to as typed queues, stage messages of a single,
fixed type.
Dequeue events.
A background apply process can dequeue events. You can also
dequeue events explicitly with a user application.
Apply events at a database
You can configure an apply process to apply all of the events in a
queue or only the events that you specify. You can also configure an
apply process to call your own PL/SQL subprograms to process events.
Custom PL/SQL subprograms to process user-enqueued events are called
message handlers. Apply can also be customized for LCR events,
both captured and user-enqueued. For DML LCRs (inserts/updates/deletes),
a dml handler can be specified at the table level. DDL LCR customization
can be specified for an apply process, and is referred to as a ddl
handler.
The database where LCR events are applied and other types of events
are processed is called the destination or target database.
In some configurations the source database and the destination database
may be the same. Captured LCRs from multiple databases can be sent to
a single destination database streams queue. Multiple apply processes
can be configured from a single streams queue.
Oracle Streams supplies additional functionality to enable:
Transformations
Data Sharing between unique tables is possible with a Streams transformation.
For example, destination tables with different names or columns can
be maintained using transformations. A transformation is a user-defined
function associated with an individual rule that is performed as an
event satisfying that rule is enqueued and/or dequeued from the staging
area.
Directed Networks
Oracle Streams events can be configured to pass through multiple intermediate
databases before arriving at a destination database, if desired. Propagation
of events is independent of the apply process. An event may or may not
be processed by an apply process at an intermediate database.
Automatic Conflict Detection and Resolution
An apply process detects conflicts automatically when directly applying
LCRs. Typically, a conflict results when the same row in the source
database and destination database is changed at approximately the same
time. Streams offers a variety of prebuilt conflict resolution handlers
to handle most conflicts, and allows custom, user-defined conflict handlers
to be defined as well.
Heterogeneous Information Sharing
Oracle Streams supports capture and apply from Oracle to non-Oracle
system using a transparent gateway. Streams also includes a programmatic
interface to allow non-Oracle data sources to easily submit or receive
change records, allowing for heterogeneous data movement in both directions.
In addition messages can be sent to and received from other message
queuing systems such as MQ Series via the Message Gateway.
DEMO
In this demo, we will focus on a subset of Oracle Streams' capabilities,
including bi-directional replication of both DML and DDL, and customized
apply processing.
Suppose we have an organization with two autonomous groups that need
to share information. Each organization uses their own applications and
databases for managing their work. One of these groups is responsible
for Order Entry. This group enters the customer orders via an application
that places the order in a queue for processing. They also respond to
customer queries on order status. The second group does Order Fulfillment.
People in this group are responsible for scheduling the delivery and shipping
the order to the customer. Pending orders are scheduled for delivery periodically
by a SQL application, known as Shipping.
We will configure bi-directional replication between the two databases
ORCL and STRM for the DEMO schema, which includes the table ORDERS. Using
the Streams Setup Wizard from Oracle Enterprise Manager, we will first
configure the replication site as the ORCL database with the target as
the STRM database. Then, we will repeat the steps specifying the replication
source site as STRM and the target as the ORCL database.
The Streams wizard will create the capture and apply processes for replication
and configure the job to schedule the queue propagation between the databases.
Rules will be defined so that we only handle DML and DDL changes for the
DEMO schema. Database objects within the DEMO schema are exported from
the source database and imported to the target site automatically as well
the requisite Streams metadata for creation, or instantiation,
of Streams replicated objects.
The Order Entry application submits a customer order into the Streams
queue for processing. We will configure an apply process that will detect
the customer order in the queue and process it using the procedure we
supplied as a message handler. Our message handler takes the order from
the queue and converts it into an insert into the ORDERS table.
Whenever the shipping application is run by the ORDER FULFILLMENT organization,
the delivery date is updated in the ORDERS table. Because we used the
Streams Wizard to configure replication on the ORDERS table between the
two databases, this update is captured at the FULFILLMENT site (STRM)
and replicated back to the ORDER ENTRY organization database (ORCL). In
addition, we want to put the same update message in the queue for later
processing by another application at the ORDER ENTRY site(ORCL). With
Streams, we can do this by customizing the apply process at the ORCL site
for when changes are made to the ORDERS table. We configure a dml handler
that not only applies the table update, but also re-enqueues the change
into the queue for future handling by another application.
The final step of this example demonstrates the ability to perform DDL,
such as a CREATE TABLE statement, in the DEMO schema and have it replicate
to the other database. Subsequent data entry into the newly created table
will also be replicated automatically.
Create STRM Database
This lesson requires three databases: a source database (ORCL) , target
database (STRM), and Enterprise Manager Repository database.
| 1. |
Start the Database Configuration Assistant to create the STRM database.
Start > Programs > Oracle - OraHome92 > Configuration
and Migration Tools > Database Configuration Assistant
|
| 2. |
Click Next.
|
| 3. |
Select Create a database then click Next.
|
| 4. |
Select General Purpose then click Next.
|
| 5. |
Enter the following values. After entering the values, click Next.
Global Database Name: strm.world
SID: strm
|
| 6. |
Select Dedicated Server Mode, then click Next.
|
| 7. |
Select Custom. Some of the values for Custom are in Bytes
and some are in M
Bytes. Change all values to M
Bytes. Use the following values.
Shared Pool: 24 M Bytes
Buffer Cache: 8 M Bytes
Java Pool: 16 M Bytes
Large Pool: 8 M Bytes
PGA: 10 M Bytes
After making the changes click on the Archive tab.
|
| 8. |
Click No, you do not want the recommend values.
|
| 9. |
Check both Archive Log Mode and Automatic archival.
Change the filename format to the following:
Log Archive Filename Format: arc_%s.log
Click Finish.

|
| 10. |
Verify the Summary then click OK.
|
| 11. |
Database creation begins.

|
| 12. |
After creating the database, you will be prompted to set the password
for both the SYS and SYSTEM users. Use the password oracle for both.
SYS Password: oracle
Confirm SYS Password: oracle
SYSTEM Password: oracle
Confirm SYSTEM Password: oracle
|
| 13. |
Start and stop the OracleOraHome92Agent service. From
the commnd prompt type:
agentctl stop
agentctl start

|
| 15. |
Select Start > Programs > Oracle - OraHome92
> Enterprise Manager Console. Log in as sysman/sysman
to your Oracle Management Server. |
| 16. |
To discover the new database in Enterprise Manager the agent must
be bounced which has been done and the node refreshed. Select Navigator
> Refresh All Nodes. Select Finish and OK when discover
is complete.

|
Setting the Preferred Credentials for your Database and Node
Before you can use the Streams Wizard, you need to make sure that your
preferred credentials in Oracle Enterprise Manager are set correctly.
To do this, perform the following steps:
| 1. |
Select Start > Programs > Oracle - OraHome92 > Enterprise
Manager Console. Log in as sysman/sysman
to your Oracle Management Server.
|
| 2. |
From the Console, select the Configuration pulldown then
select Preferences. Click the Preferred Credentials
tab. Select your primary database (orcl.world) verify the
settiting
Target Name: orcl.world
Username: sys
Password: oracle
Confirm: oracle
Role: SYSDBA
Select the newly create STRM database (strm.world) and set
its credentials:
Target Name: strm.world
Username: sys
Password: oracle
Confirm: oracle
Role: SYSDBA

|
| 3. |
Slide down to the target type NODE and select the name of your
NODE (compter name). Set the username and password for the node.
Target Name: <Your Node Name>
Username: ntoem
Password: ntoem
Confirm: ntoem

|
Verify the Preferred Credentials for your Node
The Streams Wizard submits jobs through Enterpirse Manager Jobs. If the
Preferred Credentials are not corrects the Streams lesson will fail. Verify
the Preferred Credentials by creating and submitting a simple job. This
job will execute the
dir command on your node. To do this, perform the following
steps:
| 1. |
Select Start > Programs > Oracle - OraHome92 > Enterprise
Manager Console. Log in as sysman/sysman
to your Oracle Management Server.
|
| 2. |
Right click on Network > Jobs and select Create Job...

|
| 3. |
When creating a job the job must be named and a type of job selected.
Enter the name and target type as shown below:
Job Name: Test Job
Target Type: Node
Available Tagets: <select your node>
After entering the job name and changing the target type, select
your node from the list of available tartgets then clik Add.
The node will then appear in the list of selected targets.
Next select the Tasks tab.

|
| 4. |
From the list of available tasks select Run OS Command then
click ADD. Select the Parameters tab.

|
| 5. |
On the Command line type:
dir
Click Submit.

|
| 6. |
The job can now be seen in the Active pane. Click on the History
tab. Once the job terminates it will appear in the History pane.

|
| 7. |
If the NTOEM user and Preferred Credentials are configured correctly
the status of the job in the History pane will be Complete. If the
status is not complete please verify the steps in the Postinstallation
module and the Preferred Credentials above.

|
Setup Schemas
| 1. |
In SQL*Plus connect to ORCL as SYS. Run script demo_setup_source.sql
to create the DEMO user and populate the schema.
@demo_setup_source.sql
|
| 2. |
In a seperate SQL*Plus window connect to STRM as SYS. Run script
demo_setup.sql to create the DEMO user. In the target database the
schema being replicated must already exist. In the script the DEMO
user is being created, however no objects are placed in the schema.
@demo_setup_target.sql
|
1 Streams Replication ORCL =>
STRM
This lesson provides instructions for preparing a database or a distributed
database environment to use Streams and for configuring a Streams environment.
| 1. |
Start Enterprise Manager console and connect as sysman/sysman.
|
| 2. |
Select Network > Databases > orcl.world > Distributed
> Streams. From the right hand window click on Setup Streams.
|
| 3. |
Click Next.
|
| 4. |
Enter strmadmin as the password for the user STRMADMIN.
Verify the box next to 'Create a new Streams administrator' is checked.
Click Next.
|
| 5. |
Choose strm.world from the drop down menu for the name of
the Destination Database. Enter strmadmin as the password
for the user STRMADMIN. Verify the box next to 'Create a new Streams
administrator' is checked. Enter oracle for the SYS password.
Click Next.
|
| 6. |
We want to capture both DML and DDL changes to the DEMO schema.
Select DEMO from the Available Objects, then click the arrow (>)
for it to appear in the Selected objects. Check the box next
to 'Capture, propagate, and apply data definition language (DDL)
changes.' Click Next.
|
| 7. |
Review the Summary and click Finish.
|
| 8. |
A warning will appear indicating the scripts the wizard executes
have not been saved. We want to save the scripts. Click Yes.
|
| 9. |
Click OK to save the scripts in the default location: D:\oracle\ora92\sysman\report.
The scripts are prefixed with the database global name.
|
| 10. |
When the wizard processing completes, click Close.
|
| 11. |
As part of the installation a job is executed to export the DEMO
schema from the source database (orcl) and import into the target
database (strm). Lets view the output of the job to verify it was
successful.
Select Network > Jobs. Click on the History tab.
Double click on the name that begins 'Export_orcl'. Select the listing
with a 'Completed' status and click on Show Output.
After viewing the output click Close then Cancel.
|
Verify Streams Configuration
Streams creates three processes during the setup, two for the source
database and one for the target. The source database contains the capture
and propagate processes and the target contains the apply process.
| 13. |
Navigate to Network > Database > orcl.world > Distributed
> Streams. Under Capture and Propagate sub trees are the
new processes.
|
| 14. |
Take time to investigate the various tabs available for processes
under capture and propagate.
The capture process created by the Streams Wizard is an Oracle
background process with a system-generated ruleset name and rules
for the ORDERS. The Streams Wizard automatically generates a Streams
name for the capture process by prepending the Streams Administrator
schema name to the word _CAPTURE. In this exercise, the Streams
name for capture is STRMADMIN_CAPTURE If you use the Streams Wizard
to add another table to this configuration, the new rules will be
added to the ruleset for the STRMADMIN_CAPTURE process by default.
From the operating system point of view, the identifier for the
capture background process is CPxx, where xx indicates the number
of unique capture processes that have been configured on the database.
Trace files generated for the capture process will include the OS
process name (ie, ora_cp01_1245.trc)
After a capture process is initially configured and started on
a database, it will continue to run. The state of the process will
persist across database shutdown and startup. If the capture process
was running when the database was shutdown, then it will automatically
start when the database startup is performed. If the capture process
was disabled or aborted before the shutdown, then it will remain
in that state.
You can stop and start the capture process at any time. Stopping
and starting the capture process will reset the statistics available
for monitoring capture. These statistics are dynamic views into
the capture process and are only available when the capture process
is running. During a restart of the capture process, capture automatically
determines where to start processing in the redo logs, based on
knowledge of downstream apply site information and logminer checkpoints.
Some of the key statistics for monitoring the capture process are
:
Total messages enqueued - Total number of messages from the
redo log that satisfied rules in the capture rule set. This statistic
shows the total number of messages that have been successfully staged
into the Streams queue by the running capture process.
Capture message create time - Creation time of the most
recent message scanned in the redo log. This statistic shows the
creation time of the most recent message scanned from the redo logs.
The creation time of a message is the time when a DML or DDL operation
is performed at the site.
Enqueue message create time - Creation time of the last
enqueued message. This statistic shows the creation time of the
most recent message that was successfully staged into the Streams
queue.
|
| 15. |
The apply process created by the Streams Wizard is an Oracle background
process with a system-generated ruleset name and rules for the ORDERS.
The Streams Wizard automatically generates a Streams name for the
apply process by prepending the Streams Administrator schema name
to the global name of the source database. In this exercise, the
Streams name for apply is STRMADMIN_ORCL_US_ORACLE_COM If you use
the Streams Wizard to add another table to this configuration, the
new rules will be added to the ruleset for the STRMADMIN_ORCL_US_ORACLE_COM
process by default.
From the operating system point of view, the identifier for the
apply background process is APxx, where xx indicates the number
of unique apply processes that have been configured on the database.
Each Streams APPLY configuration actually consists of a minimum
of 3 processes. In addition to the Apxx process, 2 or more parallel
execution server processes (p000, p001,
) are configured.
The APPLY reader(p000) is one of these server processes. It reads
from the queue and passes the LCRs to the coordinator process (ap01).
The coordinator process builds up the LCRs into transactions and
passes them to one or more apply servers (p001,p002) depending on
the setting of the apply parameter, PARALLELISM. Trace files generated
for the apply process will include the OS process name (ie, ora_ap01_1247.trc,
ora_p000_1248.trc, ora_p001_1250.trc)
After an apply process is initially configured and started on a
database, it will continue to run, unless the process becomes disabled
or aborted. The state of the process will persist across database
shutdown and startup. If the apply process was running when the
database was shutdown, then it will automatically start upon database
startup. You can stop and start the apply process at any time. Stopping
and starting the apply process will reset the statistics available
for monitoring the apply processes. These statistics are dynamic
views into the apply process and are only available when the apply
process is running. During a restart of the apply process, capture
automatically determines where to start processing.
|
| 16. |
By default, an apply process becomes disabled when an unresolved
error is encountered during the apply process. The message that
caused the error is placed in the Streams Error queue for later
handling and disables itself. To allow the apply process to continue
after an unresolved error has been encountered, change the Disable
on Error parameter to N.
Click on the process which starts 'STRMADMIN_ORCL' and click the
Parameter tab. Change the value of DISABLE_ON_ERROR to N,
then click Apply.
|
| 17. |
Click on the Statistics tab. At the bottom of the frame is a Refresh
button. The frame contains statistics on applying messages.
Take time to investigate the various other tabs.
There are 3 sets of statistics for monitoring the apply process.
Coordinator statistics:
Transactions applied - Number of transactions applied. This
statistic shows the overall work of the apply engine.
Transactions received - Number of transactions received from
the reader process. Each transaction can consist of one or more
messages.
Transactions assigned- Number of transactions assigned to
the apply servers
Reader statistics:
Total messages dequeued - Total number of messages dequeued
from the Streams queue that satisified the rules in the apply rule
set.
Dequeued message create time - Creation time of the message
dequeued from the Streams queue. The creation time of a message
is the time when a DML or DDL operation is performed at the source
site.
Server Statistics: (one row per parallel execution server)
Total assigned - Total number of transactions assigned to
this server
Total Messages Applied - Total number of messages applied
by this server
|
2 Streams Replication STRM =>
ORCL
We have configured replication from the ORDER ENTRY site (ORCL database)
to the ORDER FULFILLMENT site (STRM database). Now, we want to replicate
in the reverse direction, so that when the order has been shipped, the
ORDER ENTRY database is updated with the delivery date.
| 1. |
Select Network > Databases > strm.world > Distributed
> Streams. From the right hand window click on Setup Streams.
|
| 2. |
Click Next.
|
| 3. |
Enter strmadmin as the password for the user STRMADMIN.
Click Next.
|
| 4. |
Choose orcl.world from the drop down menu for the name of
the Destination Database. Enter strmadmin as the password
for the user STRMADMIN. Uncheck the box next to 'Create a new Streams
administrator'. The STRMADMIN user has already been created for
the site. Click Next.
|
| 5. |
We want to capture both DML and DDL changes to the DEMO schema.
Select DEMO from the Available Objects, then click the arrow (>)
for it to appear in the Selected objects. Check the box next to
'Capture, propagate, and apply data definition language (DDL) changes.'
Since the objects in the schema were exported from ORCL and imported
into STRM during the setup of ORCL, the data itself (rows in the
tables) have not changed from the initial configuration and thus
does not need to be done again. The export and import tasks are
still performed to set the Streams metadata necessary for the apply
process, the instantiation scn from the STRM database. Uncheck the
box in front of 'Copy existing data to the destination...'. Click
Next.
|
| 6. |
Review the Summary and click Finish.
|
| 7. |
A warning will appear indicating the scripts the wizard executes
have not been saved. We want to save the scripts. Click Yes.
|
| 8. |
Click OK to save the scripts in the default location: D:\oracle\ora92\sysman\report.
The scripts are prefixed with the database global name.
|
| 9. |
When the wizard processing completes, click Close.
|
Verify Streams Configuration
Streams creates three processes during the setup, two for the source
database and one for the target. The source database contains the capture
and propagate processes and the target contains the apply process.
| 10. |
Navigate to Network > Database > strm.world > Distributed
> Streams. Under Capture and Propagate sub trees are the
new processes.
|
| 11. |
Take time to investigate the various tabs available for processes
under capture and propagate. The Statistics tab contains statistics
on changes captured and can be refreshed with the Refresh button
at the botton of the frame.
|
| 12. |
Navigate to Network > Database > orcl.world > Distributed
> Streams. Under the Apply sub tree is a new processes.
|
| 13. |
Click on the process which starts 'STRMADMIN_ORCL' and click the
Parameter tab. Change the value of DISABLE_ON_ERROR to N,
then click Apply.
|
| 14. |
Click on the Statistics tab. At the bottom of the frame is a Refresh
buttom. The frame contains statistics on applying messages.
Take time to investigate the various other tabs
|
3 Customizing Apply
As you will recall from the Demo Overview, there are some special handling
requirements for orders. Orders are submitted to the queue for processing.
These messages will be converted into an insert into the ORDERS table
owned by DEMO. There is also a requirement that the message be made available
to another application after the delivery is scheduled. We will create
and customize the apply processes to accomplish these goals. Before we
begin, let's load up some of the scripts that we will need.
| 1. |
In SQL*Plus connect to ORCL as DEMO and run script demo_proc.sql.
@demo_proc.sql
|
| 2. |
The Order Entry application submits an order message into the
Streams queue. In the next few steps, we will describe the steps
to create the apply process with a message handler that will identify
and retrieve the order messages from our application. The script
ap_msghandler.sql performs the tasks described in steps 3 thru 6
below. But first, let's run the script @ap_msghandler.
In SQL*PLUS connect to ORACLE as STRMADMIN and run AP_msghandler.sql.
@ap_msghandler.sql
|
| 3. |
We first construct a PL/SQL procedure that takes the messages staged
into the Streams queue by the Order Entry application of the type
ORDER_EVENT_TYP as defined in the DEMO schema and converts them
into a SQL insert statement into the DEMO.ORDERS table, as shown
in the procedure apply_msghdlr_proc owned by the DEMO schema.
|
| 4. |
We will need to identify the messages to be selected for the apply
process. To do this, we will create a set of rules (apply_msghdlr_rs
is the rule set name) and add a rule to that rule set that selects
only messages of the user-defined type, DEMO.ORDER_EVENT_TYP. To
simplify the rule construction, we create an evaluation context
for the rule set. The evaluation context allows us to define a table
alias (tab) for the table 'STRMADMIN.STREAMS_QUEUE' within the rule
definition.
|
| 5. |
Now that we have both the PL/SQL procedure and the ruleset defined,
we can create the apply process. The apply process is given a name
that we choose, apply_w_msghdlr. We also specify the rule set name
(apply_msghdlr_rs) and the message handler (apply_msghdlr_proc ).
By default, the apply process is configured with an apply tag, so
that changes made by the apply process are ignored by the capture
process when it is scanning the redo log. In this case, we DO want
the changes that are made by the apply process to be captured. You
will notice that we alter the apply process to remove the apply
tag by setting the parameter remove_apply_tag to true.
The following code is a portion of ap_msghandler.sql from step
2, which creates the apply process with message handler.
/* create the apply process with the message handler */
begin
dbms_apply_adm.create_apply (
queue_name => 'streams_queue',
apply_name => 'apply_w_msghdlr',
rule_set_name => 'apply_msghdlr_rs',
message_handler => 'demo.apply_msg_hdlr_proc');
end;
/
begin
dbms_apply_adm.alter_apply(
apply_name=>'apply_w_msghdlr',
remove_apply_tag=>true);
end;
/
|
| 6. |
You can use the GUI to start the process, or execute the start_apply
api.
Code sample from ap_msghandler.sql in step 2.
/* start the apply procedure */
begin
dbms_apply_adm.start_apply (apply_name => 'apply_w_msghdlr');
end;
/
|
| 7. |
Configure Explicit Enqueue/Dequeue permissions Use SQL*PLUS to
do this and use the script demo_grants_source.sql This script is
run as STRMADMIN and sets up the privileges for demo to enq/deq
into the STRMADMIN queue, as well as explicit secure queue privileges
for DEMO to DEMO_AGENT and EXPLICIT_DQ agents.
@demo_grants_source.sql
|
| 8. |
Now we are ready to customize the apply process for the replicated
DEMO.ORDERS table. You will recall that we want to maintain the
data in the ORDERS table, and also requeue the LCR for another application.
You will notice in the orders_dml_handler procedure that we call
another procedure to enqueue the LCR into the Streams queue for
use by another application. Towards the end of the procedure, you
will notice a line "lcr.execute(true)". This is the command
to perform the changes specified in the LCR on the specified table.
A DML handler procedure can be written to apply the LCR as is, or
in a modified state, or even to not apply the change.
|
| 9. |
Customization of apply
processes that maintain tables is done by specifying a DML handler
for the table. To configure a DML handler, you specify the table
name as the object and the type as 'TABLE', the procedure name to
be performed, and the type of operation (Insert/Update/Delete) for
which this handler should be used. If the handler is to be used
for all operations, then the procedure should be called 3 times,
once each for INSERT, UPDATE, and DELETE.
@ap_dmlhandler.sql

|
| 10. |
Whenever a DML Handler is configured, any columns needed by the
handler must be unconditionally logged at the source site. A supplemental
log group is specified on a table and when configured with the ALWAYS
clause meets this requirement. Unconditional supplemental logging
of a log group will cause the before and after image of each column
in the group to be logged everytime an update is made to a row.
This needs to be setup for both the ORCL and STRM instances.
connect sys/oracle@orcl.world as sysdba;
ALTER TABLE demo.orders
ADD SUPPLEMENTAL LOG
GROUP log_group_orders (part_number, status, delivery_date) ALWAYS;
connect sys/oracle@strm.world as sysdba;
ALTER TABLE demo.orders
ADD SUPPLEMENTAL LOG
GROUP log_group_orders (part_number, status, delivery_date) ALWAYS;
|
4 Streams In Action
| 1. |
Use the submit_orders script to simulate the ORDER ENTRY application.
This script expects a part number as input and will enqueue a message
into the ORDER_ENTRY database (ORCL). The apply with a message handler
will extract the message and turn it into an INSERT statement and
commit it to the DEMO.ORDERS table. Enter a value of 1111.
@submit_order.sql
|
| 2. |
You may notice that the Total messages enqueued number increased
by 2 after the refresh. The total messages enqueued for captured
LCRs includes both the DML changes and the commits.
Navigate to Network > Database > orcl.world > Distributed
> Streams > Capture. Click on the capture process
then click on the Statistics tab. Notice the value for Total Messages
Enqueued and the Enqueue Time. If the time is not current click
on the Refresh button until the time is current.
|
| 3. |
Navigate to Network > Database > strm.world > Distributed
> Streams > Apply. Click on the apply process then the
Statistics tab. Refresh this frame until current.
|
| 4. |
Connect to the STRM instance as DEMO and SELECT from the ORDERS
table, to verify the row was pushed across.
connect demo/demo@strm.world;
SELECT *
FROM orders;
|
| 5. |
Now that the inserted row from ORCL appears at the STRM site, we
can run the shipping application, accept_orders.sql, to ACCEPT the
orders and schedule the delivery date. This script updates all PENDING
rows to a status of Accepted and sets the delivery date 3 days from
now.
From the STRM instance as DEMO execute the script accept_orders.sql
to accept the order.
@accept_orders.sql
|
| 6. |
From the Order Entry (ORCL) instance connect as DEMO and SELECT
from the ORDERS table to verify the updated row is pushed accoss.
connect demo/demo@strm.world;
SELECT *
FROM orders;
|
| 7. |
We have seen the updated row appear at the ORCL site. Recall that
we customized the apply so that the logical change record (LCR)
was re-enqueued into the Streams queue for the notify application.
Use the script dequeue to extract the messages from the queue.
Dequeue accepted entries. Run script dequeue.sql as DEMO on the
ORACLE instance.
@dequeue.sql
|
| 8. |
Streams also can replicate DDL commands, such as CREATE TABLE,
or ALTER TABLE... add column commands.
Create a new table in the ORCL instance as DEMO. The table will
need to have a primary key.
connect demo/demo@orcl.world;
CREATE TABLE emp (
emp_no NUMBER PRIMARY KEY,
emp_name VARCHAR2(20));
|
| 9. |
From the STRM instance as DEMO describe the table and insert a
row. A commit is required for the row to be pushed.
connect demo/demo@strm.world;
set echo on
describe emp;
INSERT INTO emp VALUES (1234,'Streams');
COMMIT;
|
| 10. |
In the ORCL instance SELECT from the EMP table to verify the row
was pushed.
connect demo/demo@orcl.world;
SELECT *
FROM emp;
|
Module Summary
In this module, you should have learned how to:
Copyright © 2002 Oracle Corporation. All Rights Reserved.
Close Window
|