summaryrefslogtreecommitdiff
path: root/test/xa/src5/server.c
blob: c23bdd2db48f62f28489379f97de664acac5ce16 (plain)
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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2011, 2015 Oracle and/or its affiliates.  All rights reserved.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <atmi.h>

#include <tx.h>
#include <xa.h>

#include <db.h>

#include "../utilities/bdb_xa_util.h"

#define KEY_VALUE		0
#define READ_STRING		"read_db1"
#define WRITE_STRING		"write_db1"

void write_db1(TPSVCINFO *);
void read_db1(TPSVCINFO *);

#define	HOME	"../data"
#define NUMDB	1

char *progname;					/* Server run-time name. */

/* Called once when the server is started. Creates and opens 2 databases. */
int
tpsvrinit(int argc, char* argv[])
{
	int mvcc = 0;

#ifdef MVCC_FLAG
	mvcc = 1;
#endif
	progname = argv[0];
	return (init_xa_server(NUMDB, progname, mvcc));
}

/* Called once when the servers are shutdown.  Closes the databases. */
void
tpsvrdone()
{
	close_xa_server(NUMDB, progname);
}


void
write_db1(TPSVCINFO *msg)
{
	int ret, commit, key_value, data_value;
	DBT key, data;
	DB *db;

	db = dbs[0];
	memset(&key, 0, sizeof key);
	memset(&data, 0, sizeof data);
	commit = 1;

	key_value = data_value = KEY_VALUE;
	data.data = &data_value;
	data.size = sizeof(data_value);
	key.data = &key_value;
	key.size = sizeof(key_value);

	/* Insert data into the tables. */
	if (verbose) {
		printf("put: key: %i ", key_value);
		printf("put: data: %i\n", data_value);
	}
	/* Insert data into the tables. */
	if ((ret = db->put(db, NULL, &key, 
	    &data, 0)) != 0) {
		if (ret == DB_LOCK_DEADLOCK || 
		    ret == DB_LOCK_NOTGRANTED)
			goto abort;
		fprintf(stderr, "%s: %s: %s->put: %s\n",
		    progname, WRITE_STRING, db_names[0], 
		    db_strerror(ret));
		goto err;
	}

	/* Returns a commit or abort command to the client. */
	if (verbose)
		printf("%s: %s: commit\n", progname, WRITE_STRING);
	tpreturn(TPSUCCESS, 0L, 0, 0L, 0);
	if (0) {
abort:		if (verbose)
			printf("%s: %s: abort\n", progname, WRITE_STRING);
		tpreturn(TPSUCCESS, 1L, 0, 0L, 0);
	}
	return;

err:	tpreturn(TPFAIL, 1L, 0, 0L, 0);
}

void
read_db1(TPSVCINFO *msg)
{
	int ret, db_num, commit, key_value;
	DBT key, data;
	DB *db;

	db = dbs[0];
	memset(&key, 0, sizeof key);
	memset(&data, 0, sizeof data);
	commit = 1;

	db_num = key_value = KEY_VALUE;
	key.data = &key_value;
	key.size = sizeof(key_value);
	data.flags = DB_DBT_MALLOC;

	/* Insert data into the tables. */
	if (verbose) {
		printf("get: key: %i ", key_value);
		printf("get: data.\n");
	}
	/* Get data from the table. */
	if ((ret = db->get(db, NULL, &key, &data, 0)) != 0) {
		if (ret == DB_LOCK_DEADLOCK || 
		    ret == DB_LOCK_NOTGRANTED)
			goto abort;
		fprintf(stderr, "%s: %s: %s->get: %s\n",
		    progname, READ_STRING, db_names[0], 
		    db_strerror(ret));
		goto err;
	}
	if (data.data != NULL)
		free(data.data);

	/* Returns a commit or abort command to the client. */
	if (verbose)
		printf("%s: %s: commit\n", progname, READ_STRING);
	tpreturn(TPSUCCESS, 0L, 0, 0L, 0);
	if (0) {
abort:		if (verbose)
			printf("%s: %s: abort\n", progname, READ_STRING);
		tpreturn(TPSUCCESS, 1L, 0, 0L, 0);
	}
	return;

err:	tpreturn(TPFAIL, 1L, 0, 0L, 0);
}