2 //READ_COMMITTED isolation testing
3 //T1 doing update and T2 doing delete for same tuple
5 void* runTest1(void *p
);
6 void* runTest2(void *p
);
10 int updateDone1
= 0, updateDone2
=0;
14 AbsSqlConnection
*conn
= createConnection();
15 DbRetVal rv
= conn
->connect("root", "manager");
16 if (rv
!= OK
) { printf("Error during connection %d\n", rv
); return 1; }
17 AbsSqlStatement
*stmt
= createStatement();
18 stmt
->setConnection(conn
);
20 char statement
[1024]="create table t1 (f1 int, f2 char(196), primary key(f1));";
21 int ret
= executeDdlQuery(conn
, stmt
, statement
);
22 if (ret
!= 0) { return 1; }
23 rv
= conn
->beginTrans(READ_COMMITTED
);
24 if (rv
!= OK
) return 2;
25 rv
= insert(stmt
, 100, false);
26 if (rv
!= OK
) { printf("Error in inserting the record\n"); return 3; }
28 printf("Tuple inserted\n");
31 int *status1
, *status2
;
32 pthread_create (&thr
[0], NULL
, &runTest1
, NULL
);
33 pthread_create (&thr
[1], NULL
, &runTest2
, NULL
);
34 printf("All threads started\n");
36 pthread_join(thr
[0], (void**)&status1
);
37 pthread_join(thr
[1], (void**)&status2
);
38 strcpy(statement
, "drop table t1;");
39 ret
= executeDdlQuery(conn
, stmt
, statement
);
40 if (ret
!= 0) { return 3; }
42 stmt
->free(); delete stmt
; delete conn
;
43 if (*status1
!= 0 || *status2
!= 0) return 4;
44 if (p1RetVal
) { delete p1RetVal
; p1RetVal
= NULL
; }
45 if (p2RetVal
) { delete p2RetVal
; p2RetVal
= NULL
; }
49 void* runTest1(void *message
)
51 AbsSqlConnection
*conn
= createConnection();
52 DbRetVal rv
= conn
->connect("root", "manager");
53 if (rv
!= OK
) { printf("Error during connection %d\n", rv
); return NULL
; }
54 AbsSqlStatement
*stmt
= createStatement();
55 stmt
->setConnection(conn
);
57 rv
= conn
->beginTrans(READ_UNCOMMITTED
);
59 rv
= conn
->beginTrans(READ_REPEATABLE
);
61 rv
= conn
->beginTrans(READ_COMMITTED
);
63 if (rv
!= OK
) return NULL
;
64 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
68 rv
= update(stmt
, 100, true);
69 if (rv
!= OK
) { printf("Test Failed:first thread failed to update\n"); *p1RetVal
= 1; }
71 while (updateDone2
!= 1) sleep(1);
72 rv
= select(stmt
, 100, true, true);
73 if (rv
!= OK
) { printf("Test Failed:first thread read wrong value.\n"); *p1RetVal
= 1; }
76 stmt
->free(); delete stmt
; delete conn
;
77 printf("conn closed %d for Thread and pid is %d %lu\n", rv
, os::getpid(), os::getthrid());
78 pthread_exit(p1RetVal
);
81 void* runTest2(void *message
)
83 AbsSqlConnection
*conn
= createConnection();
84 DbRetVal rv
= conn
->connect("root", "manager");
85 if (rv
!= OK
) { printf("Error during connection %d\n", rv
); return NULL
; }
86 AbsSqlStatement
*stmt
= createStatement();
87 stmt
->setConnection(conn
);
89 rv
= conn
->beginTrans(READ_UNCOMMITTED
);
91 rv
= conn
->beginTrans(READ_REPEATABLE
);
93 rv
= conn
->beginTrans(READ_COMMITTED
);
95 if (rv
!= OK
) return NULL
;
96 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
99 rv
= update(stmt
, 100, true, "RITHISH");
101 printf("Test Failed:second thread updates.\n");
106 rv
= conn
->disconnect();
107 stmt
->free(); delete stmt
; delete conn
;
108 printf("conn closed %d for Thread and pid is %d %lu\n", rv
, os::getpid(), os::getthrid());
109 pthread_exit(p2RetVal
);