Writing Stored Procedures in Oracle 12c

Preview:

Citation preview

Writing Java Stored Procedureswith Oracle Database 12c

Martin Toshev,BGOUG, 13.06.2015

Who am I

Software engineer @ EPAM Bulgaria

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

OpenJDK contributor

Agenda

• PL/SQL vs Java Stored Procedures

• Writing Java Stored Procedures

• Managing Java Stored Procedures

• New features in Oracle Database 12c

PL/SQL vs Java Stored Procedures

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

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

PL/SQL vs Java Stored Procedures

• Java procedures are executed from a JVM (Java Virtual Machine) process running inside the Oracle RDBMS:

OracleDB

OracleDB

OracleRDBMSOracle

RDBMS PL/SQLPL/SQL JVMJVM

Java method

Java method

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)

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

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

Writing Java Stored Procedures

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

Writing Java Stored Procedures

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

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

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';

Writing Java Stored Procedures

• Invoke the func function:

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

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;

Writing Java Stored Proceduresdemo

Managing Java Stored Procedures

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)

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

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);

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)

Managing Java Stored Proceduresdemo

New Features in Oracle Database 12c

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)

New features in Oracle Database 12c

• Native Oracle JVM support for JNDI

• Customizing the default java.security resource

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)

Thank you !

Q&A

Recommended