A hitchhiker’s guide to SQL/MX Stored Procedures
Upcoming SlideShare
Loading in...5
×
 

A hitchhiker’s guide to SQL/MX Stored Procedures

on

  • 456 views

Presented at NonStop Advanced Technical Bootcamp, San Jose, 2013. ...

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

Statistics

Views

Total Views
456
Views on SlideShare
455
Embed Views
1

Actions

Likes
2
Downloads
10
Comments
0

1 Embed 1

http://193.175.161.180 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • SQL/MP and SQL/MX started supporting SP because of “Common Standards” and to support porting software to NonStop.
  • The classes and method signatures are important, because they need to match with the definition laterNote the special JDBC connection URL for SPJs.
  • Note that “sp1reader” is a SQL OBJECT and is not case-sensitive.
  • Note that “sp1reader” is a SQL OBJECT and is not case-sensitive.In Embedded SQL, a cursor definition would be required since the SP returns a result set.

A hitchhiker’s guide to SQL/MX Stored Procedures A hitchhiker’s guide to SQL/MX Stored Procedures Presentation Transcript

  • A hitchhiker’s guide to SQL/MX Stored Procedures Connect Advanced Technical Bootcamp Frans Jongma, ATC / November, 2013 © 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 Galaxy Douglas Adams © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Agenda Intro: Why Stored Procedures? Architecture: SQL/MX SPJ Configure: Influencing the SPJ runtime environment Optimize: Using Module File Caching Exceptions: Logging & error reporting 3 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Intro © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • 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, 5 „UPDATE‟ ); © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Why Stored procedures? One problem, different solutions. •DMBS vendors without a transaction processing engine Stored Procedures •NonStop SQL Primarily Pathway servers – less immediate need for SPs •NonStop SQL/MP SP implementation under TS/MP used by ODBC clients •NonStop SQL/MX Stored Procedures in Java (SPJ) 6 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Procedural Languages for SPs One problem, different programming languages SP Language SP Language IBM DB2 Java SQL PL, close to ANSI SQL/PSM HP NonStop SQL/MX Java Microsoft SQL Server Transact-SQL or .NET MySQL Close to ANSI SQL/PSM Oracle Sybase ASE 7 © Copyright 2012 Hewlett-Packard (source: Wikipedia) Development Company, L.P. Java PL/SQL Transact-SQL The information contained herein is subject to change without notice.
  • Architecture © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • 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 9 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • 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"); } } 10 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Architecture of SQL/MX SPJ Defining the procedure (for example: mxci) create procedure SP1Reader( IN tableName varchar(255) ) dynamic result sets 1 external name 'SP1Reader.Reader(java.lang.String, java.sql.ResultSet[])' external path '/home/frans/spj/spj.jar' reads SQL DATA language java parameter style java; • 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 11 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Architecture of SQL/MX SPJ Defining the procedure (for example: mxci) /home/frans/spj/spj.jar Create procedure SP1 ( ) ….. ; OSS Filesystem SQL CATALOG Procedure SP1 () external name 'SP1.READER(java.lang.String, java.sql.ResultSet[])' external path '/home/frans/spj/spj.jar„ 12 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Architecture of SQL/MX SPJ Defining the procedure Definition 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. 13 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Invoke © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • 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(); 15 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. }");
  • 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‟); 16 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Architecture of SQL/MX SPJ Invoking a normal SQL call mxcmp for application …. Select a, b from T…. SQL Database Table T ; Table T2: 17 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Architecture of SQL/MX SPJ Invoking the procedure mxcmp for application MXUDR JVM …. Select x,y from T2 …… mxcmp for mxudr SQL prepare Call SP1(); SQL Execute Call… …. Select a, b from T…. SQL Database Table T ; Table T2: 18 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice. OSS Filesystem /home/frans/spj/spj.jar
  • Configure © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Influencing and Configuring SQL/MX SPJ You 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 20 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Influencing and Configuring SQL/MX SPJ The MXUDR process provides the run-time SPJ environment Embedded SQL program CALL lowerprice() JDBC/MX Application CALL lowerprice() Stored Procedure samdb.sales.lowerprice() SPJ Method Sales.lowerPrice() JDBC Type 2 driver ODBC/MX client application CALL lowerprice() MXUDR embedded JVM NonStop SQL database 21 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Influencing and Configuring SQL/MX SPJ You 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 . 22 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Influencing and Configuring SQL/MX SPJ How to set the UDR_JAVA_OPTIONS UDR_JAVA_OPTIONS contains the set of System properties used by the JVM SQL/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' ; • 23 Can be set as an environment variable called _JAVA_OPTIONS where applicable – ENV _JAVA_OPTIONS=”-Djdbcmx.maxPoolSize=1 -Djdbcmx.maxStatements=100” © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Optimize © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • 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 25 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • MFC Typical usage Use in production environments that are relatively stable First compilations have overhead Stale modules are not automatically removed Applications use specific compiled_module_location One 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 26 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Configuration options One: Instruct driver to use MFC T2: -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 SPJs UDR_JAVA_OPTIONS “-Djdbcmx.enableMFC=ON Djdbcmx.compiledModuleLocation=/home/…. “ 27 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Logging © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Logging and error reporting Designated SQL/MX error codes for SPJ user exceptions Use 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"); } 29 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Logging error and reporting Using Apache log4j Add “-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 "/> 30 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Summary Intro: Why Stored Procedures? Architecture: SQL/MX SPJ Configure: Influencing the SPJ runtime environment Optimize: Using Module File Caching Exceptions: Logging & error reporting 31 © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.
  • Further reading Concepts of NonStop SQL/MX Part 5: Stored Procedures HP Technical Whitepaper 4AA4-9428ENW.pdf Module File Caching for NonStop SQL/MX HP Technical Whitepaper 4AA3-8922ENW.pdf Other 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 32 © 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” © Copyright 2012 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice.