How to use the Rogue Wave Informix Access Library for DBTools.h++ in a multi-threaded application

Article ID: 802
Last updated: 29 Jan, 2008
Article ID: 802
Last updated: 29 Jan, 2008
Revision: 1
Views: 3403
Posted: 04 Nov, 1999
by --
Updated: 29 Jan, 2008
by
Problem


How do I share connections among threads with the Informix Access Library?


Cause


Multi-threading with the Informix access library is solely focused on the issue of a DBTools application's ability to share connections among threads. Informix supplies this ability through their ESQL/C facility and the SET CONNECTION statement. Through the use of this statement, a connection is placed in an activated or deactivated state. When deactivated, a connection is in a dormant state (i.e. no work is currently pending on that connection) and is therefore accessible to all threads wishing to gain access to it. If a thread wishes to execute a statement on a connection, it must effectively block all other threads from gaining access to it. This is accomplished by executing the SET CONNECTION statement. Once this SET CONNECTION statement is executed, the executing thread can use the connection unimpeded by other threads or concurrency issues. When the thread has finished its task, it must make the connection available again to other threads. It accomplishes this by executing the SET CONNECTION DORMANT statement.

Rogue Wave decided that neither RWDBConnection nor Connection Pool were the correct classes to encapsulate the heuristics of this mechanism, at least not entirely. An RWDBConnection has a database-specific implementation, and it is correct for this class to maintain a record of the state of this connection's availability. It has to preserve its current state {ACTIVE | DORMANT}, its session name, and the currently activated connection's session name.

Since a connection only has to be active during the execution of a statement, it was decided to control the activation of the connection through the class RWDBStatement. This class is an encapsulation of the data and methods which comprise an SQL Statement (DML, DDL, stored procedures, etc.) and its pending result set (if any). This class is hidden from the user, but all DBTools classes with an execute() method are eventually funneled through it. The execute() method of RWDBStatement is the last possible moment at which we can safely activate the connection. Therefore, it is this class which maintains the exclusive nature of the activated connection until its implementation is destructed, at which time the SET CONNECT DORMANT statement is issued.


Action


To assign an RWDBConnection to execute on a certain thread, the connection must be passed into the multi-threaded function, and then be used explicitly in execute() calls in that function. It is the call to the execute() method which will enable the Informix style of assigning a connection to a particular thread. There is one thing to be aware of when performing this: even though the connection is attached to an object representing a database query, it is not released when that object is destroyed. Rather, the connection is released when the RWDBResult object created by the call to the execute() method is destroyed (see comments in sample code below).

For example:

{ 
   RWDBBResult result;
   RWDBTable table = aDB.table( myTable );

	{ // Scoping to ensure the destruction of the RWDBSelector object and its RWDBStatement.
   	RWDBSelector select = aDB.selector();

   	// select * from table
   	select << table;

   	// Activate the underlying Informix connection associated with aConn object.
   	result = select.execute( aConn );
        // RWDBResult object now has a reference to the underlying RWDBInformixStatementImpl
        //  object which produced it.

   	RWDBReader reader = result.table().reader();
   	while( reader() ) {
   	...
   	// iterate through result set
   	}  // End while
   } // End RWDBSelector's scope. The connection has NOT YET been deactivated!

} // End RWDBResult's scope.  It is here that the connection is deactivated and available to other threads.

This article was:   Helpful | Not helpful
Report an issue
Article ID: 802
Last updated: 29 Jan, 2008
Revision: 1
Views: 3403
Posted: 04 Nov, 1999 by --
Updated: 29 Jan, 2008 by

Others in this category