24
Wed, Apr
0 New Articles

Building Commitments

Commentary
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

The AS/400 provides us with some of the best state-of-the-art tools for building complex business applications. Its integrated relational database, high-level languages (HLLs), and robust architecture allow us to tie together information, even across multiple machines. It's an architecture with tremendous power.

But all that power could be extremely dangerous in the hands of a novice programmer or a careless user. Even the best programmers in the best programming shops will tell you stories of terror when, on one fateful day, their programs failed in the middle of a complex updating process across multiple files. What happened? The user kicked out the terminal plug, or the system lost power. The result? Chaos! Some files were updated, and some were not. And, if the application was running across two or more machines, the problems were multiplied a thousandfold.

If potential accidents like this worry you, you should consider using OS/400's commitment control function. It places the control of multiple database updates directly with the programmer; it guarantees the complete process; and it even allows the programmer or user to change course in midstream and back out the transactions. Finally, this control mechanism isn't something tacked onto OS/400 but an integrated function of the operating system.

In this article, I'll show you how to use commitment control. I'll use some of the basic concepts I covered in my journaling article (see "Journal Management on the AS/400," MC, March 1996) and extend them to the COMMIT function. I'll discuss the logical unit of work (LUW) and how it changes the way we look at file updates and program logic. When I'm through, you should understand how to implement commitment control in your RPG programs and see the advantages it offers.

In order to understand how commitment control works, you have to learn the lingo. Commitment control uses a theoretical construct, an LUW, to orchestrate its control over database updates. An LUW is defined as a group of individual changes to objects on the system that should appear as a single atomic change to the user.

For example, you could conceive of an LUW embodied by the old phrase "robbing Peter to pay Paul." To the user, this LUW conjures up a simple image of removing a dollar from one person's pocket and placing it in the wallet of another. But if you write a program to perform this operation, you actually end up with six functioning operations: 1) retrieving the wallet record from Peter's database, 2) subtracting the dollar from Peter's wallet, 3) retrieving the wallet record from Paul's database, 4) adding the dollar to Paul's wallet, 5) writing Peter's wallet record back to Peter's database, and 6) writing Paul's wallet record back to Paul's database.

As defined by commitment control, all six actions are bundled together as a single LUW. When the sixth-and final-operation is performed, an explicit COMMIT operation would be issued, and the LUW would be completed. If for some reason this LUW was not completed, a ROLLBACK operation would be automatically issued, the transaction would be reversed, and the dollar would be magically replaced in Peter's wallet. Under commitment control, a programmer can perform an explicit COMMIT or ROLLBACK command, treating the entire six operations as a single atomic change.

LUWs, which can get quite complex, can be any of the following:

o Inquiries in which no database file changes.

o Simple transactions in which one database file changes.

o Complex transactions in which multiple database files change.

o Distributed transactions in which multiple database files on multiple systems change.

There are a couple of prerequisites to using commitment control: You have to create a journal receiver and a journal for the files placed under commitment control, and you have to initiate the journaling process. In the sample program, I'll use two files: PETERPF and PAULPF. To log these files, I'll create journal receiver PPMRCV0001 using the Create Journal Receiver (CRTJRNRCV) command shown in 1.

There are a couple of prerequisites to using commitment control: You have to create a journal receiver and a journal for the files placed under commitment control, and you have to initiate the journaling process. In the sample program, I'll use two files: PETERPF and PAULPF. To log these files, I'll create journal receiver PPMRCV0001 using the Create Journal Receiver (CRTJRNRCV) command shown in Figure 1.

Next, I'll create journal PPMJRN to control the journal receiver using the Create Journal (CRTJRN) command (see 2).

Next, I'll create journal PPMJRN to control the journal receiver using the Create Journal (CRTJRN) command (see Figure 2).

Finally, I'll place the files PETERPF and PAULPF under the control of the PPMJRN journal by using the Start Journal Physical File (STRJRNPF) command, as shown in 3.

Finally, I'll place the files PETERPF and PAULPF under the control of the PPMJRN journal by using the Start Journal Physical File (STRJRNPF) command, as shown in Figure 3.

Normally at this point, I'd save the PETERPF and PAULPF files; without doing so, I couldn't apply journal entries back against a restored PETERPF or PAULPF. It's certainly something you need to do before you turn a production system over to commitment control. But here, since this is just an example, it's not necessary.

