Internet Features   «Prev  Next»

Lesson 3Java and Oracle
ObjectiveDescribe how Java has been integrated into Oracle.

Java and Oracle

The Java Virtual Machine (JVM) runs Java applications. If you create a Java application on your desktop, it will run without any modification on your mainframe because both your desktop and your mainframe have JVM. That's the advantage of using Java for your application programming. And Java is quickly becoming one of the most popular programming platforms around. Oracle has JVM built into the database server itself. This means that you can run Java applications inside your database. You can also deliver an applet to a Web page using the built-in features of your Oracle database and no Web server is needed. Look at the following series of images below to see more advantages of using the Oracle JVM.

1) Combine PL-SQL and Java, or substitute Java for PL-SQL within stored procedures, functions, triggers, object methods.
The image depicts an architecture of an Oracle 8i database, illustrating how Java and PL/SQL components interact. Here's a detailed analysis of its components:
1. Oracle 8i Database (Container)
  • Represents the Oracle 8i Relational Database Management System (RDBMS).
  • Provides an environment for storing and executing different types of stored program units.

Stored Program Components Illustrated:
  1. Java Procedure
    • Role: A stored Java method that performs operations within the database.
    • Use: Enables Java code execution directly on the database server, enhancing flexibility for tasks difficult in PL/SQL alone (e.g., complex computation, file system access, external communication).
  2. Java Function
    • Role: Stored Java methods within Oracle that always return a single value.
    • Can be called by SQL and PL/SQL code, similarly to PL/SQL functions.
  3. PL/SQL Function
    • Role: A database-resident function written in Oracle’s PL/SQL language.
    • Returns a single value, and can be directly invoked from SQL or PL/SQL blocks.
  4. PL/SQL Trigger
    • Role: Specialized stored PL/SQL procedures automatically executed (triggered) by database events (insert, update, delete operations).
    • Allows automation of business logic and enforcement of integrity constraints at the database layer.
Summary of interactions depicted in the diagram:
  • The diagram highlights interoperability between Java and PL/SQL stored program units within an Oracle 8i database.
  • Java functions and procedures can interact seamlessly with PL/SQL triggers and functions.
  • Triggers written in PL/SQL can invoke Java functions for complex business logic or to leverage Java's additional capabilities beyond PL/SQL.

Conclusion: This diagram emphasizes how Oracle databases (specifically Oracle 8i shown here) support integrated development using multiple stored programming languages (Java and PL/SQL), enabling the flexible, powerful development and extension of database applications. 1) Combine PL-SQL and Java, or substitute Java for PL-SQL within stored procedures, functions, triggers, object methods. You can use either programming language for these. You can even call PL/SQL subprograms from within Java subprograms or vice versa.

2) Connect the database with the native JDBC. When you create JDBC calls to query tables or perform DML commands, the connection used by Oracles JVM is a high-performance connection
2) Connect the database with the native JDBC. When you create JDBC calls to query tables or perform DML commands, the connection used by Oracle's JVM is a high-performance connection to the database. This special connection is optimized for better performance than JDBC calls originating outside the database.

3) Take advantage of Oracles scalability to service more concurrent users with less resources than ordinary JVMs. Most JVM products cannot share resources among concurrent users
3) Take advantage of Oracle's scalability to service more concurrent users with less resources than ordinary JVMs. Most JVM products cannot share resources among concurrent users. Oracle's JVM uses the database's capability to share memory and reuse common code.

Java Virtual Machine in Oracle 19c RDBMS

In Oracle Database, the Java Virtual Machine (JVM) is embedded directly into the database, enabling the database to execute Java applications within the database environment.
Role of the Java Virtual Machine (JVM) in Oracle Database:
The JVM in Oracle fulfills several critical roles:
  1. Execution of Java Code within the Database
    • Allows Java programs to run securely inside the Oracle Database server.
    • Java methods can be stored and executed as part of stored program units, similar to PL/SQL.
  2. Integration with Database Features
    • Seamlessly integrates Java logic with database operations, allowing Java to directly interact with SQL.
    • Facilitates calling Java methods from SQL or PL/SQL, enhancing flexibility and performance.
  3. Platform Independence
    • Java code compiled once can run on any Oracle database platform supporting JVM, regardless of underlying hardware and OS.
  4. Security and Isolation
    • Provides a secure runtime environment.
    • Sandboxed execution ensures database stability and prevents unauthorized resource access.
  5. Extensibility
    • Enables extending database functionality with Java libraries (e.g., network operations, encryption, file I/O, XML processing).
    • Allows developers to leverage existing Java libraries directly within database applications.
  6. Memory and Garbage Collection Management
    • Manages memory efficiently through automatic garbage collection of Java objects, independent of Oracle's native memory management.

Example of Java Integration in Oracle:
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "HelloWorld" AS
public class HelloWorld {
    public static String sayHello() {
        return "Hello, World!";
    }
};
/

CREATE OR REPLACE FUNCTION java_hello
RETURN VARCHAR2 AS
LANGUAGE JAVA NAME 'HelloWorld.sayHello() return java.lang.String';
/

SELECT java_hello FROM dual;

-- Output:
-- Hello, World!

Practical Use Cases for JVM in Oracle:
  • Complex calculations or algorithms.
  • Network communications directly from database.
  • Implementing business logic using Java’s capabilities and libraries.
  • Advanced XML and JSON processing.
  • Encryption and security-related operations.
  • External system integration.

In short, the Java Virtual Machine inside Oracle databases provides a powerful, secure, and flexible execution environment, allowing you to leverage Java's capabilities directly within database operations and stored programs.

Java Virtual Machine used within Oracle Environment

There are numerous uses of Java beyond Oracle applications, and there are many features of the language that will not be used by most Oracle developers. The goal of this chapter is to provide developers who have a background in SQL and PL/SQL with an understanding of the Java language structures. This is not an exhaustive review of Java (there are numerous resources that accomplish that goal) but rather a short overview of the Java language components most commonly used by Oracle developers. Although there are many cases in which PL/SQL and Java correlate well to each other, there are significant differences in terminology and usage. That should not be surprising, since the two programming languages were developed independently. Furthermore, PL/SQL's object-oriented capabilities were not part of its initial implementation, whereas Java has been object-oriented since its inception. To useJava effectively, you need to take a different approach than the one you may have taken in the past with PL/SQL.
  • Components of the JVM:
    1. stack (per thread)
    2. heap
    3. constant pool
    4. code segment
    5. program counter (per thread)
With the ubiquity of Java on the Web, you may be tempted to create a fully Java-based database application. In that architecture, the client communicates with the application server via either HTTP or the Internet Inter-ORB Protocol (IIOP). Within the database, you can create Java classes, and the application server would interact with those Java classes via IIOP. Thus, you can create a Web-enabled database that does not rely on Oracle Network Services for any of its connectivity. Within the database, you can write your stored procedures in either PL/SQL. In general, you should use these languages for the functions they perform best:
  1. PL/SQL for database interaction and
  2. Java for non-database-related functions.
If you use Java exclusively throughout your application, you should expect to encounter performance or functionality limitations during its production usage. Java is well suited for use on the client and application server. In your application development process, you should test the implications of using Java in place of PL/SQL or SQL for your database access. PL/SQL has been part of the Oracle kernel for over a decade and is well suited for quick data retrieval and manipulation.
You will find a good tutorial on creating Web applets that query database tables in the Oracle demo directory under the SQL Developer installation. Java Applets are primarily used as client software components that are installed remotely in order to produce trouble shooting.
The next lesson describes interMedia, an Oracle add-on tool.

SEMrush Software 3 SEMrush Banner 3