summaryrefslogtreecommitdiffstats
path: root/reladminif/transactionif.pgc
blob: 6bd16f7a60d627b779d28632a5a42da0a1939c40 (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
/*
* 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:
 *   Code with embedded SQL for relational DBMS
 *
 *
 * COMMENTS:
 * - reconsider this 604 ignorance!
 *
 ***********************************************************************/

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

#include "debug.hh"

// general embedded SQL related definitions
EXEC SQL include sqlglobals.h;

#include "transactionif.hh"
#include "raslib/rmdebug.hh"
#include "adminif.hh"
#include "oidif.hh"
#include "catalogmgr/typefactory.hh"
#include "sqlerror.hh"
#include "objectbroker.hh"
#include "databaseif.hh"

// PG stuff for libpg connection maintenance
#include "libpq-fe.h"
extern PGconn *pgConn;


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");

		// 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();
		}

		TALK( "EXEC SQL COMMIT WORK" );
		EXEC SQL COMMIT WORK;
		if (SQLCODE == -604)	// = "no TA open" - seems to be a hickup from our double transaction
					// so we ignore it; FIXME: reinvestigate! -- PB 25-aug-2005
		{
			TALK( "TransactionIf::commit(): ignoring 'no TA open' error (SQLCODE -604) during COMMIT." );
		}
		else 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();
		}
		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);
}