Top Banner
Writing Java Stored Procedures with Oracle RDBMS Martin Toshev
31

Writing Stored Procedures in Oracle RDBMS

Apr 16, 2017

Download

Technology

Martin Toshev
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Writing Stored Procedures in Oracle RDBMS

Writing Java Stored Procedureswith Oracle RDBMS

Martin Toshev

Page 2: Writing Stored Procedures in Oracle RDBMS

Who am I

Software consultant (CoffeeCupConsulting)

BG JUG board member (http://jug.bg)

OpenJDK and Oracle RDBMS enthusiast

Twitter: @martin_fmi

Page 3: Writing Stored Procedures in Oracle RDBMS
Page 4: Writing Stored Procedures in Oracle RDBMS

Work in progress …

Page 5: Writing Stored Procedures in Oracle RDBMS

Agenda

• PL/SQL vs Java Stored Procedures

• Writing Java Stored Procedures

• Managing Java Stored Procedures

• New features in Oracle Database 12c

Page 6: Writing Stored Procedures in Oracle RDBMS

PL/SQL vs Java Stored Procedures

Page 7: Writing Stored Procedures in Oracle RDBMS

PL/SQL vs Java Stored Procedures• Both PL/SQL and Java stored procedures are executed

directly on the RDBMS

• Both PL/SQL and Java stored procedures can be recompiled dynamically when source code changes

• Easier migration of Java stored procedures to/from the application tier

Page 8: Writing Stored Procedures in Oracle RDBMS

PL/SQL vs Java Stored Procedures• The PL/SQL Virtual Machine (PVM) is the database

component that executes the PL/SQL bytecode (lower lever representation of the PL/SQL code that is generated from the PL/SQL compiler)

• PVM is written in C

Page 9: Writing Stored Procedures in Oracle RDBMS

PL/SQL vs Java Stored Procedures• Java procedures are executed from a JVM (Java Virtual

Machine) process running inside the Oracle RDBMS:

OracleDB

OracleRDBMS PL/SQL JVM

Java method

Page 10: Writing Stored Procedures in Oracle RDBMS

PL/SQL vs Java Stored Procedures• Since the Oracle JVM is embedded in the database this introduces a

number of new concepts:

– Oracle JVM process that runs within an Oracle database session

– classloader that loads classes from the database

– no notion of main() method

– loading of system classes from the SYS schema (where they are stored)

Page 11: Writing Stored Procedures in Oracle RDBMS

PL/SQL vs Java Stored Procedures• Since the Oracle JVM is embedded in the database this introduces a

number of new concepts:

– oracle.aurora.rdbms.DbmsJava.classForNameAndSchema() for loading a class from a database schema

– The Oracle JVM uses the database memory structures to store data

– The garbage collector makes use of the call and session memory on a per-user basis

Page 12: Writing Stored Procedures in Oracle RDBMS

PL/SQL vs Java Stored Procedures• Since the Oracle JVM is embedded in the database this introduces

a number of new concepts:

– no support for JNI

– server-side JDBC driver providing access to Oracle data (using the "jdbc:default:connection:“ connection URL)

– server-side SQLJ translator – allows embedding of SQL statements in Java stored procedures

Page 13: Writing Stored Procedures in Oracle RDBMS

Writing Java Stored Procedures

Page 14: Writing Stored Procedures in Oracle RDBMS

Writing Java Stored Procedures• In order to create a Java stored procedure:

o write the Java class that contains the Java procedure(s) (static method(s) in the class)

o load the compiled Java procedure in the Oracle database using the loadjava utility or the CREATE JAVA command

o map a PL/SQL function/procedure to the Java procedure using the CREATE FUNCTION/PROCEDURE command

Page 15: Writing Stored Procedures in Oracle RDBMS

Writing Java Stored Procedures

• The loadjava utility uses the CREATE JAVA {SOURCE | CLASS | RESOURCE} command to load source/class/resource files

Page 16: Writing Stored Procedures in Oracle RDBMS

Writing Java Stored Procedures

• Example: loading the Sample.java source file using the orcl user:

• Example: loading the Sample.java source file using the orcl user and compiling it:

loadjava -u orcl Sample.java

loadjava -u orcl –resolve Sample.java

Page 17: Writing Stored Procedures in Oracle RDBMS

Writing Java Stored Procedures

• Map the Java static function func() from the Sample class:

CREATE OR REPLACE FUNCTION func RETURN VARCHAR2 AS LANGUAGE JAVA NAME ‘Sample.func() return java.lang.String';

Page 18: Writing Stored Procedures in Oracle RDBMS

Writing Java Stored Procedures

• Invoke the func function:

VARIABLE result VARCHAR2(20); CALL func() INTO :resultPRINT result;

Page 19: Writing Stored Procedures in Oracle RDBMS

Writing Java Stored Procedures

• You can provide privileges for other users to invoke your class(es) using the loadjava utility or the GRANT command:loadjava -grant usr –u orcl Sample.java

GRANT EXECUTE ON Sample TO usr;

Page 20: Writing Stored Procedures in Oracle RDBMS

Writing Java Stored Proceduresdemo

Page 21: Writing Stored Procedures in Oracle RDBMS

Managing Java Stored Procedures

Page 22: Writing Stored Procedures in Oracle RDBMS

Managing Java Stored Procedures

• USER_OBJECTS table (OBJECT_TYPE columns is any of JAVA SOURCE, JAVA CLASS or JAVA RESOURCE in a valid/invalid state)

Page 23: Writing Stored Procedures in Oracle RDBMS

Managing Java Stored Procedures• JMX can be used to monitor the Oracle JVM

• The current user must be granted the JMXSERVER role

• The dbms_java.start_jmx_agent can be used to start the JMX server for the session

Page 24: Writing Stored Procedures in Oracle RDBMS

Managing Java Stored Procedures• Java stored procedures can be debugged be debugged by

a JWDP-compliant debugger

• Such as a debugger is provided by jdb and the JDeveloper IDE

exec DBMS_DEBUG_JDWP.CONNECT_TCP('localhost', 6666);

Page 25: Writing Stored Procedures in Oracle RDBMS

Managing Java Stored Procedures• Compiler options can be specified:

– in the JAVA$OPTIONS table– via the loadjava utility – via the DBMS_JAVA package (that creates/modifies

the JAVA$OPTIONS table)

Page 26: Writing Stored Procedures in Oracle RDBMS

Managing Java Stored Proceduresdemo

Page 27: Writing Stored Procedures in Oracle RDBMS

New Features in Oracle Database 12c

Page 28: Writing Stored Procedures in Oracle RDBMS

New features in Oracle Database 12c• Support for multiple JDK versions (JDK 6 by default but

JDK 7 or earlier can be specified)

• Considering multitenant databases introduced in 12c: PDBs (pluggable databases) share the same JDK version specified over the CDB (container database)

Page 29: Writing Stored Procedures in Oracle RDBMS

New features in Oracle Database 12c

• Native Oracle JVM support for JNDI

• Customizing the default java.security resource

Page 30: Writing Stored Procedures in Oracle RDBMS

New features in Oracle Database 12c• Enhanced support for logging properties lookup

• Secure use of Runtime.exec

• Improved debugging support for Java Stored Procedures (watchpoints/breakpoints)

Page 31: Writing Stored Procedures in Oracle RDBMS

Thank you !

Q&A