chapter 1 · pdf fileoracle tight / oracle database 12c pl/sql advanced programming techniques...

Download CHAPTER 1 · PDF fileOracle TIGHT / Oracle Database 12c PL/SQL Advanced Programming Techniques / McLaughlin & Harper / 514-8 / Blind folio: 3 CHAPTER 1 Java Functions

If you can't read please download the document

Post on 29-Aug-2018

234 views

Category:

Documents

3 download

Embed Size (px)

TRANSCRIPT

  • Oracle TIGHT / Oracle Database 12c PL/SQL Advanced Programming Techniques / McLaughlin & Harper / 514-8 / Blind folio: 3

    CHAPTER1

    Java Functions and Procedures

    01-ch01.indd 3 9/25/14 12:32 PM

  • Oracle TIGHT / Oracle Database 12c PL/SQL Advanced Programming Techniques / McLaughlin & Harper / 514-8 Oracle TIGHT / Oracle Database 12c PL/SQL Advanced Programming Techniques / McLaughlin & Harper / 514-8

    4 Oracle Database 12c PL/SQL Advanced Programming Techniques

    PurposeThis chapter teaches you how to configure, deploy, and troubleshoot Java libraries. It shows you how to

    Understand the JVM architecture of Oracle Database 12c

    Use the default Java 6 SDK or override the default with the Java 7 SDK

    Compile Java source files into class files, and then load the class files into Oracle Database 12c by using the loadjava utility

    Create and compile Java libraries directly through the SQL*Plus Command Line Interface (CLI) utility

    Use scalar and Attribute Data Type (ADT) collections as parameters and return data types through the Oracle JDBC libraries

    Some of these examples build on concepts introduced in Oracle Database 12c PL/SQL Programming (Oracle Press, 2014). An introduction to the Java programming language is also provided as a primer in the appendix of this book.

    AdvantagesOracle Database 12c lets you extend your applications by writing stored functions, procedures, and packages. You also have the opportunity to write these stored programs in the Java programming language. There is one catch: While you can write the implementation in the Java programming language, PL/SQL is the tool that builds the gateway between the environment and the Java libraries. Therefore, you must write PL/SQL wrappers, much like you must write SQL triggers, to launch PL/SQL anonymous block trigger bodies.

    A PL/SQL wrapper is a small piece of code that specifies a schema-level function or procedure, or a package. These PL/SQL wrappers point to internally stored implementations of Java libraries. They map the PL/SQL formal parameter and return data types to Java data types.

    PL/SQL wrappers are much like package specifications, which define functions and procedures that you implement in Java libraries rather than package bodies. Publishing Java libraries is nothing more than creating PL/SQL wrappers to Java libraries.

    Writing Java libraries in lieu of native PL/SQL functions, procedures, and packages is advantageous when your core development language is Java, because then application and database backend programmers speak the same language.

    Also, while its possible to unwrap PL/SQL programs to see the programming logic that was wrapped, theres no equivalent utility for getting at the source code of Java libraries when you use the loadjava utility. That means putting key logic into Java adds another layer of security protection to the code.

    DisadvantagesWriting code in Java isnt necessary, because PL/SQL provides all the features that you need for functions, procedures, and packages. Writing PL/SQL in the database while writing Java on the middle tier of an application can make understanding the complete logic of any applications more complex.

    01-ch01.indd 4 9/25/14 12:32 PM

  • Oracle TIGHT / Oracle Database 12c PL/SQL Advanced Programming Techniques / McLaughlin & Harper / 514-8 Oracle TIGHT / Oracle Database 12c PL/SQL Advanced Programming Techniques / McLaughlin & Harper / 514-8

    Chapter 1: Java Functions and Procedures 5

    OverviewThis chapter focuses on the following:

    Oracle Database 12c JVM features

    Java architecture in Oracle

    Oracle Java connection types

    Importing Java class libraries

    Building Java class libraries

    Troubleshooting how you build, load, and use Java libraries

    The chapter builds on the PL/SQL concepts covered in Oracle Database 12c PL/SQL Programming and introduces new material sequentially. If you feel you understand the basics of any of these sections, you should be able to jump to a section of interest for the details, though a quick browse of all the material might save you time looking for a missing piece later in the chapter.

    Oracle Database 12c JVM FeaturesThe Oracle Database 12c Java Virtual Machine (JVM) has matured. The following new features are available in this release of the database:

    The internal Oracle JVM is now compatible with Standard Edition of Java 6 by default, and configurable with Java 7.

    Oracle JVM enhancements include support for the loadjava utility, list-based operations with dropjava, the ability to resolve external class references by using the ojvmmtc utility, and increased functionality in the ojvmjava tool.

    Database-resident JAR files have been introduced, which means that when you load a JAR file, you now have the option of creating an object representing the JAR file transparently.

    Metadata can be shared between user-defined classes. This mimics the concept of a multithreaded process control block.

    Two-tier duration for Java session state is now possible. This lets you use Java as previously done in autonomous transactions within a connection, or as a persistent session with preserved state between transactions. You can use the endsession function in the dbms_java package to clear the previous session state on entry. This function preserves any property settings when it clears the session state. You should use the endsession_and_related_state function from the dbms_java package when you want to clear both the session state and property settings.

    Output streams can be redirected externally from the database.

    System properties can be set that are propagated on connection to the database server.

    A Java just-in-time (JIT) compiler reduces Java byte streams directly into machine-specific code, improving performance by eliminating the interpretation phase.

    01-ch01.indd 5 9/25/14 12:32 PM

  • Oracle TIGHT / Oracle Database 12c PL/SQL Advanced Programming Techniques / McLaughlin & Harper / 514-8 Oracle TIGHT / Oracle Database 12c PL/SQL Advanced Programming Techniques / McLaughlin & Harper / 514-8

    6 Oracle Database 12c PL/SQL Advanced Programming Techniques

    The Java Interactive InterfaceThe ojvmjava utility is an interactive interface to the session namespace and database instance. From Oracle Database 11g forward, you can launch executables through this tool. A new runjava option lets you run the ojvmjava shell in command mode or resident class mode. The current version of ojvmjava has reduced the stack trace for thrown exceptions. You also can open a new connection without leaving the current ojvmjava session.

    Java Architecture in OracleThe Oracle Database 12c databases provide a robust architecture for developing server-side or internal Java programming components. Java components are object-oriented (OO) structures that fit naturally into Oracles object-relational model. The component architecture is a library stack that contains

    Platform-dependent operating systems, such as Unix, Linux, and Microsoft Windows

    Platform-dependent Oracle database management files and libraries

    Oracle database instance JVM, which is platform independent

    Java core class libraries, which are ported to various platforms

    Oracle-supported Java application programming interfaces (APIs), such as SQLJ, JDBC, and JNDI

    Oracle PL/SQL stored objects that provide an interface between SQL and PL/SQL programs, as well as server-side Java classes

    The Oracle and Java libraries store and manage application programs like a ubiquitous file system. Virtual directories create ubiquitous file systems because the physical locations of the programs are operating system independent. Oracle libraries make storing, retrieving, and recovering files a standard process across many diverse platforms. The JVM provides a standard environment in which you can build and execute well-documented OO programs. Oracle PL/SQL enables the development of wrapper packages to access the Java libraries from other PL/SQL stored objects and SQL. The architecture of the Oracle JVM is shown in Figure 1-1.

    Oracle JVM uses two types of namespaces: the long name and the short name. The long name is exactly as the class is named in Java, which is a case-sensitive name that may be longer than object names stored in the Oracle Database 12c data catalog. The case-sensitive Java program name must be unique in the scope of the Java namespace within the database server. Short names are those assigned by the database when you store Java libraries, and they conform to the storage requirements of the data catalog.

    The short name for a Java library must be unique within the core namespaces for the Oracle Database 12c server. There are two namespaces for each schema in an Oracle database. One namespace covers all functions, libraries, procedures, packages, tables, objects, sequences, and views, which includes the short names for Java libraries. The other namespace is reserved for database triggers. Trigger names are unique within their own namespace, but that means a trigger name can duplicate any of the other object type names in a schema.

    You can call stored Java programs by their native namespace. While the chapter examples are short and not placed into packages, youll most likely put your Java programs into packages. The

    01-ch01.indd 6 9/25/14 12:32 PM

  • Oracle TIGHT / Oracle Database 12c PL/SQL Advanced Programming Techniques / McLaughlin & Harper / 514-8 Oracle TIGHT / Oracle Database 12c PL/SQL Advanced Programming Techniques / McLaughlin & Harper / 514-8

    Chapter 1: Java Functions and Procedures