• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • DB2 for i 7.2 Features And Fun: Part 2

    July 23, 2014 Michael Sansoterra

    In the first tip of this series, I covered three new features introduced in DB2 for i 7.2: ALTER TRIGGER, TRUNCATE TABLE, and the ability to vary the fractional precision of a TIMESTAMP column. This tip continues the adventure by exploring a few more new features in IBM i 7.2 (in no particular order).

    KEEP IN MEMORY Clause For Tables And Index

    When creating a new index or table, a new KEEP IN MEMORY clause is available to tell DB2 to keep the data in main storage when possible. (A table or index cached in this manner should, of course, result in a performance increase.) This clause is also available with ALTER TABLE and only requires a value of YES or NO.

    Examples:

    -- Change existing table to be in main storage
    ALTER TABLE ADVWORKS.TRANSACTIONHISTORY
    ALTER KEEP IN MEMORY YES;
    
    -- Create index for ORDER TRANSACTIONS
    CREATE INDEX ADVWORKS.IDX_TRANSACTIONHISTORY__ORDERS
    ON ADVWORKS.TRANSACTIONHISTORY (PRODUCTID,MODIFIEDDATE DESC)
    WHERE TRANSACTIONTYPE='ORDER'
    KEEP IN MEMORY YES;
    

    For production tables and indexes with this option, a database or system administrator should monitor to make sure available memory is sufficient for the data growth.

    This feature can be helpful with “special” workloads, such as month end, when other users aren’t on the system and available memory is generally higher than usual. This option is allowed to be specified on temporary tables and could benefit a scenario such as month end processing, if the temp table is used frequently throughout the month end process:

    DECLARE GLOBAL TEMPORARY TABLE MONTH_END_SALES
    (ORDER_ID INT NOT NULL,
     PRODUCT_ID INT NOT NULL,
    ...more columns...
     ORDER_STAMP TIMESTAMP(0),
     SHIP_STAMP TIMESTAMP(0),
     QTY DEC(9,3),
     EXT_AMOUNT DEC(19,4))
    WITH REPLACE
    KEEP IN MEMORY YES;
    

    In my testing, I created a table and loaded it with more data than available RAM and there were no error messages so you shouldn’t have to worry about an application failure. I imagine in a production environment, though, this “overload” situation will cause performance to drag.

    Autonomous Procedures

    If you’re using commitment control in your applications, you’ll like this one. An autonomous procedure is one that will COMMIT data changes independently of any transaction processing the caller is performing.

    Consider the CREATE_ORDER procedure below that inserts data, calls a procedure that does email confirmation and audit logging functions, does some more work, and then commits or rolls back the changes:

    -- Procedure CREATE_ORDER
    CREATE OR REPLACE PROCEDURE ADVWORKS.CREATE_ORDER (@ORDER_ID INT)
    BEGIN
        SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
    
        INSERT INTO ADVWORKS.ORDER_HEADER 
        VALUES (@ORDER_ID,1,CURRENT_TIMESTAMP);
    
        -- Send an e-mail, create a log entry in the db
        CALL ADVWORKS.SEND_EMAIL_CONF (@ORDER_ID,'receipient@email.com');
    
        -- More work is done here
    
        -- Test if something bad happened
        IF @ERROR_ID>0 THEN
            ROLLBACK;
        ELSE 
            COMMIT;
        END IF;
    END;
    

    In prior releases, the SEND_EMAIL_CONF procedure’s work would be rolled back if the caller rolled back its work. But now, the AUTONOMOUS keyword can be used on a procedure definition to indicate that all changes should be committed independently of the caller:

    -- Procedure SEND_EMAIL_CONF
    CREATE OR REPLACE PROCEDURE ADVWORKS.SEND_EMAIL_CONF 
    (@ORDER_ID INT,@EMAIL VARCHAR(64))
    AUTONOMOUS
    BEGIN
        INSERT INTO ADVWORKS.ORDER_INFO
        VALUES (@ORDER_ID,@EMAIL,CURRENT_TIMESTAMP);
        -- Send an e-mail here
    END;
    

    Since procedure SEND_EMAIL_CONF is defined as autonomous, its committed data can stay “as is” in DB2, even if calling procedure CREATE_ORDER opts to ROLLBACK thereafter. In the above example, the ORDER_INFO table will still have a newly inserted row if the transaction started in procedure CREATE_ORDER is rolled back.

    Sometimes it just happens within a transaction that you want some remnants of data left in the database after a rollback. Depending on the order of operations and complexity of the app, it’s often a pain in the neck to engineer code to work this way. The AUTONOMOUS keyword allows developers to get around this conundrum without creating spaghetti code.

    If an autonomous procedure ends with an error, its changes will not be committed. Also, autonomous procedures may not return dynamic result sets.

    CHECK CONSTRAINT VIOLATION Clause

    Check constraints play a critical role in maintaining database integrity by protecting columns in the table from being populated with invalid values. Consider this simple PO_HEADER table definition:

    CREATE TABLE data.PO_HEADER (
    PO_ID     INT NOT NULL,
    PO        VARCHAR(12) NOT NULL,
    VENDOR_ID INT NOT NULL,
    PO_DATE   DATE NOT NULL DEFAULT CURRENT_DATE,
    EMAIL     VARCHAR(64) NOT NULL 
    DEFAULT 'purchasing_service@mydomain.com');
    

    Here is an example of a CHECK CONSTRAINT that attempts to do minimal validation of an email address:

    ALTER TABLE data.PO_HEADER
    ADD CONSTRAINT data.ck_PO__EMAIL_VALIDATION
    CHECK (EMAIL LIKE '%@%.%')
    

    If an operation attempts to insert or update data in column EMAIL that does not match the constraint’s predicate, the check constraint will cause the data modification to fail.

    However, there may be cases when you don’t want bad data in your database, but you don’t want an insert/update operation to fail either if a certain check constraint is violated. For example, if you have an external system feeding new purchase orders into your application, you may want to allow a PO to be created on your system even if the email address is invalid. (Sometimes fixing a problem is easier once it’s in your db instead of trying to get an external system to fix their data and send it again.)

    The new VIOLATION clause can be used to allow an INSERT or UPDATE operation to succeed and tell DB2 what to use in place of the bad value:

    ALTER TABLE data.PO_HEADER
    ADD CONSTRAINT data.ck_PO__EMAIL_VALIDATION 
    CHECK (EMAIL LIKE '%@%.%')
    ON INSERT VIOLATION SET EMAIL=DEFAULT
    ON UPDATE VIOLATION PRESERVE EMAIL
    

    In this case, if an INSERT violates the check constraint rule, the EMAIL column’s default value (purchasing_service@mydomain.com) will be inserted instead of the supplied value. Currently, the DEFAULT value is the only allowed value. The column name supplied on the violation clause must be included in the CHECK clause’s rule definition.

    Likewise, for an update, “ON UPDATE VIOLATION PRESERVE EMAIL” instructs DB2 to leave the existing value in the column instead of failing the update operation.

    Michael Sansoterra is a DBA for Broadway Systems in Grand Rapids, Michigan. Send your questions or comments for Mike via the IT Jungle Contact page.

    RELATED STORY

    DB2 for i 7.2 Features and Fun, Part 1



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    NGS:  NGS-IQ is the complete IBM i query, reporting, and analytics solution.
    System i Developer:  Upgrade your skills at the RPG & DB2 Summit in Minneapolis, Sept 30 - Oct 2.
    COMMON:  Join us at the COMMON 2014 Fall Conference & Expo in Indianapolis, Oct 27-29

    More IT Jungle Resources:

    System i PTF Guide: Weekly PTF Updates
    IBM i Events Calendar: National Conferences, Local Events, and Webinars
    Breaking News: News Hot Off The Press
    TPM @ EnterpriseTech: High Performance Computing Industry News From ITJ EIC Timothy Prickett Morgan

    IBM i Community-Minded Planning IBM Readies More Power8 Iron For Launch

    Leave a Reply Cancel reply

Volume 14, Number 16 -- July 23, 2014
THIS ISSUE SPONSORED BY:

CCSS
Valence Framework for IBM i
System i Developer

Table of Contents

  • DB2 for i 7.2 Features And Fun: Part 2
  • Beefing Up The Job Log
  • Porting A User Profile From One IBM i Machine To Another

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle