7. 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
8. 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
9. 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
10. 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)
11. 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
12. 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
14. 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
15. Writing Java Stored Procedures
• The loadjava utility uses the
CREATE JAVA {SOURCE | CLASS |
RESOURCE}
command to load source/class/resource files
16. 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
17. 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';
18. Writing Java Stored Procedures
• Invoke the func function:
VARIABLE result VARCHAR2(20);
CALL func() INTO :result
PRINT result;
19. 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;
22. 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)
23. 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
24. 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);
25. 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)
28. 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)
29. New features in Oracle Database 12c
• Native Oracle JVM support for JNDI
• Customizing the default java.security resource
30. 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)
The Java process runs within an Oracle database session – this is similar to how a JVM runs in a standalone OS process
server-side JDBC driver is tightly integrated with the Oracle database in order to provide fast access to Oracle data
- The SQLJ translator translates Java code with SQL and creates the proper Java code that executes the statements out of those SQL statements (using the server side JDBC translator)
The call memory is further divided into an old and new space
The Java process runs within an Oracle database session – this is similar to how a JVM runs in a standalone OS process
server-side JDBC driver is tightly integrated with the Oracle database in order to provide fast access to Oracle data
- The SQLJ translator translates Java code with SQL and creates the proper Java code that executes the statements out of those SQL statements (using the server side JDBC translator)
You can also specify a different schema (e.g. SAMPLE) using the –schema parameter. For example:
loadjava -u orcl -schema SAMPPLE Sample.java
You can also specify a different schema (e.g. SAMPLE) using the –schema parameter. For example:
loadjava -u orcl -schema SAMPPLE Sample.java
You can also specify a different schema (e.g. SAMPLE) using the –schema parameter. For example:
loadjava -u orcl -schema SAMPPLE Sample.java
You can also specify a different schema (e.g. SAMPLE) using the –schema parameter. For example:
loadjava -u orcl -schema SAMPPLE Sample.java
You can also specify a different schema (e.g. SAMPLE) using the –schema parameter. For example:
loadjava -u orcl -schema SAMPPLE Sample.java
There are some prerequisites for enabling debugging for a user – the user must be granted the DEBUG ANY PROCEDURE and DEBUG CONNECT SESSION priviliges and assigned to an ACL:
begin
sys.DBMS_NETWORK_ACL_ADMIN.APPEND_HOST_ACE(
host => 'localhost',
ace => xs$ace_type(privilege_list => xs$name_list('jdwp'),
principal_name => 'c##demo',
principal_type => xs_acl.ptype_db));
end;
2) When attaching to an Eclipse IDE debugger the current session crashes
There are some prerequisites for enabling debugging for a user – the user must be granted the DEBUG ANY PROCEDURE and DEBUG CONNECT SESSION priviliges and assigned to an ACL:
begin
sys.DBMS_NETWORK_ACL_ADMIN.APPEND_HOST_ACE(
host => 'localhost',
ace => xs$ace_type(privilege_list => xs$name_list('jdwp'),
principal_name => 'c##demo',
principal_type => xs_acl.ptype_db));
end;
2) When attaching to an Eclipse IDE debugger the current session crashes
3) Many people just use plain System.out.println() statements …