Take a look at the CL program PTR2PAULC (4). PTR2PAULC is composed of three functioning elements: a Start Commitment Control (STRCMTCTL) command, a call to RPG program PTR2PLR passing an AMT parameter, and an End Commitment Control (ENDCMTCTL) statement. The program is designed to start OS/400's commitment control function against the files described in the PTR2PLR program, to pass an amount (in this example, $8.20) to that program, and then to end the commitment control function.

Take a look at the CL program PTR2PAULC (Figure 4). PTR2PAULC is composed of three functioning elements: a Start Commitment Control (STRCMTCTL) command, a call to RPG program PTR2PLR passing an AMT parameter, and an End Commitment Control (ENDCMTCTL) statement. The program is designed to start OS/400's commitment control function against the files described in the PTR2PLR program, to pass an amount (in this example, $8.20) to that program, and then to end the commitment control function.

The LCKLVL parameter of the STRCMTCTL command describes how commitment control should handle record locking. There are three LCKLVL options: *CHG, *CS, and *ALL. By specifying *CHG, you can protect all the processed records from simultaneous changes by other programs that might be running against these same files. By comparison, the option *CS ensures that other jobs are not even able to read a record accessed for update. The *ALL option protects and coordinates these files running under commitment control from other commitment control processes running in other programs. Since this application is simple, however, I'll keep the file LCKLVL parameter simple, too.

I've illustrated the essence of the commitment control implementation in the PTR2PLR RPG program (see 5). The F-spec continuation line using the COMIT statement identifies that the PETERPF and PAULPF files are to be processed under the guiding hand of a commitment control LUW.

I've illustrated the essence of the commitment control implementation in the PTR2PLR RPG program (see Figure 5). The F-spec continuation line using the COMIT statement identifies that the PETERPF and PAULPF files are to be processed under the guiding hand of a commitment control LUW.

This program is really something of a programmer's joke, though it illustrates the processes of an LUW. The program literally takes an amount out of the PETERPF wallet record and puts it into the PAULPF wallet record. However, there's something strange about this process.

Notice that testing PETER's wallet for a negative amount doesn't occur until after the UPDAT of the record. How can this be? The answer is that the COMMIT process is the final arbiter. Until COMMIT is issued, the LUW is incomplete and the multiple individual transactions remain suspended. Even if the system crashes and burns, neither transaction against PETER or PAUL is processed. When the system restarts, it will automatically roll back the transactions to the initial state. OS/400 keeps track of the actual state of the records in the journal receiver I created. I'll examine that receiver in just a moment.

What you've seen in this RPG program is a new model for record processing. I've built an atomic LUW process that makes sense unto itself. When you think about the power of commitment control, you can easily imagine the levels of process sophistication that I could devise. (I could, for example, build a process that robs PETER to pay PAUL if-and only if-PAUL first pays MARY the total royalties due from their last record deal, which she, in turn, has already borrowed from PETER.... And on and on.) Finally, these LUW orchestrations don't stop with the local OS/400 database. Under V3R1, LUW can be extended across multiple databases on multiple AS/400s, guaranteeing that LUWs are consistent across the distributed resources of the organization's information system. The process by which this is accomplished is called two-phase commitment control (see the sidebar, "Two-phase Commit: OS/400 Democracy at Work").

Now, I'll test this bit of code to show you how commitment control and journaling pull everything together. I'll start by initializing the PETER database wallet record with a value of $9.00 and the PAUL database wallet record with a value of $10.00. Then, I'll call the PTR2PAULC program twice. The first time, the program should decrease Peter's wallet by $8.20 (leaving $.80) and increase Paul's wallet by $8.20 (for a total of $18.20). Of course, the program does this admirably.

Theoretically, if I were to run this program a second time without commitment control, Peter's wallet would be decreased by another $8.20, leaving him with a negative balance (-$7.40), while Paul's would skyrocket to $26.40. If the commitment control statements function properly, however, the program will test Peter's wallet for a negative amount; if the program detects a negative amount, the program will roll back the previous transaction. Indeed, this is exactly how the program functions. Under commitment control, then, it doesn't matter how many times I run the program; Peter's amount never becomes negative.

How does this work? The answer resides in the PPMJRN journal. If I key in the Display Journal (DSPJRN) command for PPMJRN, I'll see the entries shown in 6. PPMJRN shows each step of the LUW, starting with Sequence 22 and ending with Sequence 31. The Code column for Sequence 22 shows C, indicating a commitment process, and the Type column shows SC, indicating a start commitment process. This is the actual beginning of our LUW.

