473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problems with using singleton inside Java UDF in DB2

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
7 2647
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: lawrence | last post by:
To call I would do something like: $headline = McSelectJustOneField::callDatastore("cbHeadline"); Is this the correct use of the static keyword, to implement a Singleton design?
3
by: Saruman | last post by:
Unfortunately I am not able to find a direct answer to this question neither in documentation nor in newsgroups. I have an application biggest part of which is dynamically reloaded in certain...
3
by: Alan Krueger | last post by:
Greetings, I've been able to cache Transformer objects in a Tomcat-based servlet application to avoid unnecessary Transformer rebuilding, except for certain ones on certain machines. I'm...
0
by: Brand-X | last post by:
I am using a Java Singleton to do some work that the xsl cannot manage and maintain state, however, for no reason that I can fathom the transformer gives the following error: E...
1
by: Jens Mueller | last post by:
Hi there, this is a Java-XML Question, so I am not sure whether this is the right place, haven't found anything better .... I try to convert a Java object to XML via SAX and let the FOP...
5
by: Damien | last post by:
Hi all, I'm using a pretty standard C++ Singleton class, as below: template <typename T> class Singleton { public: static T* Instance() {
3
by: wizwx | last post by:
There are two typical implementations of a singleton. The first one is to use a static pointer class Singleton { static Singleton * pSingleton; public: Singleton * instance() {...
20
by: Aek | last post by:
We recently moved our large codebase over from VS7 to 8 and found that we now get access violations in atexit calls at shutdown when debugging the application in VS2005. This occurs in static...
0
by: George Sakkis | last post by:
On Jul 3, 6:58 pm, Urizev <uri...@gmail.comwrote: Because __init__() is called to initialize the state of an object *after* it has already been created. You should create a "new-style" class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.