33
© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. A hitchhiker’s guide to SQL/MX Stored Procedures Connect Advanced Technical Bootcamp Frans Jongma, ATC / November, 2013

A hitchhiker’s guide to SQL/MX Stored Procedures

Embed Size (px)

DESCRIPTION

Presented at NonStop Advanced Technical Bootcamp, San Jose, 2013. Pictures from "Ruta del Cares" Picos de Europa. Video (not made by me!) that shows the trail is found at http://vimeo.com/75710671

Citation preview

Page 1: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

A hitchhiker’s guide toSQL/MX Stored ProceduresConnect Advanced Technical BootcampFrans Jongma, ATC / November, 2013

Page 2: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

Don’t

Panic!

The Hitchhiker’s guide to the GalaxyDouglas Adams

Page 3: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.3

Agenda

Intro: Why Stored Procedures?

Architecture:SQL/MX SPJ

Configure:Influencing the SPJ runtime

environment

Optimize:Using Module File Caching

Exceptions:Logging & error reporting

Page 4: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

Intro

Page 5: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.5

Why Stored procedures?

A stored procedure is a function or routine available to applications that can access a RDBMS• Typically stored in the database data dictionary• Typically runs “close to the database”

- Within the database engine

- Direct access to the data

• Used to encapsulate business logic • Especially useful in Client-Server environments

• Invoked by CALL or EXECUTE statement• CALL ALERTER ( VALUE_1, VALUE_2, ‘UPDATE’ );

Page 6: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.6

Why Stored procedures?

One problem, different solutions.

•DMBS vendors without a transaction processing engineStored Procedures

•NonStop SQLPrimarily Pathway servers – less immediate need for SPs

•NonStop SQL/MPSP implementation under TS/MP used by ODBC clients

•NonStop SQL/MXStored Procedures in Java (SPJ)

Page 7: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.7

Procedural Languages for SPs

One problem, different programming languages

SP Language SP Language

IBM DB2 JavaSQL PL, close to ANSI SQL/PSM

HP NonStop SQL/MX Java

Microsoft SQL ServerTransact-SQL or .NET

MySQLClose to ANSI SQL/PSM

Oracle Java PL/SQL

Sybase ASE Transact-SQL

(source: Wikipedia)

Page 8: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

Architecture

Page 9: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.9

Architecture of SQL/MX SPJ

The three areas of SQL/MX Stored Procedures in Java

• Developing the procedure– Create the Java method in your favorite development environment– Put the compiled Java class on the server

• Defining the procedure in the SQL/MX metadata– CREATE PROCEDURE DDL command

• Invoking the Stored Procedure– Using mxci, rmxci, COBOL, C, Java

Page 10: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.10

Architecture of SQL/MX SPJ

Developing the procedure (for example: Vim, Eclipse)import java.sql.*;

public class SP1Reader

{ public static void Reader

(String inString, java.sql.ResultSet[] result)throws Exception {

/* here comes the procedure code*/Connection conn = DriverManager.getConnection("jdbc:default:connection");

}}

Page 11: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.11

Architecture of SQL/MX SPJ

Defining the procedure (for example: mxci)

• SQL/MX will try to load the class from the specified location and verify the signature of the SP with the one defined in the DDL.• External path is an OSS filename

create procedure SP1Reader( IN tableName varchar(255) )dynamic result sets 1external name 'SP1Reader.Reader(java.lang.String, java.sql.ResultSet[])'external path '/home/frans/spj/spj.jar'reads SQL DATAlanguage java parameter style java;

Page 12: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.12

Architecture of SQL/MX SPJ

Defining the procedure (for example: mxci)

Create procedure SP1 ( ) ….. ;

Procedure SP1 ()external name 'SP1.READER(java.lang.String, java.sql.ResultSet[])'external path '/home/frans/spj/spj.jar‘

SQL CATALOG

/home/frans/spj/spj.jar

OSS Filesystem

Page 13: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.13

Architecture of SQL/MX SPJ

Defining the procedureDefinition is stored in the SQL/MX metadata

>>select * from zzprocedures;

SP_NAME SP_LANG SP_TYPE SP_RESULTS SP_PATH

--------------- ------- ------- ----------- ---------------------------------------------

READER Java SQL RO 1 /home/hp/frans/projects/spj/spj.jar/SPreader

READER2 Java SQL RO 1 /home/hp/frans/projects/spj/spj.jar/SPreader

SP1READER Java SQL RO 1 /home/hp/frans/projects/spj/spj.jar/SP1Reader

--- 3 row(s) selected.

Page 14: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

Invoke

Page 15: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.15

Architecture of SQL/MX SPJ

Invoking the procedure (1)

• In mxci or rmxci – >> call sp1reader ('EMP');

• In Java – CallableStatement cs = conn.prepareCall("{ CALL sp1reader (‘EMP’) }");– boolean rsAvail=cs.execute();– ResultSet rs = cs.getResulSet();

Page 16: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.16

Architecture of SQL/MX SPJ

Invoking the procedure (2)

• In Embedded SQL– EXEC SQL CALL SP1READER (:HV1) END EXEC;

• (Note: this is simplified --- the SP needs a cursor)

• In a trigger definition

- create trigger salesalertSP after update of (c2) on B

referencing new as newr , old as oldr for each row

when ( newr.c2 > 500 ) CALL SPalerts (oldr.c1,newr.c2, ‘UPDATE’);

Page 17: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.17

Architecture of SQL/MX SPJ

Invoking a normal SQL call

….Select a, b from T….

Table T ;Table T2:

SQL Database

mxcmp for application

Page 18: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.18

Architecture of SQL/MX SPJ

Invoking the procedure

/home/frans/spj/spj.jar

OSS Filesystem

MXUDR JVM….

