I'm trying to create and use Java singleton inside Java UDF just as
Mr Knut Stolze described in
http://www-128.ibm.com/developerwork...lze/index.html
.. But I've the problem because each instance of UDF seams to run in
separate Java runtime environment and has each it's own singleton
instance. For test I created simple java singleton class like this:
public class single {
private static single instance = null;
public int count = 0;
public static synchronized single getInstance(int i)
{
if (instance == null)
{
instance = new single();
instance.count = i;
}
return instance;
}
}
And java UDF using above singleton :
import COM.ibm.db2.app.*;
public class singleTest extends UDF
{
public void test(int p, int wynik)
{
try{
single.getInstance(p).count = single.getInstance(p).count +10;
set(2,single.getInstance(p).count);
} catch (Exception e) {}
}
}
CREATE FUNCTION obrazy.testSingle (val INTEGER )
RETURNS INTEGER
SPECIFIC singleTest
EXTERNAL NAME 'singleTest.test'
LANGUAGE JAVA
PARAMETER STYLE DB2GENERAL
NOT DETERMINISTIC
NOT FENCED
THREADSAFE
CALLED ON NULL INPUT
NO SQL
NO EXTERNAL ACTION
NO SCRATCHPAD
FINAL CALL
DISALLOW PARALLEL
NO DBINFO @
When I run query like this:
SELECT obrazy.testSingle(1) , obrazy.testSingle(2)
FROM TABLE ( VALUES (1, 1, 'a'), (1, 20, 'b'),
(3, 3, 'a'), (4, 4, 'a'), (2, 1, 'a') )
AS t(value, weight, group)
@
I get output
11 12
21 22
31 32
41 42
51 52
Which seems to me to indicate that each UDF instance creates it's own
instance of singleton which is not the way it should be.
I expect the result should by like this
11 21
31 41
51 61
71 81
91 101
Is there anything wrong with my Java code or DB2 Java environment
settings ?
I have no idea where is the problem, if any one can help me it will be
very helpful for
me.
Thanks
Adam