If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Multi-Threading in external procedures (DLL, LANGUAGE C)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-02-10, 04:56
willybrandt willybrandt is offline
Registered User
 
Join Date: Jan 2010
Posts: 2
Multi-Threading in external procedures (DLL, LANGUAGE C)

Hi there,

i hope, someone can help me with the following (tricky!) problem. Sorry for writing so much, but I felt, it's the minimum for properly describing my problem.

I created a stored procedure on my DB2 Express-C v9.7.0.441, WinXP System. The definition looks like this:
Code:
CREATE PROCEDURE MYSCHEMA.MYPROC
SPECIFIC MYPROC
DYNAMIC RESULT SETS 0
MODIFIES SQL DATA
LANGUAGE C
EXTERNAL NAME 'D:\Projects\Libs\release\Myproc.dll!DoSomething'
NOT FENCED
PARAMETER STYLE GENERAL
Myproc.dll is an MFC DLL, that uses the DB2 CLI API to connect with the database that invoked the external procedure. The DLL does not create any kind of UI (which would be blocked by DB2).

DoSomething is an exported function of the DLL with no return value and no parameters. It creates several worker-threads that perform some computations and then insert data into the database.

When invoking the function from a separate .exe it all works well. Unfortunately when the same function is invoked by DB2 through a call of the external procedure (i.e. "call myschema.myproc;") it does not work. Debugging showed that the main thread (which creates the worker-threads) successfully connects to the database and also successfully executes several SELECT statements on it. However, the first time when one of the worker-threads tries to execute a SELECT-statement it fails (SQLExecDirect returns code INVALID_HANDLE). This happens because the statement handle passed to SQLExecDirect indeed is invalid: Although the prior call to SQLAllocHandle returned SQL_SUCCESS, the new handle is negative, while the statement handles allocated in the main thread were all positive.

So it appears to me, that DB2 for some reason accepts queries only from the main thread of the DLL, while rejecting queries from its sub-threads. How can I resolve this? Does it have anything to do with the definition of the procedure (e.g. FENCED vs. NOT FENCED). It's really crucial for me to get this thing to work and thus I'd really appreciate any suggestions.

Best Regards,
Wilfried
Reply With Quote
  #2 (permalink)  
Old 02-02-10, 05:05
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Using multiple threads inside a routine (stored procedure or UDF) is not supported. The reason is that your routine runs inside a DB2 process. But DB2 has no control over the threads you may create and, thus, cannot guarantee proper functioning and proper cleanup when the procedure ends. Therefore, using multiple threads in a routine is not supported and DB2 does a few things to prevent you from doing that, e.g. not allowing separate connections from other threads.

p.s: Things are different for DB2 z/OS where a routine runs in a WLM environment and is seen by DB2 just like any other client application (for the most part). But that won't help you much on DB2 LUW, though.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #3 (permalink)  
Old 02-02-10, 05:23
willybrandt willybrandt is offline
Registered User
 
Join Date: Jan 2010
Posts: 2
Not actually good news, but at least I can stop trying now and work on a different solution... Anyway, thanks for the quick reply.
Reply With Quote
Reply

Tags
cli, dll, external procedures, multi-threading, procedure

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On