Select x,y from T2 ……

SQL prepare Call SP1();

SQL Execute Call…….

Select a, b from T….

Table T ;Table T2:

SQL Database

mxcmp for mxudr

mxcmp for application

Page 19: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

Configure

Page 20: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.20

Influencing and Configuring SQL/MX SPJYou do not have to configure the SPJ environment This is a difference from the SQL/MP ODBC environment• where you needed to configure TS/MP

The key component is the MX User Defined Routines MXUDR process

• What is the MXUDR• How does the MXUDR know which configuration options apply• How does is a caller able to set the configuration options

Page 21: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.21

Influencing and Configuring SQL/MX SPJThe MXUDR process provides the run-time SPJ environment

Embedded SQL program CALL lowerprice()

JDBC/MX ApplicationCALL lowerprice()

ODBC/MX client application

CALL lowerprice()

Stored Proceduresamdb.sales.lowerprice()

SPJ MethodSales.lowerPrice()

NonStop SQL database

JDBC Type 2driver

MXUDR embedded JVM

Page 22: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.22

Influencing and Configuring SQL/MX SPJYou do not have to configure the SPJ environment But there are good reasons to do so• Java run -time default: /usr/tandem/java• JDBC T2 driver location default:

/usr/tandem/jdbcMx/current/lib• Class location of the SP (taken from metadata definition)• Catalog and Schema (are the same as the current catalog and schema of the caller)

• Connection pooling and statement caching: set jdbcmx.maxPoolSize and maxStatements property

• Additional class locations: : set java.class.path• Enable MFC : set enableMFC and

compiledModuleLocation• Others like setting JDBC Trace, debugging, logging .

Page 23: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.23

Influencing and Configuring SQL/MX SPJHow to set the UDR_JAVA_OPTIONSUDR_JAVA_OPTIONS contains the set of System properties used by the JVMSQL/MX will pass these parameters when launching the MXUDR object-Djava.class.path=/home/frans/lib/Utils.jar –Djdbcmx.maxPoolsize=100 –D...

• Can be set as a CONTROL QUERY DEFAULT called before the first CALL statement

- CONTROL QUERY DEFAULT UDR_JAVA_OPTIONS

‘-Djdbcmx.maxPoolSize=1 -Djdbcmx.maxStatements=100’;

• Can be set as an MXCS Control

- ADD EVAR $MXOAS.MYDS.UDR_JAVA_OPTIONS, TYPE CONTROL ,

VALUE '-Djdbcmx.maxPoolSize=1 -Djdbcmx.maxStatements=100' ;

• Can be set as an environment variable called _JAVA_OPTIONS where applicable – ENV _JAVA_OPTIONS=”-Djdbcmx.maxPoolSize=1 -Djdbcmx.maxStatements=100”

Page 24: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

Optimize

Page 25: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.25

Optimizing with Module File Caching

What again is Module File Caching?In Summary• MFC is an additional SQL/MX SQL Statement cache

– Available for JDBC T2, JDBC T4 and ODBC (Windows) drivers

• Reduces SQL Compilations• Improves application startup times

• Simplifies application tuning– Dynamic SQL statements do not show in MEASURE– MFC Modules appear in Measure SQLSTMT entity

Page 26: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.26

MFC Typical usage

Use in production environments that are relatively stableFirst compilations have overheadStale modules are not automatically removed

Applications use specific compiled_module_locationOne per MXCS server_datasource One per T2 “application class”of JVMs • Multiple instances of JVM executing the same application services• For example: A dedicated location for all the SPJs that are defined in a schema

Page 27: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.27

Configuration options

One: Instruct driver to use MFCT2: -Djdbcmx.enableMFC=ON JVM option• Program can set this property as well

Two: Instruct driver use a location for modules T2: -Djdbcmx.compiledModuleLocation=<full pathname>

– E.g. /home/SPJ/mfc/modules

Combine these options in UDR_JAVA_OPTIONS for use by SPJsUDR_JAVA_OPTIONS “-Djdbcmx.enableMFC=ON -Djdbcmx.compiledModuleLocation=/home/…. “

Page 28: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

Logging

Page 29: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.29

Logging and error reporting

Designated SQL/MX error codes for SPJ user exceptionsUse SQLSTATE values 38001 through 38999 to signal user-defined error conditions.• Allows an SPJ to communicate error conditions which may or may not be related to SQL.

if ( condition) )

{

throw new java.sql.SQLException("This exception is thrown upon request", "38088");

}

Page 30: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.30

Logging error and reporting

Using Apache log4jAdd “-Dlog4j.configuration=file:/home/spj/log4j.xml” to the UDR_JAVA_OPTIONS

• Little output: all SPs can log to the one file• Verbose output: Use a specific file for each SPJ instance using the Java properties:

• nsk.process.cpu and nsk.process.pin

• <param name="File“ value="/home/hp/frans/spj/SPJ_${nsk.process.cpu}_${nsk.process.pin}.log"/>

Page 31: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.31

Summary

Intro: Why Stored Procedures?

Architecture:SQL/MX SPJ

Configure:Influencing the SPJ runtime

environment

Optimize:Using Module File Caching

Exceptions:Logging & error reporting

Page 32: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.32

Further reading

Concepts of NonStop SQL/MX Part 5: Stored Procedures HP Technical Whitepaper 4AA4-9428ENW.pdfModule File Caching for NonStop SQL/MX

HP Technical Whitepaper 4AA3-8922ENW.pdfOther

www.hp.com/go/nonstop from here Products NonStop SQL White papers

www.slideshare.net/fjongma All “Concepts of NonStop SQL/MX” papers and more

Page 33: A hitchhiker’s guide to SQL/MX Stored Procedures

© Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.

Thank you

“So long and thanks for all the fish”