2 //READ_COMMITTED isolation testing
3 //T1 and T2 both inserting, T2 will fail saying unique key constraint
5 void* runTest1(void *p
);
6 void* runTest2(void *p
);
12 AbsSqlConnection
*conn
= createConnection();
13 DbRetVal rv
= conn
->connect("root", "manager");
14 if (rv
!= OK
) { printf("Error during connection %d\n", rv
); return 1; }
15 AbsSqlStatement
*stmt
= createStatement();
16 stmt
->setConnection(conn
);
18 char statement
[1024]="create table t1 (f1 int, f2 char(196), primary key(f1));";
19 int ret
= executeDdlQuery(conn
, stmt
, statement
);
20 if (ret
!= 0) { return 1; }
22 int *status1
, *status2
;
23 pthread_create (&thr
[0], NULL
, &runTest1
, NULL
);
24 pthread_create (&thr
[1], NULL
, &runTest2
, NULL
);
25 printf("All threads started\n");
27 pthread_join(thr
[0], (void**)&status1
);
28 pthread_join(thr
[1], (void**)&status2
);
29 strcpy(statement
, "drop table t1;");
30 ret
= executeDdlQuery(conn
, stmt
, statement
);
31 if (ret
!= 0) { return 1; }
33 stmt
->free(); delete stmt
; delete conn
;
34 if (*status1
!= 0 || *status2
!= 0) return 1;
35 if (p1RetVal
) { delete p1RetVal
; p1RetVal
= NULL
; }
36 if (p2RetVal
) { delete p2RetVal
; p2RetVal
= NULL
; }
40 void* runTest1(void *message
)
42 AbsSqlConnection
*conn
= createConnection();
43 DbRetVal rv
= conn
->connect("root", "manager");
44 if (rv
!= OK
) { printf("Error during connection %d\n", rv
); return NULL
; }
45 AbsSqlStatement
*stmt
= createStatement();
46 stmt
->setConnection(conn
);
48 rv
= conn
->beginTrans(READ_UNCOMMITTED
);
50 rv
= conn
->beginTrans(READ_REPEATABLE
);
52 rv
= conn
->beginTrans(READ_COMMITTED
);
54 if (rv
!= OK
) return NULL
;
55 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
59 rv
= insert(stmt
, 100, true);
60 if (rv
!= OK
) { printf("Test Failed:first thread failed to insert\n"); *p1RetVal
= 1; }
64 stmt
->free(); delete stmt
; delete conn
;
65 printf("conn closed %d for Thread and pid is %d %lu\n", rv
, os::getpid(), os::getthrid());
66 pthread_exit(p1RetVal
);
69 void* runTest2(void *message
)
71 AbsSqlConnection
*conn
= createConnection();
72 DbRetVal rv
= conn
->connect("root", "manager");
73 if (rv
!= OK
) { printf("Error during connection %d\n", rv
); return NULL
; }
74 AbsSqlStatement
*stmt
= createStatement();
75 stmt
->setConnection(conn
);
77 rv
= conn
->beginTrans(READ_UNCOMMITTED
);
79 rv
= conn
->beginTrans(READ_REPEATABLE
);
81 rv
= conn
->beginTrans(READ_COMMITTED
);
83 if (rv
!= OK
) return NULL
;
84 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
88 rv
= insert(stmt
, 100, false);
89 if (rv
== OK
) { printf("Test Failed:second thread inserted\n"); *p2RetVal
= 1; }
92 rv
= conn
->disconnect();
93 stmt
->free(); delete stmt
; delete conn
;
94 printf("conn closed %d for Thread and pid is %d %lu\n", rv
, os::getpid(), os::getthrid());
95 pthread_exit(p2RetVal
);