Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Introduction to SourcePro C++
Rogue Wave web site:  Home Page  |  Main Documentation Page

4.2 An Example Using the Threads Module and the DB Interface Module

SourcePro DB is designed to work with the other SourcePro C++ products to supply database access. The following example combines class RWDBDatabase, from the DB Interface Module of SourcePro DB, with class RWServerPool, from the Threads Module of SourcePro Core, to create a higher-level abstraction, the multithreaded class QueryServer. In just a few lines of code, this class manages a controlled number of threads that can perform database queries for the whole application.

In the code using QueryServer, the server is constructed with an RWDBDatabase object and the number of threads to be used at one time for database queries is specified. Following that, application code can submit queries to the QueryServer using its enqueue() method. Internally, QueryServer manages its threads using RWServerPool, and asynchronously gives results to users via the RWTIOUResult template class. When the application is done with the -QueryServer, its destructor cleans up all the worker threads it created, and automatically frees up any used connections to the database.

QueryServer shows one way of easily wrapping up multithreaded database programming chores into an easy-to-use class for other programmers. However, the usage in main() does not exhibit its full capability. If you submitted 20 queries, the server would queue them and then send them to the database as connections are freed. This procedure allows you to keep strict control over the access and the number of connections made to your database.

The example is broken into two files, a header file for class QueryServer and the implementation file with the main() function. Both of these files are shown below.


The following example was built and tested using Solaris 8 / Forte 6, update 2, 32-bits (both static and dynamic builds). Depending on your environment and the options chosen when you build your SourcePro libraries, minor syntactical changes may be needed to build this example.

Example 3: Using the Threads Module and the DB Interface Module

Program output:



Previous fileTop of DocumentContentsNo linkNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.