Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> C++ and thread-context sanity-check

C++ and thread-context sanity-check

From: Paul J. Lucas <pjl.removethis_at_removethistoo.mac.com>
Date: Tue, 08 Jan 2002 22:49:11 GMT
Message-ID: <HHK_7.28596$zs4.2712781944@newssvr14.news.prodigy.com>

	So I have a C++ class that encapsulates a connection (thread
	context) to Oracle.  I discovered that the ProC/C++ compiler
	defines the sql_context type and it isn't defined in any .h
	file.  This means you I use sql_context in any .h file of mine
	that is #include'd into a .c file that isn't preprocessed by
	ProC/C++.  (This is brain-damaged, IMHO.)

	So what I did instead in my C++ class is of the form:

		class OracleClient {
			// ...
			void*	sqlContext;

};
and then in the constructor/destructor (in a .pc file): OracleClient::OracleClient() { sqlContext = new sql_context; EXEC SQL BEGIN DECLARE SECTION; sql_context *ctx = (sql_context*)sqlContext; EXEC SQL END DECLARE SECTION; EXEC SQL CONTEXT ALLOCATE :*ctx; EXEC SQL CONTEXT USE :*ctx; EXEC SQL CONNECT :user IDENTIFIED BY :password;
}
OracleClient::~OracleClient() { EXEC SQL BEGIN DECLARE SECTION; sql_context *ctx = (sql_context*)sqlContext; EXEC SQL END DECLARE SECTION; EXEC SQL CONTEXT USE :*ctx; EXEC SQL COMMIT WORK RELEASE; EXEC SQL CONTEXT FREE :*ctx; delete sqlContext;
}
(Note: error checking has been elided from this post.) And then to use the context for a query: OracleClient::some_func() { EXEC SQL BEGIN DECLARE SECTION; sql_context *ctx = (sql_context*)sqlContext; EXEC SQL END DECLARE SECTION; EXEC SQL CONTEXT USE :*ctx; EXEC SQL SELECT /* blah blah */; // ... EXEC SQL ROLLBACK;
}
I have a multithreaded server there each thread has its own thread-context. Each thread persists for the life of the server. Also, I'm following the recommendation in "Including the SQLCA, ORACA, and SQLDA," Pro*C/C++ Precompiler Programmer's Guide, "Chapter 5: Advanced Topics: The C Preprocessor," Oracle Corporation, 1999, that says do do: #undef SQLCA #include <sqlca.h> inside every function so sqlca will be local and not shared between threads. My questions are: 1. Is all of the above correct? 2. Is it reasonable? 3. Is there a better way? The code seems to work OK for q while, but over time in the server code I'm writing, it seems like some of the threads' connections to Oracle get disconnected. Any ideas? By the way, I'm running Oracle 9i under Solaris 2.8. - Paul
Received on Tue Jan 08 2002 - 16:49:11 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US