Bob Sparks wrote:
Could you possibly describe your scenario/problem in more detail so that
we
know and understand the real requirements? Maybe we can come up with a
solution for you.
I believe that even a SCRATCHPAD, if it were available for LANGUAGE SQL
functions, would not be any help for you.
Thanks Knut .. I hope you are still reading this thread :-)
We want to log who changed which record. Now as with many
installations we are not using RDBMS authentication. We use our own
security as we have row level security based on the enterprise
directory. So each transaction runs from a connection pool. At the
time the connection is handed from the pool I would like to write the
users enterprise id into a scratchpad.
Ok, I think I get the idea. You like to have some sort of register/storage
where you can put some information, i.e. the user name.
My understanding is that any
code running on that connection would get the same scratchpad. So
therefore.
That's not correct. Each UDF (that can have a scratchpad) gets its own
memory area. Even if you call the same UDF within a single statement more
than once, then each occurrence of the UDF would have its own, independent
scratchpad. Things become worse if the DB2 optimizer changes the plan is
such a way that the number of occurrences of the UDF changes or if you have
a partitioned environment.
For example, the following statement has 3 different, independent scratchpad
areas.
SELECT UDF1(), UDF2()
FROM table
WHERE UDF1() = <something>
You have several options to implement your scenario.
(1) Use a base table to store your data at the beginning of the connection
and remove it at the end. Of course, other connections would access the
same table and you get a bunch of problems (concurrency, how to clean up
stale entries if the application crashes, etc.) So I wouldn't do that.
(2) Use a global temporary table. At the beginning of the connection, you
create the temp table (or clean an existing one) and store the relevant
information. If the connection to DB2 is terminated (without the pooling),
then DB2 will remove the table. And global temp tables are only visible in
the current connection.
So you would do something like this:
1. DECLARE GLOBAL TEMPORARY TABLE ...
2. INSERT INTO <temp-table> ...
3. Upon insert, fire a trigger that runs a SELECT on the temp table and gets
the information you want
(3) Write your own external function to store the data somewhere else like
in the file system or shared memory. For that, you would have to resort to
Java or C/C++ code and that's not something you wanted. Besides, you will
still have to solve some problems if you have a partitioned database.
(4) You could "abuse" one of DB2's special registers like "CURRENT SCHEMA"
or so. But please be aware of the impact it may have on the SQL statement
you execute.
--
Knut Stolze
Information Integration
IBM Germany / University of Jena