Set up a Db2 for i destination¶
The Replicator Service writes to Db2 for i (the IBM i database) through an ODBC driver on the Stelo VM. It creates the destination tables and their indexes, loads baselines and snapshots, and applies incremental changes with SQL, under commitment control.
Supported configurations¶
| Supported | |
|---|---|
| Database | Db2 for i on IBM i. Merge needs IBM i 7.1 or later. For a particular IBM i release, ask Stelo Support. |
| Driver on the Stelo VM | StarSQL (installed and licensed with SQDR), the IBM i Access ODBC Driver, or the IBM Db2 ODBC driver (Db2 Connect). See Databases. |
| Snapshot replication | Yes. |
| Incremental replication | Yes, from any source with a Capture Agent. |
| Apply methods | Batch, Sequence and Merge (see Choosing the apply method). |
| Authentication | Remote Credentials: an IBM i user profile and its password. |
| Encryption | SSL/TLS: see Db2 with StarSQL and Configure SSL on the IBM i (Db2 for i). |
Docker
The SQDR container includes all three drivers (see ODBC drivers in the container).
Prepare the database¶
The IBM i administrator creates the user profile the Replicator Service connects as, and the library (schema) for the destination tables. Two things matter most:
- The destination tables must be journaled, because the Replicator Service writes under commitment control (see Why the tables must be journaled).
- The simplest setup is a profile that owns the library and its journal, so it can create, change, empty and drop the tables it creates there without other grants.
The authorities follow from the SQL the Replicator Service runs (see What the Replicator Service runs on the database).
Create the user profile and the library¶
Run these commands as a security officer (or a profile with *SECADM and *ALLOBJ special authority), for example
from a 5250 session:
CRTUSRPRF USRPRF(SQDRUSER) PASSWORD(mypassword) USRCLS(*USER) TEXT('SQDR destination')
RUNSQL SQL('CREATE SCHEMA MYLIB') COMMIT(*NONE) NAMING(*SQL)
CHGOBJOWN OBJ(QSYS/MYLIB) OBJTYPE(*LIB) NEWOWN(SQDRUSER)
CHGOBJOWN OBJ(MYLIB/QSQJRN) OBJTYPE(*JRN) NEWOWN(SQDRUSER)
CHGOBJOWN OBJ(MYLIB/QSQJRN0001) OBJTYPE(*JRNRCV) NEWOWN(SQDRUSER)
Replace SQDRUSER, mypassword and MYLIB with yours. You can also run CREATE SCHEMA MYLIB from an SQL client
such as IBM i Access Client Solutions Run SQL Scripts. Why each step is needed: see
Authorities. See also A library created with CRTLIB,
Journal receivers, Tables you create yourself for
existing tables, and, if incremental groups replicate schema changes to a release older than 7.4 TR1 or 7.3 TR7,
ALTER processing.
StarSQL packages¶
StarSQL runs its SQL through SQL packages on the IBM i, in the library named by PkgColID in the connection string
(SQDR in the template). Bind them once with StarAdmin, as for a Db2 for i source: create the library (CRTLIB SQDR,
or use QGPL), connect StarAdmin with a profile that can create objects in it, and choose Bind. StarAdmin grants
*EXECUTE on the packages to *PUBLIC by default, which SQDRUSER needs. See
Connecting SQDR to a Db2 source: bind the StarSQL packages (StarAdmin).
If the IBM i is already a source of the same Stelo VM, the packages exist. The other two drivers don't use StarSQL
packages.
Network¶
The Stelo VM must reach the IBM i: StarSQL and the IBM Db2 ODBC driver connect with DRDA (the DDM server, port 446
by default, 448 for SSL), and the IBM i Access ODBC Driver through the host servers. Make sure they're started, for
example with STRTCPSVR SERVER(*DDM) and STRHOSTSVR SERVER(*ALL), and that a firewall lets their ports through (see
Host servers and ports and Ports).
Create the destination¶
Create the destination as described in Create a destination, with these choices:
-
Driver: StarSQL is installed and licensed with SQDR: there's nothing to install. For the IBM i Access ODBC Driver or the IBM Db2 ODBC driver, see Other Db2 for i drivers.
Docker
The SQDR container includes StarSQL, the IBM i Access ODBC Driver and the IBM Db2 ODBC driver.
-
Name: choose the DBMS Type iSeries.
-
Connection: choose ODBC Driver and the driver, then the Template for Db2 for i, and fill in the fields (see Connection string). With StarSQL, set
PkgColIDto the library of the StarSQL packages. -
Authentication: Remote Credentials with the user profile and its password. Click Verify.
- Tick Advanced to check the defaults: on the Default step, Object Schema is the library where new
subscriptions create their tables,
SQDRUSERby default; set it toMYLIB. See Defaults.
Verify tests the connection, not the authorities or the journaling (see Test the authorities).
Connection string¶
StarSQL¶
- Template:
HostName=myhostip;Port=446;Server=MYRDB;IsolationLevel=2;PkgColID=SQDR.Serveris the relational database name (RDB;WRKRDBDIREshows the local one). KeepIsolationLevel=2for sources used for incremental replication. PkgColIDnames the collection (on Db2 for i, the library) that holds the StarSQL packages.- SSL/TLS (Windows): add
Netlib=SQSSL.DLLand use the server's SSL port, for exampleHostName=myhostip;Netlib=SQSSL.DLL;Port=448;Server=MYRDB;IsolationLevel=2;PkgColID=SQDR.
Docker
Put the certificate authority's certificate in /var/sqdr/certs (see
ODBC drivers in the container). The Replicator Service adds
ICONV4UTF8=Y to StarSQL connection strings on Linux.
Bind the StarSQL packages¶
StarSQL runs its SQL through SQL packages on the Db2 system, in the collection named by PkgColID (on Db2 for i, a
library). They're bound once per collection, and every connection that uses the collection shares them. Some are
bound for you: the Add Capture Agent wizard binds them in the agent's control library or collection (see
Db2 for i and Db2 for LUW), and the SQDRC database that Stelo
Capture creates has them in STARSQL. Bind them yourself with StarAdmin for any other collection, for example for a
Db2 source without a Capture Agent, a Db2 for z/OS source, or a Db2 destination.
- Get StarAdmin. It's installed with Stelo Capture: Start menu SQDR Plus (64-bit) › Tools › StarAdmin
(see Programs and files). Without Stelo Capture, install it from its own
installer (ask Stelo Support): run
setup.exeas an administrator and accept the defaults. -
Collect the values:
Value What to enter Host The Db2 server's host name or IP address. Port The port Db2 listens on (446 for Db2 for i). Database name The relational database name (RDB) of the Db2 database. Package collection SQDR(orQGPLon Db2 for i), the value you'll use forPkgColID.User and password A Db2 user with authority to create and bind packages in that collection. -
On Db2 for i, create the library for the collection, unless you use
QGPL:CRTLIB SQDR. - Connect. Start StarAdmin and enter the values in the Connection Settings dialog. When it connects, it binds one package right away. If it can't connect, see the common error messages in Binding StarSQL Packages Using StarAdmin, correct the values and try again.
- Bind. Keep the default Package Settings and Grant Options (they grant
EXECUTEon the packages toPUBLIC), and select Command › Bind. If the summary reports errors, correct them (see the same document) and bind again; if you can't, send the whole summary to Stelo Support.
Then name the collection in the connection string of the source or destination, for example
Server=MYRDB;HostName=myhostip;Port=446;IsolationLevel=2;PkgColID=SQDR (IsolationLevel=2 for Db2 for i sources).
IBM i Access ODBC Driver¶
- Template:
System=mysystem;CommitMode=1;DefaultLibraries={QGPL,SQDR}.Systemis the IBM i's host name. KeepCommitMode=1(*CS) for sources used for incremental replication.DefaultLibrariesis optional. - Independent ASP (IASP): add
Database=with the database name (RDB entry) of the IASP, for exampleSystem=mysystem;CommitMode=1;DefaultLibraries={QGPL,SQDR};Database=IASP1. - The driver doesn't use StarSQL packages.
IBM Db2 ODBC driver¶
- Template:
Hostname=myhostip;Port=446;Database=MYRDB;Protocol=TCPIP;LongDataCompat=1;Authentication=SERVER.Databaseis the RDB name on Db2 for i, the database name on Db2 for LUW and the location name on Db2 for z/OS. UseAuthentication=ENCRYPTto encrypt the user ID and password. - LOB columns:
LongDataCompat=1is required to replicate LOB data correctly. For large LOB values, also set the driver'sLOBMaxColumnSizeand the service's Indeterminable-size column limit to the largest size you replicate. - SSL/TLS: add
Security=SSL, the server's SSL port, and the GSKit key database and stash file:SSLClientKeystoredb=C:\mySSLdir\mykeystore.kdb;SSLClientKeystash=C:\mySSLdir\mykeystore.sth. The account the Replicator Service runs as must be able to read the stash file. This driver uses its own key database, not the certificates in/var/sqdr/certsin the container. - The driver doesn't use StarSQL packages.
Other Db2 for i drivers¶
- IBM i Access ODBC Driver: install IBM i Access Client Solutions with its ODBC driver package. See IBM i Access ODBC Driver for its connection string.
- IBM Db2 ODBC driver: install IBM's Db2 client (Db2 Connect). See IBM Db2 ODBC driver for its connection string.
Group settings for Db2 for i¶
When you create an incremental group to this destination, choose the DML Replication method (see Apply):
- Batch (the default) and Sequence work with every IBM i release.
- Merge needs IBM i 7.1 or later and a key on every member (see
Tables without a usable key). It's the fastest method for high
volumes (see Choosing the apply method). Its merge tables are declared
temporary tables in
QTEMP, which need no authority.
Check the destination¶
After the first baseline of the incremental group (for snapshots, after the first run):
- Group status. On the Groups page, the group shows Actively monitoring and applying change data (see Group status). If a member failed, its replication events show the Db2 for i error.
-
Rows at the destination. Compare a table's row count with the source, for example in Run SQL Scripts:
SELECT COUNT(*) FROM MYLIB.MYTABLE; -
Changes. Change a few rows at the source; they're applied with the group's next poll (the Poll Interval; see Scheduling). Query the rows again to see them.
What the Replicator Service runs on the database¶
The authorities in Prepare the database follow from these statements:
| When | Statements |
|---|---|
| A subscription is saved or a baseline starts (depending on its Destination DDL Options) | DROP TABLE and CREATE TABLE for the destination table, named with its library. |
| Before a baseline or snapshot copies the data | Truncate table before replication runs DELETE FROM <table>, because Db2 for i releases up to 7.1 have no TRUNCATE. Delete existing data before replication deletes in batches of the ODBC commit interval: DELETE FROM <table> WHERE RRN(<table>) IN (SELECT RRN(<table>) FROM <table> ... FETCH FIRST n ROWS ONLY). See Baseline Replication Options. |
| During the copy | INSERT, with ODBC parameter arrays. Db2 for i has no native loader: Use native-loader function uses ODBC inserts. |
| Around the copy | CREATE INDEX and DROP INDEX for the index on the replication key (suffix IR) and the source indexes you replicate; ALTER TABLE ... ADD CONSTRAINT ... PRIMARY KEY and ALTER TABLE ... DROP PRIMARY KEY for Allow UPSERT members and replicated primary keys. See The key at the destination. |
| Applying changes with Batch or Sequence | INSERT, UPDATE ... WHERE <key> and DELETE ... WHERE <key>, one row at a time, committed per batch. |
| Applying changes with Merge | DECLARE GLOBAL TEMPORARY TABLE SESSION.SQDRWT_<id> (...) WITH REPLACE ON COMMIT PRESERVE ROWS NOT LOGGED (created in the job's QTEMP library, not journaled), a multi-row INSERT into it, then one MERGE INTO <table> .... It's emptied with DELETE FROM. See How Merge works. |
| With Merge In Place | A permanent merge table next to the destination table (or in the library of the Decoration), created, emptied with DELETE FROM and dropped like a destination table. It's journaled like any table created in that library. |
| ALTER processing | First CALL QSYS2.QCMDEXC('CHGJOB INQMSGRPY(*SYSRPYL)'), so that the job answers inquiry messages from the system reply list; then ALTER TABLE ... ADD COLUMN, ALTER COLUMN ... SET DATA TYPE, ALTER COLUMN ... DROP NOT NULL or SET NOT NULL, and DROP COLUMN. ADD Copy loads a work table (SESSION.SQDRCP_<id>, or a regular table SQDRCP_<id> next to the destination table when the group allows checkpoint/restart) and updates the destination table from it with a MERGE. |
Data types¶
The Replicator Service maps the source columns to the types the driver reports; see Data type mapping and its Per-database notes. For Db2 for i:
- Decimal precision. Db2
DECIMALhas at most 31 digits; a source number with more digits maps to a character column (see the default mapping). - Character data. Data from a single-byte or Unicode source can need more bytes in the destination's CCSID: use the inflation options or Unicode types (see Character data and the host variable error).
- IBM Db2 ODBC driver.
GRAPHIC,VARGRAPHICandDBCLOBare treated as Unicode character types, andLongDataCompat=1is needed for LOB data (see IBM Db2 ODBC driver). - RRN columns from a Db2 for i source are
BIGINT(see RRN and ROWID columns).
Limitations¶
- Unjournaled tables can't be written. See Prepare the database.
- No TRUNCATE. Emptying a table is a
DELETE, which is journaled row by row and slower than a truncate. For a very large table, clear it without row-by-row journaling instead: set the subscription to Use existing table (Use existing table for baseline), and add a destination before-processing statement on its Processing tab that clears the file member, for exampleCALL QSYS2.QCMDEXC('CLRPFM MYLIB/MYTABLE'). TheDELETEthat follows then has no rows to delete. Take care if the table is also the source of an incremental subscription:CLRPFMcan make the Capture Agent treat the table as altered, so that subscription has to be created again. - Merge needs IBM i 7.1 or later, and a key on every member.
- No column renames. ALTER processing generates no
RENAME COLUMNfor Db2 for i destinations. - Parallel baselines are available (see Parallel baselines with slices);
the helper column
SQDR_SLICEis kept after the baseline by default, because dropping a column rebuilds the table.
Troubleshooting¶
Problems and messages, with their causes and fixes, are in Troubleshooting › Db2 for i.
Related¶
- More about preparing a Db2 for i destination
- Connection strings: IBM i Access ODBC driver
- Prepare the IBM i (as a source)
- IBM documentation: CREATE SCHEMA, DECLARE GLOBAL TEMPORARY TABLE, MERGE