*** empty log message ***
[csql.git] / test / sqlapi / Csql / Isolation / isotest7.c
blobc8157e6d3a1db4d880eb28657fdae0cee16a9b78
1 #include "common.h"
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);
7 int *p1RetVal = NULL;
8 int *p2RetVal = NULL;
9 int main()
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; }
21 rv = conn->beginTrans(READ_COMMITTED);
22 if (rv != OK) return 2;
23 rv = insert(stmt, 100, false);
24 if (rv != OK) { printf("Error in inserting the record\n"); return 3; }
25 conn->commit();
26 printf("Tuple inserted\n");
28 pthread_t thr[2];
29 int *status1, *status2;
30 pthread_create (&thr[0], NULL, &runTest1, NULL);
31 pthread_create (&thr[1], NULL, &runTest2, NULL);
32 printf("All threads started\n");
34 pthread_join(thr[0], (void**)&status1);
35 pthread_join(thr[1], (void**)&status2);
36 strcpy(statement, "drop table t1;");
37 ret = executeDdlQuery(conn, stmt, statement);
38 if (ret != 0) { return 3; }
39 conn->disconnect();
40 stmt->free(); delete stmt; delete conn;
41 if (*status1 != 0 || *status2 != 0) return 4;
42 if (p1RetVal) { delete p1RetVal; p1RetVal = NULL; }
43 if (p2RetVal) { delete p2RetVal; p2RetVal = NULL; }
44 return 0;
47 void* runTest1(void *message)
49 AbsSqlConnection *conn = createConnection();
50 DbRetVal rv = conn->connect("root", "manager");
51 if (rv != OK) { printf("Error during connection %d\n", rv); return NULL; }
52 AbsSqlStatement *stmt = createStatement();
53 stmt->setConnection(conn);
54 # ifdef RDUNCOM
55 rv = conn->beginTrans(READ_UNCOMMITTED);
56 # elif defined RDRPT
57 rv = conn->beginTrans(READ_REPEATABLE);
58 # else
59 rv = conn->beginTrans(READ_COMMITTED);
60 #endif
61 if (rv != OK) return NULL;
62 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
63 p1RetVal = new int();
64 *p1RetVal = 0;
66 rv = update(stmt, 100, true);
67 if (rv != OK) { printf("Test Failed:first thread failed to update\n"); *p1RetVal = 1; }
68 conn->commit();
69 conn->disconnect();
70 stmt->free(); delete stmt; delete conn;
71 printf("conn closed %d for Thread and pid is %d %lu\n", rv, os::getpid(), os::getthrid());
72 pthread_exit(p1RetVal);
75 void* runTest2(void *message)
77 AbsSqlConnection *conn = createConnection();
78 DbRetVal rv = conn->connect("root", "manager");
79 if (rv != OK) { printf("Error during connection %d\n", rv); return NULL; }
80 AbsSqlStatement *stmt = createStatement();
81 stmt->setConnection(conn);
82 # ifdef RDUNCOM
83 rv = conn->beginTrans(READ_UNCOMMITTED);
84 # elif defined RDRPT
85 rv = conn->beginTrans(READ_REPEATABLE);
86 # else
87 rv = conn->beginTrans(READ_COMMITTED);
88 #endif
89 if (rv != OK) return NULL;
90 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
91 p2RetVal = new int();
92 *p2RetVal = 0;
93 rv = remove(stmt, 100, false);
94 if (rv == OK) {
95 printf("Test Failed:second thread removes.\n");
96 *p2RetVal = 1;
98 rv = conn->commit();
99 rv = conn->disconnect();
100 stmt->free(); delete stmt; delete conn;
101 printf("conn closed %d for Thread and pid is %d %lu\n", rv, os::getpid(), os::getthrid());
102 pthread_exit(p2RetVal);