changes for libcsqlstorage and inclusion of libcsqlbase
[csql.git] / test / sqlapi / Csql / Isolation / isotest1.c
blob1c95ee1f17bb9f962cfbf1804c626830144a175e
1 #include "common.h"
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);
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 pthread_t thr[2];
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; }
32 conn->disconnect();
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; }
37 return 0;
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);
47 # ifdef RDUNCOM
48 rv = conn->beginTrans(READ_UNCOMMITTED);
49 # elif defined RDRPT
50 rv = conn->beginTrans(READ_REPEATABLE);
51 # else
52 rv = conn->beginTrans(READ_COMMITTED);
53 # endif
54 if (rv != OK) return NULL;
55 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
56 p1RetVal = new int();
57 *p1RetVal = 0;
59 rv = insert(stmt, 100, true);
60 if (rv != OK) { printf("Test Failed:first thread failed to insert\n"); *p1RetVal = 1; }
62 conn->commit();
63 conn->disconnect();
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);
76 # ifdef RDUNCOM
77 rv = conn->beginTrans(READ_UNCOMMITTED);
78 # elif defined RDRPT
79 rv = conn->beginTrans(READ_REPEATABLE);
80 # else
81 rv = conn->beginTrans(READ_COMMITTED);
82 # endif
83 if (rv != OK) return NULL;
84 printf("Thread and pid is %d %lu\n", os::getpid(), os::getthrid());
85 p2RetVal = new int();
86 *p2RetVal = 0;
87 ::sleep(2);
88 rv = insert(stmt, 100, false);
89 if (rv == OK) { printf("Test Failed:second thread inserted\n"); *p2RetVal = 1; }
91 rv = conn->commit();
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);