SQL/MX Programming Manual for C and COBOL (G06.24+, H06.03+)
C++ Sample Program
HP NonStop SQL/MX Programming Manual for C and COBOL—523627-004
B-2
Ensuring Data Consistency
// Member function to prompt for the job description
void getdesc(){ cout << "Enter job description: " ; 
 cin >> memhv_jobdesc; } 
// Member function to put the host variables into the table.
// The host variables are referenced in member functions
// defined within the same class.
void putjob(){
 EXEC SQL
 INSERT INTO job
 VALUES (:memhv_jobcode, :memhv_jobdesc);
 }
// Member functions for begin work, commit work, rollback work
static void bw() { EXEC SQL BEGIN WORK; }
static void cw() { EXEC SQL COMMIT WORK; }
static void rw() { EXEC SQL ROLLBACK WORK; }
}; // End of class definition for jobsql
int main() 
{
 SQLSTATE[5]='\0'; 
 SQLSTATE_OK[5]='\0'; 
 cout << "Example begins and ends a transaction." << endl;
 jobsql mysql; // Instantiate a member of class jobsql
// Prompt for member class data for table
 mysql.getcode();
 mysql.getdesc();
 jobsql::bw(); // Begin the transaction
// Insert the data into the table
 mysql.putjob();
 jobsql::cw(); // Commit the transaction
 cout << "The insert is committed." << endl;
 return 0;
} // End of main
void end_prog()
{
 EXEC SQL WHENEVER SQLERROR CONTINUE;
 if (strcmp(SQLSTATE, SQLSTATE_OK) != 0) {
 cout << "The insert is rolled back." << endl;
 cout << "SQLSTATE: "<< SQLSTATE << endl;
 EXEC SQL ROLLBACK WORK; // Rollback the changes
 }
 exit(1);
} // End of end_prog
Example B-1. Using TMF to Ensure Data Consistency (page 2 of 2)










