January 25, 2021
Hot Topics:

Faster and Friendlier Access to Oracle's OCI API

  • By Victor Volkman
  • Send Email »
  • More Articles »

Starting off in main(), Lines #17-28 rehash what you did in the first example except that you did add OCI_ENV_THREADED to OCI_Initialize() this time around. Next, you're going to initialize the connection pool that all the threads will draw from. Back in the bad old days, developers had to write their own connection pooling code. In fact, if you are using Oracle 8i, OCILIB will do this for you. For Oracle 9i (and later), OCILIB uses the native connection pooling behind-the-scenes.

26    pool = OCI_ConnPoolCreate("db", "user", "pwd",

The main parameters of interest are the last three: minimum number of connections to open, maximum connections to open, and next increment. For example, you could ask to start with four connections, a maximum of 32 open, and to increment by 8 each time you need more (and you're under the 32 open limit).

You'll skip directly to the thread creating and spawning in Lines #29-32.

29    for (i = 0; i < MAX_THREADS; i++) {
30       th[i] = OCI_ThreadCreate();
31       OCI_ThreadRun(th[i], worker, pool);
32    }

OCI_ThreadCreate() just initializes your thread handle, which you are storing in the th[] array for convenience sake. Next, you spawn the new thread (process) with OCI_ThreadRun() passing in the thread handle, pointer to a thread function to execute, and an arbitrary argument. It makes sense to pass in the connection handle (named "pool") that you are going to be sharing amongst all the happy threads.

Now, you must visualize several instances of the worker() function starting up, each with a unique thread handle and a copy of the pool handle. However, before the worker thread can do anything useful, it's got to get its own connection handle via OCI_ConnPoolGetConnection(). Your simple-minded app blithely assumes that it will always have enough resources available to get the job done. A real-world app would probably use OCI_ConnPoolSetNoWait(FALSE) to stall out the thread until a connection was freed up. It's also possible to manage connections by timeout using OCI_ConnPoolSetTimeout() to reap connections that have been idled for a specified duration.

At last, when the thread has completed its work in Lines #11-14, it can free its resources and await termination. Meanwhile, back in the main() program, the original program thread is in a rather simple loop where it is merely waiting on each thread to complete and freeing their resources in the same order in which they were launched. OCI_ThreadJoin() is a blocking call, so the main thread in fact sits and does nothing until each thread finishes and then the function returns. If the thread had already been dead, before OCI_ThreadJoin() was called, I believe it might return FALSE, but in your case you don't care to know how or when the child thread departed. All-in-all, the thread and pooling system strikes me as both simple in conception and sophisticated in operation.


It's not every day that I say this, but I can emphatically say that OCILIB implements an SQL database interface that is exactly the way I would design it myself and I would be hard pressed to come up with a better solution for C programmers. Of course, you have barely scratched the surface of what OCILIB could do for you. It would take probably a dozen articles to adequately cover topics such as working with Internal Large Objects (LOBs) and External Large Objects (FILEs), hash tables, binding arrays, fetching multiple rules, PL/SQL integration, and much much more. If I have piqued your curiosity in any way, my goal here has been achieved.

About the Author

Victor Volkman has been writing for C/C++ Users Journal and other programming journals since the late 1980s. He is a graduate of Michigan Tech and a faculty advisor board member for the Washtenaw Community College CIS department. Volkman is the editor of numerous books including, C/C++ Treasure Chest and is the owner of Loving Healing Press. He can help you in your quest for open source tools and libraries; just drop an email to sysop@HAL9K.com

Page 2 of 2

This article was originally published on May 13, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date