By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,264 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

Problems with using singleton inside Java UDF in DB2

P: n/a
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

Jan 6 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Adam Duszenko wrote:
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 ?
No, there isn't. I think by article isn't detailed enough to explain how
DB2 manages JVM and class loaders. The thing is that each UDF uses its
own, specific DB2 class loader. That way, DB2 ensures that no two UDFs can
interfere with each other - both could be run by different SQL sessions,
after all. So what you would have to do is to overcome DB2's UDF-specific
class loader objects. I have described this here http://tinyurl.com/yl6m2x
on page 105, section "Process model", in more detail.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Jan 7 '07 #2

P: n/a
Knut,

Thanks for sharing your PhD thesis with public. :). It seems that it
has answers to tricky questions. I'll start digging it. :))

BTW: I always insert my generic classes into sqllib/java/db2java.zip.
By doing so, I guess I am avoiding CLASSPATH restriction enforced by
DB2 Class Loader. Is this a good practice?

Thanks,

Mehmet Baserdem

Jan 8 '07 #3

P: n/a
Thank you for your answer, but I'm afraid that I still have not
enough information to include my singleton class with other DB2 Java
classes in the restricted classpath that is searched by the system
class loader.
Should I copy my singleton into c:\Program
Files\IBM\SQLLIB\java\db2java.zip or anywhere else ? db2java.zip is
protected against modification.

Adam Duszenko

Jan 8 '07 #4

P: n/a
Adam,

I am able to modify db2java.zip file. AFAIK it is always unprotected.
You can try inserting your java classes as follows:

db2java.zip\COM\ibm\db2\your_folder_name\your_java .class

!!! Keep a backup of your java files in case you may need to reinsert
them. (i.e. when rebuilding your db2 server)
Regards,

Mehmet


On Jan 8, 1:50 pm, "Adam Duszenko" <gucioS...@gmail.comwrote:
Thank you for your answer, but I'm afraid that I still have not
enough information to include my singleton class with other DB2 Java
classes in the restricted classpath that is searched by the system
class loader.
Should I copy my singleton into c:\Program
Files\IBM\SQLLIB\java\db2java.zip or anywhere else ? db2java.zip is
protected against modification.

Adam Duszenko
Jan 9 '07 #5

P: n/a
Adam,

I am able to modify db2java.zip file. AFAIK it is always unprotected.
You can insert your java class as follows:

db2java.zip\COM\ibm\db2\app\your_java.class

!!! Keep a backup of your java files in case you may need to reinsert
them. (i.e. when rebuilding your db2 server. God knows for any other
reason)

Regards,

Mehmet

Jan 9 '07 #6

P: n/a
Adam,

I am able to modify db2java.zip file. AFAIK it is always unprotected.
You can insert your java class as follows:

db2java.zip\COM\ibm\db2\your_folder\your_java.clas s

and call it like this

import COM.ibm.db2.your_folder.*;

!!! Keep a backup of your java files in case you may need to reinsert
them. (i.e. when rebuilding your db2 server. God knows for any other
reason)

Regards,

Mehmet

Jan 9 '07 #7

P: n/a
Mehmet Baserdem wrote:
BTW: I always insert my generic classes into sqllib/java/db2java.zip.
By doing so, I guess I am avoiding CLASSPATH restriction enforced by
DB2 Class Loader. Is this a good practice?
No, it is not good practice. The protection of the db2java.zip file (it is
protected because the "sqllib/java" is actually a link to the system-wide
installation of DB2) is there for a reason. You could very easily run into
concurrency issues with multiple connections and/or multiple UDF/SP
invocations. So I would always recommend to put the classes and JAR files
in the proper directory, i.e. sqllib/function/.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Jan 9 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.