Asynchronous Usage Example
As you will discover, the asynchronous mechanism can be used in many powerful ways. At this point, however, let's look at a very simple example just to see how it works.
Example 21 – Asynchronous usage
int
main() {
RWDBDatabase aDb = RWDBManager::database(
"SYBASE_CT",
"sybase_server",
"sybase_user",
"password",
"DEMO_DB"
);
RWDBConnection asyncConn =
aDb.connection(RWDBConnection::Asynchronous); //1
RWDBResult result =
aSyncConn.executeSql(“create table X(y int)”); //2
while (! result.isReady()){ //3
// Do something useful here
cout << "Waiting for completion " << endl; //4
}
assert(result.isValid()); //5
return 0;
}
On
//1 an asynchronous connection is obtained from the database object. On
//2 an SQL statement is executed using the
executeSql() method. This method has been invoked asynchronously by using the asynchronous connection obtained in
//1. Then
//3 checks whether the returned
RWDBResult object is ready or not. If the
RWDBResult object is not ready, it goes into the loop and does some work in
//4. In this example, the work done in
//4 gets repeated until the
RWDBResult object gets to a
ready state.
//5 checks whether the call has been executed successfully or not.