summaryrefslogtreecommitdiffstats
path: root/reladminif/test/testta.pgc
blob: 38a994c1e351fe4d730bad4f87bbc5c4e524e413 (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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
/*************************************************************************
 *
 *
 * PURPOSE:
 * test class TransactionIF.
 *
 *
 * COMMENTS:
 *   none
 *
 * PRECONDITIONS:
 * - database exists and is accessible (PG & rasdaman server running)
 * - reladminif/DatabaseIF works
 *
 ***********************************************************************/

static const char rcsid[] = "@(#)reladminif,TransactionIf: $Id: testta.pgc,v 1.6 2003/12/27 23:11:43 rasdev Exp $";

#define DEBUG_MAIN
#include "debug.hh"

// constants:
#define DATABASE "RASBASE"

// pointers representing O2, database, ta and session
AdminIf*       admin = NULL;
DatabaseIf*    db    = NULL;
TransactionIf* ta    = NULL;

int main()
{
	const char *prog = argv[0];	// this program's name

	cout << prog << ": test for transactionif, generated on " << COMPDATE << endl;

	// --- outmost exception catcher ------------------------
	try
	{

		// --- (1) preparation: open connection
		admin = AdminIf::instance();
        	if( !admin )
        	{
                	cout << prog << ": cannot create adminIf instance" << endl;
               	 	throw r_Error();
        	}
               	// connect to the database
                db = new DatabaseIf();
        	if( !admin )
        	{
                	cout << prog << ": cannot create DatabaseIf instance" << endl;
               	 	throw r_Error();
        	}
               	db->open( DATABASE );

		ta = new TransactionIf();
        	if( !admin )
        	{
                	cout << prog << ": cannot create TransactionIf instance" << endl;
               	 	throw r_Error();
        	}
		ta->begin( db );
		ta->abort( db );
		ta->commit( db );

		db->close();
	}
	catch(...)
	{
		cout << prog << ": error abort." << endl;
	}

	cout << prog << ": test for transactionif done." << endl;
}


void 
TransactionIf::begin( bool readOnly ) throw ( r_Error )
{
	RMDBGENTER(2, RMDebug::module_adminif, "TransactionIf", "begin(" << readOnly << ")");
	ENTER( "TransactionIf::begin, readOnly=" << readOnly );

	isReadOnly = readOnly;  
	AdminIf::setAborted(false);
	AdminIf::setReadOnlyTA(readOnly);

	TALK( "EXEC SQL BEGIN WORK" );
	EXEC SQL BEGIN WORK;
	if (sqlca.sqlwarn[2] == 'W')	// real error, not just a warning
	{
		SQLCODE = 0;		// FIXME: bad hack, as PG can't reset error state and SQLCODE is queried in many places -- PB 2005-jan-09
	}
	else
	{
		RMDBGMIDDLE(4, RMDebug::module_adminif, "TransactionIf", "error occured while issuing BEGIN");  
		LEAVE( "TransactionIf::begin(): error during BEGIN: " << SQLCODE );
		generateException();
	}
	
	if (readOnly)
	{
		TALK( "EXEC SQL SET TRANSACTION READ ONLY" );
		EXEC SQL SET TRANSACTION READ ONLY;
		// no error check, as this doesn't inhibit work
	}

	// prelim.:have additional libpq TA -- PB 2005-jan-09
	TALK( "PQexec( pgConn, BEGIN )" );
	PGresult *pgResult = PQexec( pgConn, "BEGIN" );
	if (PQresultStatus(pgResult) != PGRES_COMMAND_OK)
	{
		LEAVE( "TransactionIf::begin() Error: cannot open libpq TA: " << PQerrorMessage(pgConn) );
		generateException();
	}

#ifdef RMANBENCHMARK
	DBObject::readTimer.start();
	DBObject::readTimer.pause();
	
	DBObject::updateTimer.start();
	DBObject::updateTimer.pause();
	
	DBObject::deleteTimer.start();
	DBObject::deleteTimer.pause();
	
	DBObject::insertTimer.start();
	DBObject::insertTimer.pause();
  
        OId::oidAlloc.start();
        OId::oidAlloc.pause();

        OId::oidResolve.start();
        OId::oidResolve.pause();
#endif

	OId::initialize();
	TypeFactory::initialize();

	LEAVE( "TransactionIf::begin, SQLCODE=" << SQLCODE );
	RMDBGEXIT(2, RMDebug::module_adminif, "TransactionIf", "begin(" << readOnly << ") ");
}

void 
TransactionIf::commit() throw (  r_Error  )
{
	RMDBGENTER(2, RMDebug::module_adminif, "TransactionIf", "commit()");
	ENTER( "TransactionIf::commit" );

	if (isReadOnly)
	{
		RMDBGMIDDLE(9, RMDebug::module_adminif, "TransactionIf", "read only: aborting");
		TALK( "TA is readonly: aborting" );
		abort();
	}
	else
	{
		AdminIf::setAborted(false);
		RMDBGMIDDLE(9, RMDebug::module_adminif, "TransactionIf", "set aborted false");
		TypeFactory::freeTempTypes();
		RMDBGMIDDLE(9, RMDebug::module_adminif, "TransactionIf", "freed temp types");
		ObjectBroker::clearBroker();
		RMDBGMIDDLE(9, RMDebug::module_adminif, "TransactionIf", "cleared broker");
		OId::deinitialize();
		RMDBGMIDDLE(9, RMDebug::module_adminif, "TransactionIf", "wrote oid counters");
		AdminIf::setReadOnlyTA(false);
		RMDBGMIDDLE(9, RMDebug::module_adminif, "TransactionIf", "committing");

		TALK( "EXEC SQL COMMIT WORK" );
		EXEC SQL COMMIT WORK;
		if (check("TransactionIf::begin() COMMIT\0"))
		{
			RMDBGMIDDLE(4, RMDebug::module_adminif, "TransactionIf", "error occured while issuing COMMIT");  
			LEAVE( "TransactionIf::commit(): error during COMMIT:" << SQLCODE );
			generateException();
		}

		// prelim.:have additional libpq TA -- PB 2005-jan-09
		TALK( "PQexec( pgConn, END )" );
		PGresult *pgResult = PQexec( pgConn, "END" );
		if (PQresultStatus(pgResult) != PGRES_COMMAND_OK)
		{
			LEAVE( "TransactionIf::commit() Error: cannot commit libpq TA: " << PQerrorMessage(pgConn) );
			generateException();
		}

		if (lastBase) 
		{
			RMDBGMIDDLE(9, RMDebug::module_adminif, "TransactionIf", "closing dbms");
			lastBase->baseDBMSClose();
		}
	}

#ifdef RMANBENCHMARK
	DBObject::readTimer.stop();
	
	DBObject::updateTimer.stop();
	
	DBObject::deleteTimer.stop();
	
	DBObject::insertTimer.stop();

	OId::oidAlloc.stop();

        OId::oidResolve.stop();
#endif

	LEAVE( "TransactionIf::commit" );
	RMDBGEXIT(2, RMDebug::module_adminif, "TransactionIf", "commit() " << endl << endl);
}

void 
TransactionIf::abort()
{
	RMDBGENTER(2, RMDebug::module_adminif, "TransactionIf", "abort()");
	ENTER( "TransactionIf::abort" );
  
	// prelim.:have additional libpq TA -- PB 2005-jan-09
	TALK( "PQexec( pgConn, ABORT )" );
	PGresult *pgResult = PQexec( pgConn, "ABORT" );
	if (PQresultStatus(pgResult) != PGRES_COMMAND_OK)
	{
		LEAVE( "TransactionIf::abort() Error: cannot abort libpq TA: " << PQerrorMessage(pgConn) );
		generateException();
	}

	AdminIf::setAborted(true);
	TypeFactory::freeTempTypes();
	ObjectBroker::clearBroker();
	OId::deinitialize();
	AdminIf::setReadOnlyTA(false);

	TALK( "EXEC SQL ROLLBACK WORK" );
	EXEC SQL ROLLBACK WORK;
	if (check("TransactionIf::abort() ROLLBACK\0"))
	{
		RMDBGMIDDLE(4, RMDebug::module_adminif, "TransactionIf", "error occured while issuing ROLLBACK");  
		TALK( "TransactionIf::abort(): error during ROLLBACK, still continuing: " << SQLCODE );
	}
	if(lastBase)
		lastBase->baseDBMSClose();

#ifdef RMANBENCHMARK
	DBObject::readTimer.stop();
	
	DBObject::updateTimer.stop();
	
	DBObject::deleteTimer.stop();
	
	DBObject::insertTimer.stop();

	OId::oidAlloc.stop();

        OId::oidResolve.stop();
#endif

	LEAVE( "TransactionIf::abort, SQLCODE=" << SQLCODE );
	RMDBGEXIT(2, RMDebug::module_adminif, "TransactionIf", "abort() " << endl << endl);
}