2 //READ_COMMITTED isolation testing
4 // -------------------------
8 // T1 second read should fail saying "tuple not found
9 void* runTest1(void *p
);
10 void* runTest2(void *p
);
13 int selectDone
= 0, updateDone
=0, allDone
= 0;
18 AbsSqlConnection
*conn
= createConnection();
19 DbRetVal rv
= conn
->connect("root", "manager");
20 if (rv
!= OK
) { printf("Error during connection %d\n", rv
); return 1; }
21 AbsSqlStatement
*stmt
= createStatement();
22 stmt
->setConnection(conn
);
24 char statement
[1024]="create table t1 (f1 int, f2 char(196), primary key(f1));";
25 int ret
= executeDdlQuery(conn
, stmt
, statement
);
26 if (ret
!= 0) { return 1; }
27 rv
= conn
->beginTrans(READ_COMMITTED
);
28 if (rv
!= OK
) return 2;
29 rv
= insert(stmt
, 100, false);
30 if (rv
!= OK
) { printf("Error in inserting the record\n"); return 3; }
32 printf("Tuple inserted\n");
35 int *status1
, *status2
;
36 pthread_create (&thr
[0], NULL
, &runTest1
, NULL
);
37 pthread_create (&thr
[1], NULL
, &runTest2
, NULL
);
38 printf("All threads started\n");
40 pthread_join(thr
[0], (void**)&status1
);
41 pthread_join(thr
[1], (void**)&status2
);
42 strcpy(statement
, "drop table t1;");
43 ret
= executeDdlQuery(conn
, stmt
, statement
);
44 if (ret
!= 0) { return 3; }
46 stmt
->free(); delete stmt
; delete conn
;
47 if (*status1
!= 0 || *status2
!= 0) return 4;
48 if (p1RetVal
) { delete p1RetVal
; p1RetVal
= NULL
; }
49 if (p2RetVal
) { delete p2RetVal
; p2RetVal
= NULL
; }
53 void* runTest1(void *message
)
55 AbsSqlConnection
*conn
= createConnection();
56 DbRetVal rv
= conn
->connect("root", "manager");
57 if (rv
!= OK
) { printf("Error during connection %d\n", rv
); return NULL
; }
58 AbsSqlStatement
*stmt
= createStatement();
59 stmt
->setConnection(conn
);
60 rv
= conn
->beginTrans(READ_COMMITTED
);
61 if (rv
!= OK
) return NULL
;
62 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
66 rv
= select(stmt
, 100, true);
67 if (rv
!= OK
) { printf("Test Failed:first thread failed to select\n"); *p1RetVal
= 1; }
69 while (updateDone
!= 1) {::sleep(1); }
70 rv
= select(stmt
, 100, true, true);
71 if (rv
== OK
) { printf("Test Failed:first thread read succeeded \n"); *p1RetVal
= 1; }
74 rv
= conn
->disconnect();
75 stmt
->free(); delete stmt
; delete conn
;
76 printf("conn closed %d for Thread and pid is %d %lu\n", rv
, os::getpid(), os::getthrid());
77 pthread_exit(p1RetVal
);
80 void* runTest2(void *message
)
82 AbsSqlConnection
*conn
= createConnection();
83 DbRetVal rv
= conn
->connect("root", "manager");
84 if (rv
!= OK
) { printf("Error during connection %d\n", rv
); return NULL
; }
85 AbsSqlStatement
*stmt
= createStatement();
86 stmt
->setConnection(conn
);
87 rv
= conn
->beginTrans(READ_COMMITTED
);
88 if (rv
!= OK
) return NULL
;
89 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
90 while (selectDone
!= 1) {::sleep(1); }
93 rv
= update(stmt
, 100, true);
95 printf("Test Failed:second thread failed to update\n");
99 while (allDone
!=1) ::sleep(1);
101 rv
= conn
->disconnect();
102 stmt
->free(); delete stmt
; delete conn
;
103 printf("conn closed %d for Thread and pid is %d %lu\n", rv
, os::getpid(), os::getthrid());
104 pthread_exit(p2RetVal
);