Thursday, June 7, 2018

Generate Global Unique Identifier (GUID) or Universally Unique Identifier (UUID) from Db2 z/OS

Generate Global Unique Identifier (GUID) or Universally Unique Identifier (UUID) from DB2 z/OS

Here are the steps to generate this  identifier  using DB2 z/OS.
It consists of a DB2 User Defined Function that calls a standard Java Function, this is inspired from the Developer Works  blog page :
https://www.ibm.com/developerworks/community/blogs/SQLTips4DB2LUW/entry/generating_universally_unique_identifiers_uuid63


1/ Compile the java source  below to for example UUIDUDF.class

import java.util.UUID;       // for UUID class

public class UUIDUDF
{
  public static String randomUUID()
  {
    return UUID.randomUUID().toString();
  }
}

Example : javac uuidudf.java 


2/ Create Function :


CREATE FUNCTION SYSFUN.GEN_UID()
  RETURNS  VARCHAR(36)
  LANGUAGE JAVA
  PARAMETER STYLE JAVA
  EXTERNAL NAME 'UUIDUDF.randomUUID'   => name_of_java_class.name_of_function
  WLM ENVIRONMENT CALD_DSN1J_JAVA ;

3/  Modify the CLASSPATH  used by the Java WLM Application Environment STC. Using standard IBM jcl coding , it is referred in the file env.txt coded in your hlq.JAVAENV dataset.
The CLASSPATH must now point to the directory where the UUIDUDF.class has been generated.
When finished, don't forget to submit the command /V WLM, APPLENV=xxxx,REFRESH

4/ That's all , time to test :



Note : The other way to do this is to create a JAR from the .class then copy the JAR to the DB2 catalog using the Stored Procedure SQLJ.DB2_INSTALL_JAR. You must use DB2 Data Studio for this. 
The main difference is that you don't need to modify the Classpath, every thing is coded in the DB2 catalog. 


1 comment: