 |
|
Administration of Java
Objects
Oracle Tips by Burleson Consulting
|
In Oracle 8i, Java must first be loaded to
make use of it; Oracle9i loads the Java VM automatically through the
LoadJava utility, which is described next.
Using LoadJava
Using a built-in PL/SQL package named LoadLobs,
the LoadJava utility uploads Java files into a BLOB column in the
table create$java$lob$table, which the utility creates in the logon
schema. Then it uses the SQL CREATE JAVA statement to load the Java
files into the RDBMS.
LoadJava also accepts SQLJ input files (.sqlj
files). To process them, it invokes the Server SQLJ Translator, which
outputs Java sources and serialized objects for SQLJ profiles (.ser
files). Then it invokes the Java compiler, which compiles the Java
sources into standard binaries. (For more information, refer to the
numerous Java references on the documentation Web site.)
Using a client-side JDBC driver, LoadJava can
upload individual Java sources and binaries, SQLJ input files, and
entire Java archives (JARs). However, it cannot upload individual
resources, so you must put them in a JAR. You can enter .java, .class,
.jar, and .sqlj files on the command line in any order. The syntax is:
loadjava
[{-h |
-help}]
[{-v | -verbose}]
[{-f | -force}]
[{-r | -resolve}]
[{-a | -andresolve}]
[{-S | -schema} schema_name]
[{-d | -definer}]
[{-s | -synonym}]
[{-g | -grant} {username | role_name}[, {username | role_name}]...]
[{-R | -resolver} "resolver_spec"]
[{-e | -encoding} encoding_scheme_name]
[{-t | -thin} | {-o | -oci81}]
[{-u | -user}
username/password@host_name:port_number:database_sid]
{filename.java | filename.class | filename.jar | filename.sqlj} ...
where:
resolver_spec. Specifies schemas to be
searched (in the order listed) for referenced Java fullnames. Its
syntax is:
((match_string
schema_name) (match_string schema_name) ...)
where match_string is a Java fullname; the
wildcard, an asterisk (*),which matches any name; or a combination,
such as home/java/bin/*; and where schema_name is the name of a schema
or the wildcard (which indicates that names matching the pattern
need not be resolved). The default resolver_spec follows:
((*
definer's_schema) (* public))
The following list presents the LoadJava
command-line options:
|
Option |
Description |
|
-h |
Displays a help screen that lists all the
command-line options. |
|
-v |
Enables verbose mode, in which progress
messages are displayed. |
|
-f |
Forces the loading of Java classes whether
or not they have been loaded before. By default, previously loaded
classes are rejected. You cannot force the loading of a binary if
you previously loaded the source. You must drop the source first.
|
|
-r |
Compiles all uploaded sources, then
resolves external references in uploaded Java classes. Otherwise,
sources are compiled and classes are resolved at runtime or when
the Java virtual machine (JVM) needs the definitions to resolve
other classes. If errors occur during name resolution, they are
displayed. |
|
-a |
Works like the -r option except that each
uploaded source is compiled and resolved separately. This lets you
replace a class that might invalidate other classes. |
|
-S |
Assigns newly created Java library units
to the specified schema. Otherwise, the logon schema is used. |
|
-s |
Creates public synonyms for uploaded
classes. To specify this option, you must have the CREATE PUBLIC
SYNONYM privilege. |
|
-d |
Specifies that the methods of uploaded
classes will execute with the privileges of their definer, not
their invoker. |
|
-g |
Grants the EXECUTE privilege on uploaded
classes and resources to the listed users and/or roles. (To call
the methods of a class directly, users must have the EXECUTE
privilege.) |
|
-R |
Specifies a resolver spec, which is bound
to newly created Java library units. Because it contains spaces,
the resolver spec must be enclosed by double quotation marks. |
|
-e |
Sets the encoding option in the database
table java$options to the specified value, which must be a
standard JDK encoding-scheme name. The encoding of uploaded
sources must match the specified encoding. |
|
-t | -o |
Selects the client-side JDBC driver used
to communicate with Oracle. You can choose the Thin JDBC Driver
(the default) or the OCI JDBC Driver. For information about these
drivers, see the JDBC reference on the documentation Web site.
|
|
-u |
Specifies a database connect string
without spaces. The string includes a user name, password,
host-computer name, port number, and database system identifier.
If this option is not specified, the string defaults to
internal/oracle@localhost:1521:orcl. |
Caution: LoadJava uses the hash table
java$class$md5$table to track the loading of Java library units into a
schema (md5 refers to RSA Data Security’s MD5 Message-Digest
Algorithm, which does the hashing). If you use LoadJava to load a Java
library unit, you must use DropJava to drop the unit. Otherwise, the
table will not be updated properly. Also, if you use the SQL
DROP JAVA statement to drop a Java class, and then use LoadJava to
upload the same class, you must specify the force (-f) command-line
option. Otherwise, the upload fails.
LoadJava Examples
In the first example, LoadJava uploads
archived Java binaries and resources from an OS file system (> is the
prompt). The utility reads the JAR files, stores each binary or
resource in a database row keyed by the filename of the class or
resource, then uses the SQL CREATE JAVA statement to load the Java
files into the RDBMS.
> loadjava
-u scott/tiger@myComputer:1521:orcl managers.jar
managerText.jar
In the next example, operating in verbose
mode, LoadJava uploads and resolves a Java source file. The force (-f)
option forces the loading of the file. The resolve option (-r)
compiles the file and resolves external references; also, by reporting
resolution errors, it helps to identify missing classes.
> loadjava
-v -f -r -u scott/tiger@myComputer:1521:orcl Agent.java
Load Lobs package already installed:
loading : Agent
creating : Agent
resolving: Agent
In the following example, a name resolver is
specified:
> loadjava
-R "((/home/java/bin/* scott) (* public))"
-u scott/tiger@myComputer:1521:orcl Swapper.class
If the path to a file begins with a slash,
LoadJava prefixes the word ROOT to the Java fullname, as the following
example shows:
> loadjava
-v -f -u
scott/tiger@myComputer:1521:orcl
/routines/java/src/Alerter.java
Load Lobs package already installed:
loading
: ROOT/routines/java/src/Alerter
creating : ROOT/routines/java/src/Alerter
Checking Upload Results
To check upload results, you can query the
database view user_objects, which contains information about schema
objects owned by the user (Scott, in this case). For example, the
following SQL*Plus script formats and displays useful information
about uploaded Java sources, binaries, and resources:
SET
SERVEROUTPUT ON
SET VERIFY OFF
PROMPT A)ll or J)ava only?
ACCEPT x CHAR PROMPT 'Choice: '
DECLARE
choice CHAR(1) := UPPER('&x');
printable BOOLEAN;
bad_choice EXCEPTION;
BEGIN
IF choice NOT IN ('A',
'J') THEN RAISE bad_choice; END IF;
DBMS_OUTPUT.PUT_LINE(CHR(0));
DBMS_OUTPUT.PUT_LINE('Object Name
' ||
'Object Type Status Timestamp');
DBMS_OUTPUT.PUT_LINE('——————————————— ' ||
'——————- ———- ————————');
FOR i IN (SELECT object_name, object_type, status, timestamp
FROM user_objects ORDER BY object_type, object_name)
LOOP
/* Exclude objects generated for LoadJava and DropJava. */
printable := i.object_name NOT LIKE 'SYS_%'
AND i.object_name NOT LIKE 'CREATE$%'
AND
i.object_name NOT LIKE 'JAVA$%'
AND i.object_name NOT LIKE 'LOADLOBS';
IF choice = 'J' THEN
printable := i.object_type LIKE 'JAVA %';
END IF;
IF printable THEN
DBMS_OUTPUT.PUT_LINE(RPAD(i.object_name,31) ||
RPAD(i.object_type,14) ||
RPAD(i.status,8) ||
SUBSTR(i.timestamp,1,16));
END IF;
END LOOP;
EXCEPTION
WHEN bad_choice THEN
DBMS_OUTPUT.PUT_LINE('Bad choice');
END;
/
You can choose to display all your schema objects or only the
Java objects:
SQL> @usr_obj
A)ll or J)ava only?
Choice: a
Object Name
Object Type Status Timestamp
————————————————————————————————————————————————————————————————————
Alerter
JAVA CLASS VALID 1998-10-08:13:42
POManager
JAVA CLASS VALID 1998-10-08:17:14
Alerter
JAVA Source VALID 1998-10-08:13:42
POManager
JAVA Source VALID 1998-10-08:17:11
BONUS TABLE
VALID 1998-10-08:14:02
DEPT
TABLE VALID
1998-10-08:14:02
EMP
TABLE VALID
1998-10-08:14:02
SALGRADE
TABLE VALID
1998-10-08:14:02
SQL> @usr_obj
A)ll or J)ava only?
Choice: j
Object Name
Object Type Status Timestamp
————————————————————————————————————————————————————————————————————
Alerter
JAVA CLASS VALID 1998-10-08:13:42
POManager
JAVA CLASS VALID 1998-10-08:17:14
Alerter
JAVA Source VALID 1998-10-08:13:42
POManager
JAVA Source VALID 1998-10-08:17:11
The column object_name stores Java fullnames.
However, if a name is longer than 30 characters or contains an
untranslatable character, the shortname is stored instead. To convert
shortnames to fullnames, you can use the function longname in the
utility package DBMS_JAVA, as follows:
SQL> SELECT
dbms_java.longname(object_name), ... FROM user_objects;
Table 7.1 describes all the columns in
database view user_objects.
Table 7.1 USER_OBJECTS View
Columns
|
Column Name |
Datatype |
Description |
|
OBJECT_NAME |
VARCHAR2(128) |
Name of the object |
|
SUBOBJECT_NAME |
VARCHAR2(30) |
Name of any subobject (a partition, for
example) |
|
OBJECT_ID |
NUMBER |
Object number of the object |
|
DATA_OBJECT_ID |
NUMBER |
Object number of the segment that contains
the object |
|
OBJECT_TYPE |
VARCHAR2(15) |
Type of object (a table or index, for
example) |
|
CREATED |
DATE |
Date on which the object was created |
|
LAST_DDL_TIME |
DATE |
Date of the last DDL operation on the
object |
|
TIMESTAMP |
VARCHAR2(19) |
Character string containing the date and
time the object was created |
|
STATUS |
VARCHAR2(7) |
Status (valid or invalid) of the object |
|
TEMPORARY |
VARCHAR2(1) |
Indicator (y/n) of whether or not the
current session sees only the data that it stores in the object |
|
GENERATED |
VARCHAR2(1) |
Indicator of whether or not the name of
the object was generated by the system |
|
SECONDARY |
VARCHAR2(1) |
Indicator of whether or not this is a
secondary object created for domain indexes |
See
Code Depot for Full Scripts
 |
This is an excerpt
from Mike Ault, bestselling author of "Oracle
10g Grid and Real Application Clusters".
You can buy it direct from the publisher for 30%-off and get
instant access to the code depot of Oracle tuning scripts. |
 |
Expert Remote DBA
BC is America's oldest and largest Remote DBA Oracle support
provider. Get real Remote DBA experts, call
BC Remote DBA today. |
 |
|