Using record blocking

The AS400File class uses record blocking to improve performance:

  • If the file is opened for read-only access, a block of records is read when the Java program reads a record. Blocking improves performance because subsequent read requests may be be handled without accessing the server. Little performance difference exists between reading a single record and reading several records. Performance improves significantly if records can be served out of the block of records cached on the client.

    The number of records to read in each block can be set when the file is opened. For example:

                       // Create an AS400 object, the file exists on this
                       // AS/400.
     AS400 sys = new AS400("mySystem.myCompany.com");

                       // Create a file object that represents the file
     SequentialFile myFile = new SequentialFile(sys, "/QSYS.LIB/MYLIB.LIB/MYFILE.FILE/%FILE%.MBR");

                       // Assume that the AS400FileRecordDescription class
                       // was used to generate the code for a subclass of
                       // RecordFormat that represents the record format
                       // of file MYFILE in library MYLIB.  The code was
                       // compiled and is available for use by the Java
                       // program.
     RecordFormat recordFormat = new MYFILEFormat();

                       // Set the record format for myFile.  This must
                       // be done prior to invoking open()
     myFile.setRecordFormat(recordFormat);

                       // Open the file.  Specify a blocking factor of 50.
     int blockingFactor = 50;
     myFile.open(AS400File.READ_ONLY, blockingFactor, AS400File.COMMIT_LOCK_LEVEL_NONE);

                       // Read the first record of the file.  Because
                       // a blocking factor was specified, 50 records
                       // are retrieved during this read() invocation.
     Record record = myFile.readFirst();
     for (int i = 1; i < 50 && record != null; i++)
     {

       // The records read in this loop will be served out of the block of
       // records cached on the client.
       record = myFile.readNext();
     }
                     ....

                       // Close the file since I am done using it
     myFile.close();

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

  • If the file is opened for write-only access, the blocking factor indicates how many records are written to the file at one time when the write(Record[]) method is invoked.

    For example:

                       // Create an AS400 object, the file exists on this
                       // AS/400.
     AS400 sys = new AS400("mySystem.myCompany.com");

                       // Create a file object that represents the file
     SequentialFile myFile = new SequentialFile(sys, "/QSYS.LIB/MYLIB.LIB/MYFILE.FILE/%FILE%.MBR");

                       // Assume that the AS400FileRecordDescription class
                       // was used to generate the code for a subclass of
                       // RecordFormat that represents the record format
                       // of file MYFILE in library MYLIB.  The code was
                       // compiled and is available for use by the Java
                       // program.
     RecordFormat recordFormat = new MYFILEFormat();

                       // Set the record format for myFile.  This must
                       // be done prior to invoking open()
     myFile.setRecordFormat(recordFormat);

                       // Open the file.  Specify a blocking factor of 50.
     int blockingFactor = 50;
     myFile.open(AS400File.WRITE_ONLY, blockingFactor, AS400File.COMMIT_LOCK_LEVEL_NONE);

                       // Create an array of records to write to the file
     Record[] records = new Record[100];
     for (int i = 0; i < 100; i++)
     {
                       // Assume the file has two fields,
                       // CUSTNAME and CUSTNUM
       records[i] = recordFormat.getNewRecord();
       records[i].setField("CUSTNAME", "Customer " + String.valueOf(i));
       records[i].setField("CUSTNUM", new Integer(i));
     }

                       // Write the records to the file.  Because the
                       // blocking factor is 50, only two trips to the
                       // AS/400 are made with each trip writing 50 records
     myFile.write(records);

                     ....

                       // Close the file since I am done using it
     myFile.close();

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

  • If the file is opened for read-write access, no blocking is done. Any blocking factor specified on open() is ignored.


[ Legal | AS/400 Glossary ]