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, deleteDone
=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
);
61 rv
= conn
->beginTrans(READ_UNCOMMITTED
);
63 rv
= conn
->beginTrans(READ_REPEATABLE
);
65 rv
= conn
->beginTrans(READ_COMMITTED
);
67 if (rv
!= OK
) return NULL
;
68 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
72 rv
= select(stmt
, 100, true);
73 if (rv
!= OK
) { printf("Test Failed:first thread failed to select\n"); *p1RetVal
= 1; }
75 while (deleteDone
!= 1) {::sleep(1); }
76 printf("P2 deleted says P1 and P1 is selecting now\n");
77 rv
= select(stmt
, 100, false);
78 if (rv
== OK
) { printf("Test Failed:first thread read succeeded \n"); *p1RetVal
= 1; }
79 printf ("P1 second select returned with rv = %d\n", rv
);
81 rv
= conn
->disconnect();
82 stmt
->free(); delete stmt
; delete conn
;
83 printf("conn closed %d for Thread and pid is %d %lu\n", rv
, os::getpid(), os::getthrid());
84 pthread_exit(p1RetVal
);
87 void* runTest2(void *message
)
89 AbsSqlConnection
*conn
= createConnection();
90 DbRetVal rv
= conn
->connect("root", "manager");
91 if (rv
!= OK
) { printf("Error during connection %d\n", rv
); return NULL
; }
92 AbsSqlStatement
*stmt
= createStatement();
93 stmt
->setConnection(conn
);
95 rv
= conn
->beginTrans(READ_UNCOMMITTED
);
97 rv
= conn
->beginTrans(READ_REPEATABLE
);
99 rv
= conn
->beginTrans(READ_COMMITTED
);
101 if (rv
!= OK
) return NULL
;
102 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
103 while (selectDone
!= 1) {::sleep(1); }
104 p2RetVal
= new int();
106 rv
= remove(stmt
, 100, true);
109 printf("Test Failed:second thread failed to remove.\n");
112 printf("Test Failed:second thread selects.\n");
115 printf("Test Failed:second thread failed to remove\n");
119 printf("P2 deleted\n");
123 printf("P2 committed\n");
124 rv
= conn
->disconnect();
125 stmt
->free(); delete stmt
; delete conn
;
126 printf("conn closed %d for Thread and pid is %d %lu\n", rv
, os::getpid(), os::getthrid());
127 pthread_exit(p2RetVal
);