1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2011, 2015 Oracle and/or its affiliates. All rights reserved.
*/
/*
* This server is called by the client. It inserts a value into db1 then
* calls bdb2 to insert a value into db2.
*/
#include <db.h>
#include <xa.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <atmi.h> /* TUXEDO Header File */
#include <userlog.h> /* TUXEDO Header File */
#include <string.h>
#include <time.h>
#include <tpadm.h>
#include <unistd.h>
#include "../utilities/bdb_xa_util.h"
#define NUMDB 1
static int times = 1;
static long seq = 1;
static char *progname;
/* Open the database when the server is started. */
int
tpsvrinit(argc, argv)
int argc;
char **argv;
{
char ch;
progname = argv[0];
/* Some compilers warn if argc and argv aren't used. */
while ((ch = getopt(argc, argv, "t:")) != EOF)
switch (ch) {
case 't':
times = atoi(optarg);
break;
}
return (init_xa_server(NUMDB, progname, 0));
}
/* Close the database when the server is shutdown. */
void
tpsvrdone(void)
{
close_xa_server(NUMDB, progname);
}
/* Insert a value into db1, then call bdb2 to insert that value into db2. */
int
WRITE(rqst)
TPSVCINFO *rqst;
{
long rcvlen;
int ret, i;
DBT key, data;
size_t len;
int ch;
char *p, *t, buf[1024];
DB *dbp = dbs[0];
tpbegin(10,0);
for(i=0; i<times; i++){
/* Insert random records into the db1*/
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
sprintf(buf, "%ld %ld",random(), seq++);
len = strlen(buf);
key.data = buf;
data.data = buf;
data.size = key.size = (u_int32_t)len ;
switch (ret = dbp->put(dbp, NULL, &key, &data,
DB_NOOVERWRITE)) {
case 0:
break;
case DB_LOCK_DEADLOCK:
if(tpabort(0) == -1){
userlog("tpabort() fail:%s",
tpstrerror(tperrno));
}
tpreturn(TPSUCCESS, 1, rqst->data, 0L, 0);
default:
userlog("put: %s", db_strerror(ret));
if(tpabort(0) == -1){
userlog("tpabort() fail:%s",
tpstrerror(tperrno));
}
tpreturn(TPFAIL, 0, rqst->data, 0L, 0);
}
}
/*
* Insert the key into a queue and call bdb2 where it will read
* the key out of the queue.
*/
FBFR32 *reqbuf;
char * rcvbuf;
if((reqbuf = (FBFR32*) tpalloc("FML32", NULL, 100)) == NULL) {
tpabort(0);
userlog("alloc fail");
tpreturn(TPFAIL, 0, rqst->data, 0L, 0);
}
if((rcvbuf = (char *) tpalloc("STRING", NULL, 100)) == NULL) {
tpabort(0);
userlog("alloc fail");
tpreturn(TPFAIL, 0, rqst->data, 0L, 0);
}
Fadd32(reqbuf, TA_REPOSPARAM, buf, 0);
/* Call bdb2 */
ret = tpcall("WRITE2", (char *)reqbuf, 0, &rcvbuf, &rcvlen,TPSIGRSTRT);
tpfree((char*)reqbuf);
tpfree((char*)rcvbuf);
if(ret == -1){
userlog("call WRITE2 fail");
if(-1 == tpabort(0))
userlog("tpabort() fail:%s", tpstrerror(tperrno));
tpreturn(TPFAIL, 0, rqst->data, 0L, 0);
}
/* Commit for a return value of 0, otherwise abort. */
if (tpurcode == 0) {
if(tpcommit(0) == -1){
userlog("tpcommit fail");
tpreturn(TPFAIL, 0, rqst->data, 0L, 0);
}
} else {
if(tpabort(0) == -1){
userlog("tpabort fail");
tpreturn(TPFAIL, 0, rqst->data, 0L, 0);
}
}
tpreturn(TPSUCCESS, 0, rqst->data, 0L, 0);
}
/* Iterates the database with a cursor. */
int
CURSOR(rqst)
TPSVCINFO *rqst;
{
int ret,count;
DBT key, value;
DBC *cursorp;
DB *dbp = dbs[0];
tpbegin(60*10,0);
/* Get the cursor */
ret = dbp->cursor(dbp, NULL, &cursorp, 0);
if (ret != 0) {
userlog("count_records: cursor open failed.");
tpreturn(TPFAIL, 0, rqst->data, 0L, 0);
}
/* Get the key DBT used for the database read */
memset(&key, 0, sizeof(DBT));
memset(&value, 0, sizeof(DBT));
do {
ret = cursorp->c_get(cursorp, &key, &value, DB_NEXT);
switch (ret) {
case 0:
count++;
break;
case DB_NOTFOUND:
break;
case DB_LOCK_DEADLOCK:
if(tpcommit(0) == -1)
userlog("tpcommit() fail:%s",
tpstrerror(tperrno));
cursorp->c_close(cursorp);
tpreturn(TPSUCCESS, 1L, rqst->data, 0L, 0);
break;
default:
if(tpabort(0) == -1)
userlog("tpabort() fail:%s",
tpstrerror(tperrno));
cursorp->c_close(cursorp);
dbp->err(dbp, ret,
"Count records unspecified error");
tpreturn(TPFAIL, 0, rqst->data, 0L, 0);
}
} while (ret == 0);
cursorp->c_close(cursorp);
tpcommit(0);
tpreturn(TPSUCCESS, 0, rqst->data, 0L, 0);
}
|