How does this work? The answer resides in the PPMJRN journal. If I key in the Display Journal (DSPJRN) command for PPMJRN, I'll see the entries shown in Figure 6. PPMJRN shows each step of the LUW, starting with Sequence 22 and ending with Sequence 31. The Code column for Sequence 22 shows C, indicating a commitment process, and the Type column shows SC, indicating a start commitment process. This is the actual beginning of our LUW.

Thereafter, Sequences 23-26 show a series of record transactions (Code R). Type UB contains a before-image of the record, and UP contains an after-image. These are the record updates corresponding to the UPDAT op codes in the RPG program in 5. Note that starting commitment control automatically turns on before and after image record logging, even though I didn't specify it when I set up the journaling criteria.

Thereafter, Sequences 23-26 show a series of record transactions (Code R). Type UB contains a before-image of the record, and UP contains an after-image. These are the record updates corresponding to the UPDAT op codes in the RPG program in Figure 5. Note that starting commitment control automatically turns on before and after image record logging, even though I didn't specify it when I set up the journaling criteria.

Sequences 27-30 show the rollback journal entries. The Type BR coded transactions are "Before Rollback" images of the transaction; the UR are "After Rollback" images of the transaction.

I've shown the actual detail display of the records-sequence by sequence-in 7. Notice that Paul's wallet did, in fact, skyrocket to $26.40 in the BR transaction but then reverted to $18.20 after the rollback. Conversely, Peter's wallet did briefly go to -$7.40 but was then restored to $.80.

I've shown the actual detail display of the records-sequence by sequence-in Figure 7. Notice that Paul's wallet did, in fact, skyrocket to $26.40 in the BR transaction but then reverted to $18.20 after the rollback. Conversely, Peter's wallet did briefly go to -$7.40 but was then restored to $.80.

Finally, back in 6, the final journal entry sequence 31 shows a Code C, indicating the end of our LUW. This second Code C is important because it identifies the commitment boundary of the operation. Had the process been interrupted during this update, OS/400 would have restored the transactions up to the last completed commitment boundary, thus guaranteeing that all LUWs were complete. If for some reason the COMMIT or the ROLLBACK is not performed, the system issues error message CPA8350 ENDCMTCTL requested with changes pending. (RB C CM). The system then suspends the transactions until someone manually performs a rollback or a commit.

Finally, back in Figure 6, the final journal entry sequence 31 shows a Code C, indicating the end of our LUW. This second Code C is important because it identifies the commitment boundary of the operation. Had the process been interrupted during this update, OS/400 would have restored the transactions up to the last completed commitment boundary, thus guaranteeing that all LUWs were complete. If for some reason the COMMIT or the ROLLBACK is not performed, the system issues error message CPA8350 ENDCMTCTL requested with changes pending. (RB C CM). The system then suspends the transactions until someone manually performs a rollback or a commit.

So, that's all there is to it. In this simple program, I showed you the entire process of setting up and implementing commitment control on the AS/400. You can use this basic program as a model for implementing commitment control in your shops. Most importantly, I showed you how commitment control extends the power of program updates to the construct of the LUW. The LUW offers you a more logical and powerful means of managing the internal flow of your program logic and gives you a finer control over the updating process.

Commitment control significantly extends the power of OS/400. If you want to learn more about it, you should check out the AS/400 Backup and Recovery ? Advanced manual. Inside, you'll find information about some other features of commitment control and some sample programs that explain many of this tool's rigorous features. Once you've walked through a couple of examples, you'll see how easy it really is to live up to your commitments.

Thomas M. Stockwell is a senior technical editor for Midrange Computing. He can be reached by E-mail at This email address is being protected from spambots. You need JavaScript enabled to view it..

Reference

OS/400 Backup and Recovery ? Advanced (SC41-3305, CD-ROM QBKALF00).


Building Commitments

Two-phase Commit: OS/400 Democracy at Work

Building a commitment control scenario across multiple AS/400s might represent a serious programming nightmare without V3R1's two-phase commitment control. How can the programmer be certain that all the resources of multiple machines are available at the precise moment an update is to occur? What if two machines are ready, but the third is unavailable? Two-phase commit addresses this problem.

Under two-phase commit, the system performs the commit operation in two waves: a prepare wave and a commit wave. During the prepare wave, the AS/400 that is initiating the update informs all the other machines that an LUW is about to take place. "Are we ready?" it asks. The machines involved then vote! The initiating machine tallies these votes and then decides what course of action to take. If the prepare wave completes successfully and all participants vote ready, the initiating machine instructs all the other resource managers to commit the LUW. If the prepare wave does not complete successfully, all the resource managers are instructed to roll back the LUW.


