Step 4: Keeping Data in Memory
This step shows how to use multiple binding, as well as the advantage of using it. Multiple binding is used to get better performance when the application needs to keep in memory the objects it reads from the RDBMS.
In previous steps, data always went to the same address location when it was retrieved from the RDBMS. Therefore, each fetch operation overwrites the data previously fetched. If the application needs to keep this data in memory, it has to copy it to another location (this can lead to memory exhaustion problems).
To avoid this, the bindCol method is called as often as necessary to specify a new address location between each fetch. This is demonstrated by the following sample (see method readData in the complete code sample for this step).
The select query is executed as in previous steps:
static const char* selectStr = "select I from OPTIMS4" ;
if (!request->execute(selectStr)) {
IldDisplayError("Could not run select query : ", request) ;
Ending(dbms) ;
exit(1) ;
}
A first bindCol call is executed to get the first column value. Then, within the fetch loop, the column binding is changed so that each value fetched is stored in a new location:
IldUShort i = 0 ;
if (!request->bindCol((IldUShort)0, IldIntegerType, &values[i])) {
IldDisplayError("Could not bind column : ", request) ;
Ending(dbms) ;
exit(1) ;
}
while (request->fetch().hasTuple())
if (++i == nbVal) break ;
else
if (!request->bindCol((IldUShort)0, IldIntegerType, &values[i])) {
IldDisplayError("Could not bind column : ", request) ;
Ending(dbms) ;
exit(1) ;
}
Note
In this sample, only the first values in an array are retrieved. This is why the fetch loop is broken after a given number of rows. In a real application, memory used to store data is allocated dynamically as required. Data is then printed at the end of the program. |
Conclusion
This step demonstrated how to use the multiple binding method to keep in memory data read from the RDBMS. This is easy to use and avoids the possible memory exhaustion problems that can arise when copying the data to another application buffer.
See source code.