*** empty log message ***
[csql.git] / test / sqlapi / Csql / Isolation / isotest5.c
blob58be3dcd44757c5cb848fd8b2bb0077a6b3a44d9
1 #include "common.h"
2 //READ_COMMITTED isolation testing
3 //T1 doing Select and T2 doing delete for same tuple
4 //Both should succeed
6 void* runTest1(void *p);
7 void* runTest2(void *p);
8 int *p1RetVal = NULL;
9 int *p2RetVal = NULL;
10 int main()
13 AbsSqlConnection *conn = createConnection();
14 DbRetVal rv = conn->connect("root", "manager");
15 if (rv != OK) { printf("Error during connection %d\n", rv); return 1; }
16 AbsSqlStatement *stmt = createStatement();
17 stmt->setConnection(conn);
19 char statement[1024]="create table t1 (f1 int, f2 char(196), primary key(f1));";
20 int ret = executeDdlQuery(conn, stmt, statement);
21 if (ret != 0) { return 1; }
22 rv = conn->beginTrans(READ_COMMITTED);
23 if (rv != OK) return 2;
24 rv = insert(stmt, 100, false);
25 if (rv != OK) { printf("Error in inserting the record\n"); return 3; }
26 conn->commit();
27 printf("Tuple inserted\n");
29 pthread_t thr[2];
30 int *status1, *status2;
31 pthread_create (&thr[0], NULL, &runTest1, NULL);
32 pthread_create (&thr[1], NULL, &runTest2, NULL);
33 printf("All threads started\n");
35 pthread_join(thr[0], (void**)&status1);
36 pthread_join(thr[1], (void**)&status2);
37 strcpy(statement, "drop table t1;");
38 ret = executeDdlQuery(conn, stmt, statement);
39 if (ret != 0) { return 3; }
40 conn->disconnect();
41 stmt->free(); delete stmt; delete conn;
42 if (*status1 != 0 || *status2 != 0) return 4;
43 if (p1RetVal) { delete p1RetVal; p1RetVal = NULL; }
44 if (p2RetVal) { delete p2RetVal; p2RetVal = NULL; }
45 return 0;
48 void* runTest1(void *message)
50 AbsSqlConnection *conn = createConnection();
51 DbRetVal rv = conn->connect("root", "manager");
52 if (rv != OK) { printf("Error during connection %d\n", rv); return NULL; }
53 AbsSqlStatement *stmt = createStatement();
54 stmt->setConnection(conn);
55 # ifdef RDUNCOM
56 rv = conn->beginTrans(READ_UNCOMMITTED);
57 # elif defined RDRPT
58 rv = conn->beginTrans(READ_REPEATABLE);
59 #else
60 rv = conn->beginTrans(READ_COMMITTED);
61 #endif
62 if (rv != OK) return NULL;
63 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
64 p1RetVal = new int();
65 *p1RetVal = 0;
67 rv = select(stmt, 100, true);
68 if (rv != OK) { printf("Test Failed:first thread failed to select\n"); *p1RetVal = 1; }
69 conn->commit();
70 conn->disconnect();
71 stmt->free(); delete stmt; delete conn;
72 printf("conn closed %d for Thread and pid is %d %lu\n", rv, os::getpid(), os::getthrid());
73 pthread_exit(p1RetVal);
76 void* runTest2(void *message)
78 AbsSqlConnection *conn = createConnection();
79 DbRetVal rv = conn->connect("root", "manager");
80 if (rv != OK) { printf("Error during connection %d\n", rv); return NULL; }
81 AbsSqlStatement *stmt = createStatement();
82 stmt->setConnection(conn);
83 # ifdef RDUNCOM
84 rv = conn->beginTrans(READ_UNCOMMITTED);
85 # elif defined RDRPT
86 rv = conn->beginTrans(READ_REPEATABLE);
87 #else
88 rv = conn->beginTrans(READ_COMMITTED);
89 #endif
90 if (rv != OK) return NULL;
91 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
92 p2RetVal = new int();
93 *p2RetVal = 0;
94 rv = remove(stmt, 100, false);
95 # ifdef RDUNCOM
96 if (rv != OK) {
97 printf("Test Failed:second thread failed to remove.\n");
98 # elif defined RDRPT
99 if (rv == OK) {
100 printf("Test Failed:second thread removes.\n");
101 # else
102 if (rv != OK) {
103 printf("Test Failed:second thread failed to remove.\n");
104 #endif
105 *p2RetVal = 1;
107 rv = conn->commit();
108 rv = conn->disconnect();
109 stmt->free(); delete stmt; delete conn;
110 printf("conn closed %d for Thread and pid is %d %lu\n", rv, os::getpid(), os::getthrid());
111 pthread_exit(p2RetVal);