Building Commitments

Figure 1: Create Journal Receiver Command

 CRTJRNRCV JRNRCV(MYLIB/PPMRCV0001) THRESHOLD(5000) 
Building Commitments

Figure 2: Create Journal Command

 CRTJRN JRN(MYLIB/PPMJRN) JRNRCV(MYLIB/PPMRCV0001) 
Building Commitments

Figure 3: Start Journal Physical File Command

 STRJRNPF FILE(MYLIB/PETERPF) JRN(MYLIB/PPMJRN) STRJRNPF FILE(MYLIB/PAULPF) JRN(MYLIB/PPMJRN) 
Building Commitments

Figure 4: CL Program PTR2PAULC

 PGM /* Set a variable AMT to the value of $ 8.20 */ DCL VAR(&AMT) TYPE(*DEC) LEN(10 2) VALUE(8.20) /* Start Commitment Control with a lock level of *CHG */ STRCMTCTL LCKLVL(*CHG) TEXT('Start Commitment Control') /* Run the RPG Program to rob from Peter to pay Paul */ CALL PTR2PLR PARM(&AMT) /* End Commitment Control */ ENDCMTCTL /* End Commitment Control */ ENDPGM 
Building Commitments

Figure 5: RPG Program PTR2PLR

 FPETERPF UF E K DISK F KCOMIT FPAULPF UF E K DISK F KCOMIT C* Read the amount that Paul wants C *ENTRY PLIST C PARM AMT 102 C* C MOVEL'WALLET' WALLET 10 C* Get out Peter's Wallet C WALLET CHAINPTRWAL 64 C* C* Get out Paul's Wallet C WALLET CHAINPAULWAL 65 C* C* Take the money out of Peter's Wallet C SUB AMT PTRAMT C* C* Put the money into Paul's Wallet C ADD AMT PALAMT C* C* Put Peter's Wallet back in his pocket C UPDATPTRWAL C* C* Put Paul's Wallet back in his pocket C UPDATPAULWAL C* C* Now your conscience is acting up. What if Peter didn't C* have enough money to pay PAUL? Better check. C PTRAMT IFLT *ZERO C* C* If Peter doesn't have enough, then roll back the C* transaction C ROLBK C* C* Otherwise, go ahead and finalize the transaction C ELSE C COMIT C ENDIF C* C* Now, get out of there. C MOVE *ON *INLR 
Building Commitments

Figure 6: Journal Entries Created by the Programs



Building Commitments

Figure 7: Detail Journal Entries Showing Sequence of Events

 Object . . . . . . . : PAULPF Library . . . . . . : MYLIB Member . . . . . . . : PAULPF Sequence . . . . . . : 27 Code . . . . . . . . : R - Operation on specific record Type . . . . . . . . : BR - Update, before-image for rollback Entry specific data Column *...+....1....+....2....+....3....+....4....+....5 00001 '0000002640WALLET ' Object . . . . . . . : PAULPF Library . . . . . . : MYLIB Member . . . . . . . : PAULPF Sequence . . . . . . : 28 Code . . . . . . . . : R - Operation on specific record Type . . . . . . . . : UR - Update, after-image for rollback Entry specific data Column *...+....1....+....2....+....3....+....4....+....5 00001 '0000001820WALLET ' Object . . . . . . . : PETERPF Library . . . . . . : MYLIB Member . . . . . . . : PETERPF Sequence . . . . . . : 29 Code . . . . . . . . : R - Operation on specific record Type . . . . . . . . : BR - Update, before-image for rollback Entry specific data Column *...+....1....+....2....+....3....+....4....+....5 00001 '000000074}WALLET ' Object . . . . . . . : PETERPF Library . . . . . . : MYLIB Member . . . . . . . : PETERPF Sequence . . . . . . : 30 Code . . . . . . . . : R - Operation on specific record Type . . . . . . . . : UR - Update, after-image for rollback Entry specific data Column *...+....1....+....2....+....3....+....4....+....5 00001 '0000000080WALLET ' 
Thomas Stockwell

Thomas M. Stockwell is an independent IT analyst and writer. He is the former Editor in Chief of MC Press Online and Midrange Computing magazine and has over 20 years of experience as a programmer, systems engineer, IT director, industry analyst, author, speaker, consultant, and editor.  

 

Tom works from his home in the Napa Valley in California. He can be reached at ITincendiary.com.

 

 

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: