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.
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:
- 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.
- 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.
- Platform Independence
- Java code compiled once can run on any Oracle database platform supporting JVM, regardless of underlying hardware and OS.
- Security and Isolation
- Provides a secure runtime environment.
- Sandboxed execution ensures database stability and prevents unauthorized resource access.
- 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.
- 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.
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:
- stack (per thread)
- heap
- constant pool
- code segment
- 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:
- PL/SQL for database interaction and
- 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.