Two samples are provided which demonstrate how WebSphere MQ can coordinate both WebSphere MQ updates and database updates within the same unit of work:
Figure 37 shows how the samples provided are used to update databases:
Figure 37. The database coordination samples
The programs read a message from a queue (under syncpoint), then, using the information in the message, obtain the relevant information from the database and update it. The new status of the database is then printed.
The program logic is as follows:
SQL cursors are used in the samples, so that reads from the databases (that is, multiple instances) are locked whilst a message is being processed, thus multiple instances of these programs can be run simultaneously. The cursors are explicitly opened, but implicitly closed by the MQCMIT call.
The single database sample (AMQSXAS0 or AMQ0XAS0) has no SQL CONNECT statements and the connection to the database is implicitly made by WebSphere MQ with the MQBEGIN call. The multiple database sample (AMQSXAG0 or AMQ0XAG0, AMQSXAB0 or AMQ0XAB0, and AMQSXAF0 or AMQ0XAF0) has SQL CONNECT statements, as some database products allow only one active connection. If this is not the case for your database product, or if you are accessing a single database in multiple database products, the SQL CONNECT statements can be removed.
The samples are prepared with the IBM DB2 database product, so they may need some modification to work with other database products.
The SQL error checking uses routines in UTIL.C and CHECKERR.CBL supplied by DB2. These must be compiled or replaced before compiling and linking.
The databases and tables must be created before the samples can be compiled. To create the databases, use the normal method for your database product, for example:
DB2 CREATE DB MQBankDB DB2 CREATE DB MQFeeDB
Create the tables using SQL statements as follows:
In C:
EXEC SQL CREATE TABLE MQBankT(Name VARCHAR(40) NOT NULL, Account INTEGER NOT NULL, Balance INTEGER NOT NULL, PRIMARY KEY (Account)); EXEC SQL CREATE TABLE MQBankTB(Name VARCHAR(40) NOT NULL, Account INTEGER NOT NULL, Balance INTEGER NOT NULL, Transactions INTEGER, PRIMARY KEY (Account)); EXEC SQL CREATE TABLE MQFeeTB(Account INTEGER NOT NULL, FeeDue INTEGER NOT NULL, TranFee INTEGER NOT NULL, Transactions INTEGER, PRIMARY KEY (Account));
In COBOL:
EXEC SQL CREATE TABLE MQBankT(Name VARCHAR(40) NOT NULL, Account INTEGER NOT NULL, Balance INTEGER NOT NULL, PRIMARY KEY (Account)) END-EXEC. EXEC SQL CREATE TABLE MQBankTB(Name VARCHAR(40) NOT NULL, Account INTEGER NOT NULL, Balance INTEGER NOT NULL, Transactions INTEGER, PRIMARY KEY (Account)) END-EXEC. EXEC SQL CREATE TABLE MQFeeTB(Account INTEGER NOT NULL, FeeDue INTEGER NOT NULL, TranFee INTEGER NOT NULL, Transactions INTEGER, PRIMARY KEY (Account)) END-EXEC.
Fill in the tables using SQL statements as follows:
EXEC SQL INSERT INTO MQBankT VALUES ('Mr Fred Bloggs',1,0); EXEC SQL INSERT INTO MQBankT VALUES ('Mrs S Smith',2,0); EXEC SQL INSERT INTO MQBankT VALUES ('Ms Mary Brown',3,0);
·
·
·
EXEC SQL INSERT INTO MQBankTB VALUES ('Mr Fred Bloggs',1,0,0); EXEC SQL INSERT INTO MQBankTB VALUES ('Mrs S Smith',2,0,0); EXEC SQL INSERT INTO MQBankTB VALUES ('Ms Mary Brown',3,0,0);
·
·
·
EXEC SQL INSERT INTO MQFeeTB VALUES (1,0,50,0); EXEC SQL INSERT INTO MQFeeTB VALUES (2,0,50,0); EXEC SQL INSERT INTO MQFeeTB VALUES (3,0,50,0);
·
·
·
The .SQC files (in C) and .SQB files (in COBOL) must be precompiled and bound against the appropriate database to produce the .C or .CBL files. To do this, use the normal method for your database product, as shown below.
db2 connect to MQBankDB db2 prep AMQSXAS0.SQC db2 connect reset db2 connect to MQBankDB db2 prep AMQSXAB0.SQC db2 connect reset db2 connect to MQFeeDB db2 prep AMQSXAF0.SQC db2 connect reset
db2 connect to MQBankDB db2 prep AMQ0XAS0.SQB bindfile target ibmcob db2 bind AMQ0XAS0.BND db2 connect reset db2 connect to MQBankDB db2 prep AMQ0XAB0.SQB bindfile target ibmcob db2 bind AMQ0XAB0.BND db2 connect reset db2 connect to MQFeeDB db2 prep AMQ0XAF0.SQB bindfile target ibmcob db2 bind AMQ0XAF0.BND db2 connect reset
The following sample commands use the symbol <DB2TOP>. <DB2TOP> represents the installation directory for the DB2 product.
/usr/lpp/db2_05_00
/opt/IBMdb2/V5.0
c:\sqllib
Copy the following files into a temporary directory:
<MQMTOP>/samp/xatm
<MQMTOP>\tools\c\samples\xatm
<DB2TOP>/samples/c
Build the object files for each .c file using the following compiler command for the platform that you are using:
xlc_r -I<MQMTOP>/inc -I<DB2TOP>/include -c -o <FILENAME>.o <FILENAME>.c
cc -Aa +z -I<MQMTOP>/inc -I<DB2TOP>/include -c -o <FILENAME>.o <FILENAME>.c
icc /c /I<MQMTOP>\tools\c\include /I<DB2TOP>\include <FILENAME>.c
cc -Aa -KPIC -mt -I<MQMTOP>/inc -I<DB2TOP>/include -c -o <FILENAME>.o <FILENAME>.c
cl /c /I<MQMTOP>\tools\c\include /I<DB2TOP>\include <FILENAME>.c
Build the amqsxag0 executable using the following link command for the platform that you are using:
xlc_r -H512 -T512 -L<DB2TOP>/lib -ldb2 -L<MQMTOP>/lib -lmqm util.o amqsxaf0.o amqsxab0.o amqsxag0.o -o amqsxag0
ld -E -L<DB2TOP>/lib -ldb2 -L<MQMTOP>/lib -lmqm -1c /lib/crt0.o util.o amqsxaf0.o amqsxab0.o amqsxag0.o -o amqsxag0
ld -E -L<DB2TOP>/lib -ldb2 -L<MQMTOP>/lib -lmqm -lc -lpthread -lcl /lib/crt0.o util.o amqsxaf0.o amqsxab0.o amqsxag0.o -o amqsxag0
ilink util.obj amqsxaf0.obj amqsxab0.obj amqsxag0.obj mqm.lib db2api.lib /out:amqsxag0.exe
cc -mt -L<DB2TOP>/lib -ldb2 -L<MQMTOP>/lib -lmqm -lmqmzse-lmqmcs -lthread -lsocket -lc -lnsl -ldl util.o amqsxaf0.o amqsxab0.o amqsxag0.o -o amqsxag0
link util.obj amqsxaf0.obj amqsxab0.obj amqsxag0.obj mqm.lib db2api.lib /out:amqsxag0.exe
Build the amqsxas0 executable using the following compile and link commands for the platform that you are using:
xlc_r -H512 -T512 -L<DB2TOP>/lib -ldb2 -L<MQMTOP>/lib -lmqm util.o amqsxas0.o -o amqsxas0
ld -E -L<DB2TOP>/lib -ldb2 -L<MQMTOP>/lib -lmqm -lc /lib/crt0.o util.o amqsxas0.o -o amqsxas0
ld -E -L<DB2TOP>/lib -ldb2 -L<MQMTOP>/lib -lmqm -lc -lpthread -lcl /lib/crt0.o util.o amqsxas0.o -o amqsxas0
ilink util.obj amqsxas0.obj mqm.lib db2api.lib /out:amqsxas0.exe
cc -mt -L<DB2TOP>/lib -ldb2-L<MQMTOP>/lib -lqm -lmqmzse -lmqmcs -lthread -lsocket -lc -lnsl -ldl util.o amqsxas0.o -o amqsxas0
link util.obj amqsxas0.obj mqm.lib db2api.lib /out:amqsxas0.exe
Additional information
If you are working on AIX or HP-UX and wish to access Oracle, use the xlc_r compiler and link to libmqm_r.a.
Before the samples can be run, the queue manager must be configured with the database product you are using. For information about how to do this, see WebSphere MQ System Administration Guide.
Messages must be in the following format to be read from a queue:
UPDATE Balance change=nnn WHERE Account=nnn
AMQSPUT can be used to put the messages on the queue.
The database coordination samples take two parameters:
Assuming that you have created and configured a queue manager for the single database sample called singDBQM, with a queue called singDBQ, you increment Mr Fred Bloggs's account by 50 as follows:
AMQSPUT singDBQ singDBQM
Then key in the following message:
UPDATE Balance change=50 WHERE Account=1
You can put multiple messages on the queue.
AMQSXAS0 singDBQ singDBQM
The updated status of Mr Fred Bloggs's account is then printed.
Assuming that you have created and configured a queue manager for the multiple-database sample called multDBQM, with a queue called multDBQ, you decrement Ms Mary Brown's account by 75 as follows:
AMQSPUT multDBQ multDBQM
Then key in the following message:
UPDATE Balance change=-75 WHERE Account=3
You can put multiple messages on the queue.
AMQSXAG0 multDBQ multDBQM
The updated status of Ms Mary Brown's account is then printed.
Messages must be in the following format to be read from a queue:
UPDATE Balance change=snnnnnnnn WHERE Account=nnnnnnnn
For simplicity, the Balance change must be a signed eight-character number and the Account must be an eight-character number.
The sample AMQSPUT can be used to put the messages on the queue.
The samples take no parameters and use the default queue manager. It can be configured to run only one of the samples at any time. Assuming that you have configured the default queue manager for the single database sample, with a queue called singDBQ, you increment Mr Fred Bloggs's account by 50 as follows:
AMQSPUT singDBQ
Then key in the following message:
UPDATE Balance change=+00000050 WHERE Account=00000001
You can put multiple messages on the queue.
AMQ0XAS0
Type in the name of the queue:
singDBQ
The updated status of Mr Fred Bloggs's account is then printed.
Assuming that you have configured the default queue manager for the multiple database sample, with a queue called multDBQ, you decrement Ms Mary Brown's account by 75 as follows:
AMQSPUT multDBQ
Then key in the following message:
UPDATE Balance change=-00000075 WHERE Account=00000003
You can put multiple messages on the queue.
AMQ0XAG0
Type in the name of the queue:
multDBQ
The updated status of Ms Mary Brown's account is then printed.