Commitment control

Through commitment control, your Java™ program has another level of control over changing a file. With commitment control turned on, transactions to a file are pending until they are either committed or rolled back. If committed, all changes are put to the file. If rolled back, all changes are discarded. The transaction can be changing an existing record, adding a record, deleting a record, or even reading a record depending on the commitment control lock level specified on the open().

The levels of commitment control are as follows:

You can use the startCommitmentControl() method to start commitment control. Commitment control applies to the AS400 connection. Once commitment control is started for a connection, it applies to all files opened under that connection from the time that commitment control was started. Files opened before commitment control is started are not under commitment control. The level of commitment control for individual files is specified on the open() method. Specify COMMIT_LOCK_LEVEL_DEFAULT to use the same level of commitment control as was specified on the startCommitmentControl() method.

For example:

                       // Create an AS400 object, the files exist on this
                       // server.
     AS400 sys = new AS400("mySystem.myCompany.com");

                       // Create three file objects
     SequentialFile myFile = new SequentialFile(sys, "/QSYS.LIB/MYLIB.LIB/MYFILE.FILE/%FILE%.MBR");
     SequentialFile yourFile = new SequentialFile(sys, "/QSYS.LIB/YOURLIB.LIB/YOURFILE.FILE/%FILE%.MBR");
     SequentialFile ourFile = new SequentialFile(sys, "/QSYS.LIB/OURLIB.LIB/OURFILE.FILE/%FILE%.MBR");

                       // Open yourFile before starting commitment control
                       // No commitment control applies to this file.  The
                       // commit lock level parameter is ignored because
                       // commitment control is not started for the connection.
     yourFile.setRecordFormat(new YOURFILEFormat());
     yourFile.open(AS400File.READ_WRITE, 0, AS400File.COMMIT_LOCK_LEVEL_DEFAULT);

                       // Start commitment control for the connection.
                       // Note: Any of the three files might be used for
                       // this call to startCommitmentControl().
     myFile.startCommitmentControl(AS400File.COMMIT_LOCK_LEVEL_CHANGE);

                       // Open myFile and ourFile
     myFile.setRecordFormat(new MYFILEFormat());

                       // Use the same commit lock level as specified
                       // when commitment control was started
     myFile.open(AS400File.WRITE_ONLY, 0, COMMIT_LOCK_LEVEL_DEFAULT);

     ourFile.setRecordFormat(new OURFILEFormat());
                       // Specify a different commit lock level than
                       // when commitment control was started
     ourFile.open(AS400File.READ_WRITE, 0, COMMIT_LOCK_LEVEL_CURSOR_STABILITY);

                       // write and update records in all three files
                     ....

                       // Commit the changes for files myFile and ourFile.
                       // Note that the commit commits all changes for the connection
               // even though it is invoked on only one AS400File object.
     myFile.commit();
                       // Close the files
     myFile.close();
     yourFile.close();
     ourFile.close();

                       // End commitment control
               // This ends commitment control for the connection.
     ourFile.endCommitmentControl();

                       // Disconnect since I am done using record-level access
     sys.disconnectService(AS400.RECORDACCESS);

The commit() method commits all transactions since the last commit boundary for the connection. The rollback() method discards all transactions since the last commit boundary for the connection. Commitment control for a connection is ended through the endCommitmentControl() method. If a file is closed before invoking the commit() or rollback() method, all uncommitted transactions are rolled back. All files opened under commitment control must be closed before the endCommitmentControl() method is called.

The following examples shows how to start commitment control, commit or roll back functions, and then end commitment control:

                       // ... assume the AS400 object and file have been
                       // instantiated.

                       // Start commitment control for *CHANGE
     aFile.startCommitmentControl(AS400File.COMMIT_LOCK_LEVEL_CHANGE);

                       // ... open the file and do several changes.  For
                       // example, update, add or delete records.

                       // Based on a flag either save or discard the
                       // transactions.
     if (saveChanges)
        aFile.commit();
     else
        aFile.rollback();

                       // Close the file
     aFile.close();

                       // End commitment control for the connection.
     aFile.endCommitmentControl();