Download Using Java in Oracle Database

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Database wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Relational model wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

SQL wikipedia , lookup

Database model wikipedia , lookup

Clusterpoint wikipedia , lookup

PL/SQL wikipedia , lookup

Object-relational impedance mismatch wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Oracle Database wikipedia , lookup

Transcript
Informācijas sistēmas struktūra
"thick" client
"biezais"
klients
Lietojumu
serveris
"plānais"
klients
Datu bāzes interfeiss
Datu bāzes sistēma
Datu
bāzes
vadības
sistēma
Datu bāze
Dati un
meta-dati
Servera
procedūras
(PL/SQL, Java)
"thin" client
Lietojumi
ODBC
Web serveris
OLE DB
Transakciju serveris ADO
JDBC
Relāciju datu bāze
Objektu datu bāze
Relāciju-objektu datu bāze
(Universālās datu bāzes)
2
Using Java in Oracle Database
You can write and load Java applications within the database because it is a safe language with a lot
of security features. Java has been developed to prevent anyone from tampering with the operating
system where the Java code resides in. Some languages, such as C, can introduce security problems
within the database. However, Java, because of its design, is a robust language that can be used
within the database.
Oracle Database provides Java applications with a dynamic data-processing engine that supports
complex queries and different views of the same data. All client requests are assembled as data
queries for immediate processing, and query results are generated dynamically.
The combination of Java and Oracle Database helps you to create component-based, networkcentric applications that can be easily updated as business needs change. In addition, you can move
applications and data stores off the desktop and onto intelligent networks and network-centric
servers. More important, you can access those applications and data stores from any client device.
3
In the Java-integrated Oracle Database, your Java applications exist within the context of a database
session. Oracle JVM sessions are entirely analogous to traditional Oracle sessions. Each Oracle
JVM session maintains the state of the Java applications accessed by the client across calls within
the session.
Figure illustrates how each Java client starts a database session as the environment for running Java
applications within the database. Each Java database session has a separate garbage collector,
session memory, and call memory.
Within the context of a session, the client performs the following:
1) connects to the database and opens a session;
2) runs Java within the database. This is referred to as a call;
3) continues to work within the session, performing as many calls as required;
4) ends the session.
Within a session, the client has its own Java environment. It appears to the client as if a separate,
individual JVM was started for each session, although the implementation is more efficient than this
seems to imply. Within a session, Oracle JVM manages the scalability of applications. Every call
from a single client is managed within its own session, and calls from each client are handled
separately. Oracle JVM maximizes sharing read-only data between clients and emphasizes a
minimum amount of per-session incremental footprint, to maximize performance for multiple
clients.
The underlying server environment hides the details associated with session, network, state, and
other shared resource management issues from the Java code. Fields defined as static are local to
the client. No client can access the static fields of other clients, because the memory is not
available across session boundaries. Because each client runs the Java application calls within its
own session, activities of each client are separate from any other client. During a call, you can store
objects in static fields of different classes, which will be available in the next call. The entire state
of your Java program is private and exists for your entire session.
Oracle JVM manages the following within the session:
4

All the objects referenced by static Java fields, all the objects referred to by these
objects, and so on, till their transitive closure

Garbage collection for the client that created the session

Session memory for static fields and across call memory needs

