I - Java in the database, JDBC, UCP, DRCP, Application Continuity, Transaction Guard
II - In-Database Container for Hadoop, Table Access for Hadoop
building modern Web, mobile, and cloud based applications, the ability to use
the same language across all tiers (client, middle, and database) feels like
Nirvana but the IT landscape is not a green field; enterprises have invested a
with it becomes imperative. WebSockets and RESTful services enable loose integration
and Node.js APIs on the JVM (Avatar.js, Nodyn, Trireme), make possible and very
tempting to co-locate Java and Node applications on the same JVM.
directly on the embedded JVM in Oracle database 12c and the steps for running Node.js
applications on the JVM against Orace database 12c, using Avatar.js, JDBC and
Evolution of Web Applications Architecture
thing while business logic, back-end services and even presentations where handled/produced
engines (Google’s V8, Rhino) leave the browsers and gave birth to server-side
Node Programming Model
and similar frameworks bring ease of development rapid prototyping,
event-driven, and non-blocking programming model
however, unlike Java, Node lacks standardization in many areas such as database
access i.e., JDBC equivalent, and may lead, without discipline, to the so
called “callback hell”.
Nonetheless, Node is popular and has a vibrant community and a large set of frameworks.
Node Impact on Web Applications
the advent of Node, REST and Web Sockets, the architecture of Web applications
has evolved into
interacting with Java business logic and databases.
new proposal for Web applications architecture is the integration of Node.js and
Java on the JVM. Let’s discuss the enabling
typical use cases with Oracle database 12c.
In summary, the JVM brings(i) portability; (ii) manageability; (iii) Java tools; (iv) Java libraries/technologies such as JDBC, Hadoop; and (v) the
preservation of investments in Java.
There are several implementations/projects
engine in Java SE, now replaced by
Nashorn in Java SE 8.
engine for the JVM. Here is the project homepage http://dynjs.org/.
Introduced in Java 7 but “production” in Java 8,
the goal of project Nashorn (JEP 174), is to enhance the performance and
Java and invoking Java from Nashorn).
To illustrate the reach of Nashorn on the JVM and the interaction between Java
in Oracle database 12c.
with Oracle database 12c Using Nashorn
Java in Oracle database. Then you might ask: why run Java in the database, in
the first place? As discussed in my book,
the primary motivations are:
(i) reuse skills and code, i.e., which programming
languages are your new hire knowledgeable of or willing to learn;
data shipping i.e.,
in-place processing of billions of data/documents;
(iii) combine SQL with foreign
libraries to achieve new database capability thereby extending SQL and the reach
of the RDBMS, e.g., Web Services callout, in-database container for Hadoop.
Some developers/architects prefer a tight separation between the RDBMS and
applications therefore, no programming language in the databasebut
there are many pragmatic developers/architects who run code near data, whenever
it is more efficient than shipping data to external infrastructure.
functions with data on the same compute engine is shared by many programming
models such as Hadoop. With the surge and prevalence of Cloud computing, RESTful
service based architecture is the new norm. Data-bound services can be secured
and protected by the REST infrastructure, running outside the RDBMS. Typical
procedures service would process millions/billions of JSON documents in the Oracle database and would return the result sets to the service invoker.
programming language on the JVM in the database is a sound architectural choice.
The best practices consist in: (i) partitioning applications into data-bound
and compute-bound modules or services; (ii) data-bound services are good
candidates for running in the database; (iii) understand Oracle database DEFINERINVOKER rights 
and grant only the necessary privilege(s) and/or permission(s).
The following steps allow implementing
JavaScipt stored procedure running in
Oracle database; these steps represent an enhancement from the ones presented
database schema for reading from RDBMS file system something not recommended
from security perspective. Here is a safer approach:
is part of Java 8 but early editions can be built for Java 7; the embedded
JavaVM in Oracle database 12c supports Java 6 (the default) or Java 7. For this
proof of concept, install Oracle database 12c with Java SE 7 
a standard Nashorn.jar;
(ii) modify the Shell code to interpret the given script name as an OJVM
resource; this consists mainly in invoking getResourceAsStream()on
the current thread's context class loader ; (iii) rebuild Nashorn.jar with the
3.Load the modified
Nashorn jar into an Oracle database shema e.g., HR loadjava -v -r -u
with a script name as parameter create or replace
procedure run(script varchar2);
procedure run(script varchar2) as
language java name
SQL which will invoke Nashorn Shell to
execute the previously loaded myscript.js.
5.Create a custom
role, we will name it NASHORN, as follows, connected as SYSTEM SQL> create role nashorn;
SQL> call dbms_java.grant_permission('NASHORN',
'SYS:java.lang.RuntimePermission', 'createClassLoader', '' );
SQL> call dbms_java.grant_permission('NASHORN',
'SYS:java.lang.RuntimePermission', 'getClassLoader', '' );
SQL> call dbms_java.grant_permission('NASHORN', 'SYS:java.util.logging.LoggingPermission',
'control', '' );
Best practice: insert those statements
in a nash-role.sqlfile
and run the script as SYSTEM
the NASHORN role created above to the HR schema as
follows (connected as SYSTEM):
SQL> grant NASHORN to HR;
stored on your client machine’s (i.e., a machine
from which you will invoke loadjava as explained in the next step).
JDBC driver to execute a PreparedStatement to retrieve the
first and last names from the EMPLOYEES table.
var Driver =
var oracleDriver = new Driver();
var url = "jdbc:default:connection:"; // server-side JDBC driver
var query ="SELECT first_name, last_name from employees";
// Establish a JDBC connection
var connection = oracleDriver.defaultConnection();
// Prepare statement
var preparedStatement = connection.prepareStatement(query);
// execute Query
var resultSet = preparedStatement.executeQuery();
// display results
"== " + resultSet.getString(2) + " " );
database.jsin the database as a Java
resource (not a vanilla class) loadjava –v –r –u hr/ database.js
run the loaded script
SQL>set serveroutput on
Nashorn Shell reads ‘database.js’ script stored
execute a PreparedStatement
the result set is displayed on the console. The message “ORA=29515: exit called from Java code with status 0” is due to the
invocation of java.lang.Runtime.exitInternal; and status 0
means normal exit (i.e., no error). The fix is to remove that call from
Node.js on the JVM
discussed earlier, Node.js is becoming the man-in-the-middle between Web
applications front ends and back-end legacy components and since companies have
invested a lot in Java, it is highly desirable to co-locate Node.js and Java components
on the same JVM for better integration thereby eliminating the communication
overhead. There are several projects re-implementing Node.js APIs on the JVM
including: Avatar.js, Nodyn, and Trireme. This paper will only discuss Oracle’s
The goal of project Avatar.js
is to furnish “Node.js on the JVM”;
in other words, an implementation of Node.js APIs, which runs on top of Nashorn
and enables the co-location of Node.js programs and Java components. It has
been outsourced by Oracle under GPL license.
Many Node frameworks and/or applications have been certified to run unchanged
or slightly patched, on Avatar.js.
There are binary
distributions for Oracle Enterprise Linux, Windows and MacOS (64-bits). These builds
can be downloaded from https://maven.java.net/index.html#welcome.
for avatar-js.jar and platform specific libavatar-jslibraries (.dll,
Get the latest and rename the jar and the specific native libary accordingly. For
example: on Linux, rename the libary to avatar-js.so; on Windows, rename
the dll to avatar-js.dlland add its location to your
PATH (or use -Djava.library.path=).
in general and Oracle database in particular remain the most popular
persistence engines and there are RDBMS specific Node drivers
as well as ORMs frameworks. However, as we will demonstrate in the following
section, with Avatar.js, we can simply reuse existing Java APIs including JDBC
and UCP for database access.
Node Programming with Oracle
Database using Avatar.js, JDBC and UCP
The goal of this proof of concept is to
illustrate the co-location of a Node.js application, the Avatar.js library, the
Oracle JDBC driver and the Oracle Universal Connection Pool (UCP) on the same
Java 8 VM.
The sample application consists in a Node.js
application which performs the following actions:
(i) Request a JDBC-Thin connection from the
Java pool (UCP)
(ii)Create a PreparedStatement object for “SELECT FIRST_NAME, LAST_NAME
(iii)Execute the statement and return the ResultSet in a callback
(iv)Retrieve the rows and display in browser on port 4000
(v) Perform all steps above in a non-blocking
fashion – this is Node.js’s raison d’être.
The demo also uses Apache abload generator to simulate concurrent users running the same application
in the same/single JVM instance.For the Node application to scale in the absence of
asynchronous JDBC APIs, we need to turn synchronous calls into non-blocking
ones and retrieve the result set via callback.
Synchronous JDBC Calls into Non-Blocking Calls
We will use the following wrapper functions to
turn any JDBC call into a non-blocking call i.e., put the JDBC call into a
thread pool and free up the Node event loop thread.
AB, we were able to scale to hundreds of simultaneous invocations of the Node application.
Each instance grabs a Java connection from The Universal Connection Pool (UCP),
executes the SQL statements through JDBC then return the result set via a Callbak
on port 4000.