Improve company productivity with a Business Account.Sign Up

x
?
Solved

how can i call a simple sql pl-defined function using jdbc?

Posted on 2008-06-22
5
Medium Priority
?
989 Views
Last Modified: 2008-06-30
i created - in the db2 development center -  a simple scalar sql pl-based udf that returns the next sequence number generated by a  sequence object (see code snippet).

the function runs perfectly in the db2 development center. it also runs perfectly from the db2 command line processor. so, i'd expect it to run with the attached code from a java app (see code snippet).

i'm doing the exact same thing as above in an oracle db (except for the db2-specific stuff, of course), and it works perfectly. but with db2, the code results in the exception attached (see code snippet).

i've been wrestling with this for the past 24 hours! i've googled my brains out searching for a solution. i've found this page describing "Reason code: "4".  SQLSTATE=42724":

  http://publib.boulder.ibm.com/infocenter/db2luw/v8/topic/com.ibm.db2.udb.doc/core/rsql0400.htm

it's just a simple function! there ain't no shared libraries or dlls involved! how come i can't call it from java like i can with an analogous function defined in an oracle db? please, don't tell me that i have to go through all the rigamarole here describing what's done in the examples in $DB2/SQLLIB/samples/java/jdbc:

   http://publib.boulder.ibm.com/infocenter/db2luw/v8/topic/com.ibm.db2.udb.doc/ad/t0007049.htm

thanks in advance for your replies.





db2level:
 
DB21085I  Instance "DB2" uses "32" bits and DB2 code release "SQL08023" with level identifier "03040106".
Informational tokens are "DB2 v8.1.10.812", "s050811", "WR21362", and FixPak "10".
 
================
 
CREATE FUNCTION suncertified.get_next_trans_id(  )
    RETURNS INTEGER
    SPECIFIC suncertified.get_next_trans_id
------------------------------------------------------------------------
-- SQL UDF (Scalar)
------------------------------------------------------------------------
F1: BEGIN ATOMIC
   DECLARE aVar INTEGER;
    set aVar = nextval for iau_transaction_seq;
   RETURN aVar;
END
 
================
 
   db2 =>select suncertified.get_next_trans_id() from sysibm.sysdummy1 
   
   1
   -----------
              12
 
   1 record(s) selected.
================
 
 
...
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
Connection conn = DriverManager.getConnection("jdbc:db2:FOO","suncertified, "********");
CallableStatement func = conn.prepareCall(" {? = call suncertified.get_next_trans_id()} ");
func.registerOutParameter(1, java.sql.Types.INTEGER);
func.execute();
int xid = func.getInt(1);
...
 
================
 
COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver][DB2/NT] SQL0444N  Routine "*trans_id" (specific name "SQL080622121524410") is implemented with code in library or path "\get_next_trans_id", function "get_next_trans_id" which cannot be accessed.  Reason code: "4".  SQLSTATE=42724
 
================

Open in new window

0
Comment
Question by:wboyd
  • 3
  • 2
5 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 21841033
when you write a function in sql/pl, db2 translate that function into C code and compiles it
that is why you receive the library path message
make sure that your PATH envrionment variable (if you use windows / linux) or your LIBPATH variable (if you use unix) is set up correctly when you run your java program

if you want to test it you could open a db2 command windows (you just run db2cmd.exe on windows->start) and try to run your java code from there, the variables should be set up correctly inside the console window
0
 
LVL 2

Author Comment

by:wboyd
ID: 21841195
thanks for your reply, momi_sabag,

i did "Start -> Run -> db2cmd.exe"; a windows command shell appeared titled "DB2 CLP". at the command prompt, i added the java class that's trying to call the sql pl udf to the classpath. and then i ran the java class. i got the exact same exception i got hundreds of times in the last 24 hours (see code snippet).

please, can you spell out what you mean by "set up correctly"? in other words, what is a "correct" setup? $DB2HOME/SQLLIB/FUNCTIONS (among other things) is in my PATH. what else needs to be set up?

again, i created the udf in db2's development center. i used the DC's default project directory ("C:/Documents and Settings/%user_dir%/Application Data/IBM/DB2/DC/Projects/..."). i added that project dir to the PATH too - with no luck.

thanks again for trying to help. can anybody think of anything else, please?
...
COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver][DB2/NT] SQL0444N  Routine "*TRANS_ID" (specific name "SQL080622165108270") is implemented with code in library or path "...NS_ID", function "SUNCERTIFIED.GET_NEXT_TRANS_ID" which cannot be accessed.  Reason co
de: "4".  SQLSTATE=42724
 
        at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(SQLExceptionGenerator.java:275)
        at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(SQLExceptionGenerator.java:217)
        at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.check_return_code(SQLExceptionGenerator.java:449)
        at COM.ibm.db2.jdbc.app.DB2PreparedStatement.execute2(DB2PreparedStatement.java:2195)
        at COM.ibm.db2.jdbc.app.DB2PreparedStatement.execute(DB2PreparedStatement.java:3967)
        at ...UDFTester.main(UDFTester.java:39)
got: 0
 
 
===========
 
echo %PATH%
...;C:\DEVELO~1\IBM\SQLLIB\BIN;C:\DEVELO~1\IBM\SQLLIB\FUNCTION;C:\DEVELO~1\IBM\SQLLIB\SAMPLES\REPL;... 

Open in new window

0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 21841208
when i say - set up correctly i mean
check what the path variable contains when you run your clp session which you say works fine with the above function
then, copy the value from the path variable from there to the path variable in the console window in which you run your java application
0
 
LVL 2

Author Comment

by:wboyd
ID: 21841440
while in the 1st clp command window in which i mentioned that i successfully ran the udf, i typed "quit" to end the clp session. then - while in that same window - i typed "echo %PATH%". and it reported that the PATH values were exactly the same as the values in the 2nd command window that i launched with "Start -> run -> db2cmd.exe".

as soon as it finishes downloading, i plan to apply fixpack #11 to see if that makes any difference.
0
 
LVL 2

Accepted Solution

by:
wboyd earned 0 total points
ID: 21841965
it turns out that sql pl udfs are NOT callable statements  (like sprocs are). with db2, sql pl-defined functions can only be used as part of an sql statement. who knew?

so instead of "{? = call suncertified.get_next_trans_id }", now i do a select like i did from the clp (see code snippet)

live and learn! ;¬)
...
Statement stmt = conn.createStatement();
ResultSet result = stmt.executeQuery("select get_next_trans_id() from sysibm.sysdummy1");
result.next();
int xid = result.getInt(1); 
...

Open in new window

0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

How much do you know about the future of data centers? If you're like 50% of organizations, then it's probably not enough. Read on to get up to speed on this emerging field.
Exchange database can often fail to mount thereby halting the work of all users connected to it. Finding out why database isn’t mounting is crucial and getting the server back online. Stellar Phoenix Mailbox Exchange Recovery is a champion product t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…

584 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question