Call memory for fields that exist within a call
5
Oracle Datu bāzes Java komponenšu struktūra
6
Java katalogu sistēma Oracle11c ver.2 datu bāzes sistēmās
E:\app\Janis\product\11.2.0\dbhome_1
bin ocijdbc11.dll (OCI datu bāzes draiveris)
j2ee
home
OC4J_EM
javavm
admin
demo
doc
install
jahome
lib
jdbc
lib ojdbc5.jar (datu bāzes draiveris)
jdev
doc
lib
jdk
bin
include
jre
lib
jlib orai18n.jar
The driver versions in the 11R2 release:
- JDBC Thin Driver 11R2. 100% Java client-side JDBC driver for use in client
applications, middle-tier servers and applets.
- JDBC OCI Driver 11R2. Client-side JDBC driver for use on a machine where OCI
11R2 is installed.
- JDBC Thin Server-side Driver 11R2. JDBC driver for use in Java program in the
database to access remote Oracle databases.
- JDBC Server-side Internal Driver 11R2. Server-side JDBC driver for use by Java
Stored procedures. This driver used to be called the "JDBC Kprb Driver".
For all platforms [ORACLE_HOME]/jdbc/lib contains:
- ojdbc5.jar Classes for use with JDK 1.5. It contains the JDBC driver classes,
except classes for NLS support in Oracle Object and Collection types.
- ojdbc5_g.jar Same as ojdbc5.jar, except that classes were compiled with "javac -g"
and contain tracing code.
- ojdbc5dms.jar Same as ojdbc5.jar, except that it contains instrumentation to
support DMS and limited java.util.logging calls.
7
- ojdbc5dms_g.jar Same as ojdbc5_g.jar, except that it contains instrumentation to
support DMS.
- ojdbc6.jar Classes for use with JDK 1.6. It contains the JDBC driver classes
except classes for NLS support in Oracle Object and Collection types.
- ojdbc6_g.jar Same as ojdbc6.jar except compiled with "javac -g" and contains
tracing code.
- ojdbc6dms.jar Same as ojdbc6.jar, except that it contains instrumentation to
support DMS and limited java.util.logging calls.
- ojdbc6dms_g.jar Same as ojdbc6_g.jar except that it contains instrumentation to
support DMS.
The dms (DMS) versions of the jar files are the same as standard jar files, except that
they contain additional code to support Oracle Dynamic Monitoring Service. They
contain a limited amount of tracing code. These can only be used when dms.jar is in
the classpath. dms.jar is provided as part of Oracle Application Server releases. As a
result the dms versions of the jar files can only be used in an Oracle Application
Server environment.
[ORACLE_HOME]/jdbc/doc/javadoc.tar contains the JDBC Javadoc for the
public API of the public classes of Oracle JDBC. This JavaDoc is the primary
reference for Oracle JDBC API extensions. The Oracle JDBC Development Guide
contains high level discussion of Oracle extensions. The details are in this JavaDoc.
The JavaDoc is every bit as authorative as the Dev Guide.
[ORACLE_HOME]/jdbc/demo/demo.tar contains sample JDBC programs.
[ORACLE_HOME]/jlib/orai18n.jar NLS classes for use with JDK 1.5, and 1.6. It
contains classes for NLS support in Oracle Object and Collection types. This jar file
replaces the old nls_charset jar/zip files.
For the Windows platform:
[ORACLE_HOME]\bin directory contains ocijdbc11.dll and heteroxa11.dll, which
are the libraries used by the JDBC OCI driver.
Installation
Please do not try to put multiple versions of the Oracle JDBC drivers in your
CLASSPATH.
The Oracle installer installs the JDBC Drivers in the
[ORACLE_HOME]/jdbc directory.
Setting Up Your Environment
On Windows platforms:
- Add [ORACLE_HOME]\jdbc\lib\ojdbc5.jar to your CLASSPATH if you use
JDK 1.5 or [ORACLE_HOME]\jdbc\lib\ojdbc6.jar if you use JDK 1.6.
- Add [ORACLE_HOME]\jlib\orai18n.jar to your CLASSPATH if needed.
- Add [ORACLE_HOME]\bin to your PATH if you are using the JDBC OCI
8
driver.
If you are installing the JDBC Thin driver, then you do not have to set any other
environment variables. However, to use the JDBC server-side Thin driver, you need
to set permission.
The JDBC server-side Thin driver opens a socket for its connection to the database.
Because Oracle Database enforces the Java security model, a check is performed for
a SocketPermission object. To use the JDBC server-side Thin driver, the connecting
user must be granted the appropriate permission. The following is an example of how
the permission can be granted for the user SCOTT:
CREATE ROLE jdbcthin;
CALL dbms_java.grant_permission('JDBCTHIN', 'java.net.SocketPermission',
'*', 'connect');
GRANT jdbcthin TO SCOTT;
These are a few simple things that you should do in your JDBC program:
1. Import the necessary JDBC classes in your programs that use JDBC.
import java.sql.*;
import java.math.*; // if needed
To use OracleDataSource, you need to do:
import oracle.jdbc.pool.OracleDataSource;
2. Create an OracleDataSource instance.
OracleDataSource ods = new OracleDataSource();
3. set the desired properties if you don't want to use the default properties. Different
connection URLs should be used for different JDBC drivers.
ods.setUser("my_user");
ods.setPassword("my_password");
For the JDBC OCI Driver:
To make a bequeath connection, set URL as:
ods.setURL("jdbc:oracle:oci:@");
To make a remote connection, set URL as:
ods.setURL("jdbc:oracle:oci:@<database>");
where <database> is either a TNSEntryName or a SQL*net name-value pair defined
in tnsnames.ora.
For the JDBC Thin Driver, or Server-side Thin Driver:
ods.setURL("jdbc:oracle:thin:@<database>");
where <database> is either a string of the form
//<host>:<port>/<service_name>,
or a SQL*net name-value pair, or a TNSEntryName.
9
For the JDBC Server-side Internal Driver:
ods.setURL("jdbc:oracle:kprb:");
Note that the trailing ':' is necessary. When you use the Server-side Internal Driver,
you always connect to the database you are executing in. You can also do this:
Connection conn = new oracle.jdbc.OracleDriver().defaultConnection();
4. Open a connection to the database with getConnection() methods defined in
OracleDataSource class.
Connection conn = ods.getConnection();
10
Java programmu izveidošana Oracle datu bāzē
1. Java klases failu ielāde DB izmantojot loadjava programmu.
CMD vide
Fails ar Java
klases definējumu
loadjava
programma
Datu bāzes
sistēma
2. Java klases izveidošana ar create java … komandu.
Datu bāzes
sistēma
SQL*PLUS programma
SQL> create java ...
11
Java metodes izsaucošās (call specs) PL/SQL
procedūras
To publish Java methods, you write call specs. For a given Java method, you declare
a function or procedure call spec using the SQL CREATE FUNCTION or CREATE
PROCEDURE statement. You publish value-returning Java methods as functions or
procedures and void Java methods as procedures. The function or procedure body
contains the LANGUAGE JAVA clause.
Datu bāze
Lietojums
PL/SQL izsaucošā
procedūra vai
funkcija (Call Spec)
Java
metode
Datu vārdnīca
As an alternative, use the native Java interface to directly invoke Java in the
database from a Java client.
A call spec and the Java method it publishes must reside in the same schema (unless
the Java method has a PUBLIC synonym). You can declare the call spec as a:
1.
2.
3.
standalone (top-level) PL/SQL function or procedure;
packaged PL/SQL function or procedure;
member method of a SQL object type.
A call spec exposes a Java method's top-level entry point to Oracle. Therefore, you
can publish only public static methods—with one exception. You can publish
instance methods as member methods of a SQL object type.
Packaged call specs perform as well as top-level call specs. So, to ease maintenance,
you might want to place call specs in a package body. That way, you can modify
them without invalidating other schema objects. Also, you can overload them.
12
Java programmas izpildes varianti
13
Savienojuma ar DB veidošanas draiveri
1. JDBC Thin Driver
You can use the JDBC Thin driver to write 100% pure Java applications and applets
that access Oracle SQL data. The JDBC Thin driver is especially well-suited to Web
browser-based applications and applets, because you can dynamically download it
from a Web page just like any other Java applet.
2. JDBC Oracle Call Interface Driver
The JDBC Oracle Call Interface (OCI) driver accesses Oracle-specific native code
(that is, non-Java) libraries on the client or middle tier, providing some performance
boost compared to the JDBC Thin driver, at the cost of significantly larger size and
client-side installation.
3. JDBC Server-side Internal Driver
Oracle Database uses the server-side internal driver when Java code executes on the
server. It allows Java applications executing in the server's Java virtual machine to
access locally defined data (that is, on the same machine and in the same process)
with JDBC. It provides a further performance boost because of its ability to use
underlying Oracle RDBMS libraries directly, without the overhead of an intervening
network connection between your Java code and SQL data. By supporting the same
Java-SQL interface on the server, Oracle Database does not require you to rework
code when deploying it.
14
DB glabājamās procedūras (stored procedures)
Oracle javas virtuālā mašīna OracleJVM nodrošina sekojošu programmu
izpildi:
1) funkciju un procedūru;
2) trigeru;
3) relāciju-objektu metožu.
15
Vai Java programmēšanas valodas lietošanas iespējas ir
nodrošinātas datu bāzē (vai ir Java Virtual Machine
(JVM) "aurora")
Sākot ar Oracle8i versiju datu bāzes serverī ir iekļauta Java Virtual Machine,
kas ļauj izmantot un veidot serverī Java klases.1
select COUNT(*) from ALL_OBJECTS where OBJECT_TYPE like 'JAVA%';
COUNT(*)
------------------16 983
Ja rezultāts ir 0, jāpalaiž $ORACLE_HOME/javavm/install/initjvm.sql
1
Ken Arnold, James Gosling, David Holmes. The Java Programming Language.
Addison-Wesley.
David Flanagan. Java in a Nutshell. O'Reilly.
Bruce Eckel. Thinking in Java. Prentice Hall.
16
Java Memory Areas
In OracleJVM, the states of Java objects are preserved in special data structures
called “object memories.” There are several object memory types:
1) newspace is the default memory for allocating almost all Java objects, except
large objects, which are allocated in Oldspace.
2) oldspace is an object memory used for holding long-lived or large objects (i.e.,
larger than 1-K Bytes) for the duration of a call.
3) runspace is an object memory residing in CGA or PGA and is used to hold
system objects (e.g., classloader objects) allocated for the duration of a call.
4) stackspace is an object memory residing in CGA or PGA and is used to
allocate system objects and objects used by the Java interpreter (no user
objects);
5) sessionspace, also called “session memory,” is an object memory residing in
the User Global Area (UGA), which is associated with each session (each
session has its own Sessionspace) and lives as long as the session lives.
Sessionspace is used to preserve state of system objects, user objects, and
metadata used by the Java runtime.
17
Java atmiņas apgabali izdalītā servera gadījumā
18
Java atmiņas apgabali koplietojuma servera gadījumā
19
A Java application may result in:
1) of bytecodes (interpreted);
2) natively compiled binaries.
Therefore, the natively compiled code must be fully interoperable with the Java
VM. The native compiler has been designed to generate C code that allows
natively compiled routines to call (and be called from) interpreted Java
bytecode.