In Section 8.2, we saw how to use the caching mechanism of class RWDBInserter. With class RWDBReader, we can use a similar caching mechanism when receiving data from a database server. To enable the caching features of RWDBReader, we pass a cache size of n > 1 to its constructor. The following example shows how to construct a reader with a cache size of 5.
RWDBConnection aConn = aDb.connection(); RWDBReader reader = aSelector.reader(aConn, 5); //1 while (reader()) //2 { reader >> aInt; //3 //process aInt }
In //1, we produce reader from an RWDBSelector, requesting that it use a cache size of 5. In //2, we use operator() of RWDBReader to fetch data from the database server; in this case, it fetches five rows, since we set the cache size to 5.
In //3, we shift the first row of the reader's cache into the variable aInt. After processing the value, we return to step //2, where operator() is called a second time. On this second call to operator(), the reader returns without a network hit to fetch data. We then shift the second row in the reader's cache into the application variable aInt in //3.
This pattern is repeated until the sixth call to operator(). At the time of this call, the reader has exhausted its internal cache of rows, and must request a new set of rows from the database server. By using the reader in this fashion, the reader must make only one network trip for every five rows read.
©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.