JavaVM

From Oracle FAQ
Jump to: navigation, search

JServer or JavaVM is a database resident JVM (Java Virtual Machine) that allows Java programs to run within the Oracle database server. Oracle also provides a JServer Accelerator to compile Java code natively. This speeds up the execution of Java code by eliminating the interpreter overhead. Users can write procedures, packages and functions in Java and map it to the PL/SQL layer.

Contents

[edit] Install JavaVM

JavaVM can be installed with the DBCA utility (GUI) or manually (command line).

Manual steps:

  • Make sure your database is started with large java_pool_size (>20M) and shared_pool_size (>50M) INIT.ORA parameter values.
  • Run the $ORACLE_HOME/javavm/install/initjvm.sql script from SYS AS SYSDBA to install the Oracle JServer Option on a database.
cd $ORACLE_HOME/javavm
sqlplus "SYS/ AS SYSDBA" @initjvm.sql
  • Grant JAVAUSERPRIV to users that wants to use Java:
SQL> GRANT JAVAUSERPRIV TO SCOTT;

Follow the steps in the Oracle Upgrade Guide to upgrade or downgrade the JServer option from one release to another.

[edit] Remove JavaVM

JavaVM can be deinstalled with the DBCA utility (GUI) or manually (command line).

Manual method - use the rmjvm.sql script to deinstall the JServer option from your database:

cd $ORACLE_HOME/javavm
sqlplus "SYS/ AS SYSDBA" @rmjvm.sql

[edit] Writing Java Stored procedures

A Java Stored Procedure is a procedure coded in Java (as opposed to PL/SQL) and stored in the Oracle database. Java Stored procedures are executed by the database JVM in database memory space.

Java Stored Procedures can be developed in JDBC or SQLJ. Interfacing between PL/SQL and Java are extremely easy. Please note that Java Stored procedures are by default executed with invokers rights. PL/SQL procedures are by default executed with defines rights.

Java Stored Procedures must comply with the following rules:

  • No constructor method is needed
  • Variables and methods must be declared [b]static[/b]
  • Use the default database connection (no userid/ password required, run in session)
  • Declare output variables as arrays

Example declaration:

public static void getEmpInfo(int empno, String[] empName, int[] salary) {
...

Console output from System.out.println() statements will be written to trace files in the Oracle UDUMP destination directory.

Note: JSP is Java Server Pages and NOT Java Stored Procedures.

[edit] Load Java Source code into the database

Create a deployment profile from JDeveloper or use the "CREATE OR REPLACE JAVA SOURCE" command or "loadjava" utility. See this script for an example.

Loaded code can be viewed by selecting from the USER_SOURCE view.

[edit] Execute Java in the database

  • From SQL*Plus: Use the execute or call commands
  • From JDBC: Use the CallableStatement object
  • From SQLJ: #sql { call ...} eg:
#sql { call ACME.BEGINRENTAL(:in memberID, :in employeeID, :out, ...) };

[edit] Removing Java from the database

Use these commands to drop a Java Stored Procedure from the database:

  • From SQL*Plus: drop java class .../ drop package ... (drop the wrapper)
  • dropjava utility: dropjava -u scott/tiger myPackage.myClass

[edit] Publish Java in the database

Publishing Java classes in the database makes it visible on a SQL and PL/SQL level. It is important to publish your code before calling it from SQL statements or PL/SQL code. Look at this example:

create or replace function hello (str varchar2) return varchar as
  language java name 'Hello.Msg(java.lang.String) return java.lang.String';
/