Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Writing Stored Procedures with Oracle Database 12c

1,658 views

Published on

Session from the java2days 2015 conference (http://2015.java2days.com/conference-agenda/)

Published in: Technology
  • Have done some java stored procedure to sign xml documents but have problems because the same code on node A run faster thatn the same code on node B (have 2 nodes which are connected to database). What can be the reason?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Writing Stored Procedures with Oracle Database 12c

  1. 1. Writing Java Stored Procedures with Oracle Database 12c Martin Toshev
  2. 2. Who am I Software consultant (self-employed) BG JUG governance board member (http://jug.bg) OpenJDK contributor
  3. 3. Agenda • PL/SQL vs Java Stored Procedures • Writing Java Stored Procedures • Managing Java Stored Procedures • New features in Oracle Database 12c
  4. 4. PL/SQL vs Java Stored Procedures
  5. 5. 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
  6. 6. 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
  7. 7. PL/SQL vs Java Stored Procedures • Java procedures are executed from a JVM (Java Virtual Machine) process running inside the Oracle RDBMS: Oracle DB Oracle RDBMS PL/SQL JVM Java method
  8. 8. 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)
  9. 9. 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
  10. 10. 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
  11. 11. Writing Java Stored Procedures
  12. 12. 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
  13. 13. Writing Java Stored Procedures • The loadjava utility uses the CREATE JAVA {SOURCE | CLASS | RESOURCE} command to load source/class/resource files
  14. 14. 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
  15. 15. 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';
  16. 16. Writing Java Stored Procedures • Invoke the func function: VARIABLE result VARCHAR2(20); CALL func() INTO :result PRINT result;
  17. 17. 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;
  18. 18. Writing Java Stored Procedures demo
  19. 19. Managing Java Stored Procedures
  20. 20. 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)
  21. 21. 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
  22. 22. 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);
  23. 23. 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)
  24. 24. Managing Java Stored Procedures demo
  25. 25. New Features in Oracle Database 12c
  26. 26. 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)
  27. 27. New features in Oracle Database 12c • Native Oracle JVM support for JNDI • Customizing the default java.security resource
  28. 28. 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)
  29. 29. Thank you ! Q&A

×