JavaScript Programming with Oracle c Tiers · Angular.js, Require.js, Grunt.js, React.js, Redux,...
Transcript of JavaScript Programming with Oracle c Tiers · Angular.js, Require.js, Grunt.js, React.js, Redux,...
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
JavaScript Programming with Oracle Database 12c Using Nashorn Across Tiers
Kuassi Mensah Srivatsan Govindarajan Paul Lo Director Product Mgmt Principal MTS Sr Director Server Technologies October 04, 2017
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.
3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Program Agenda
The State of JavaScript
JavaScript Programming Oracle database using Nashorn/JVM
Oracle Database Embedded JVM (a.k.a. OJVM)
JavaScript Programming Oracle database using Nashorn/OJVM
1
2
3
4
4
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Program Agenda
The State of JavaScript
JavaScript Programming Oracle database using Nashorn/JVM
Oracle Database Embedded JVM (a.k.a. OJVM)
JavaScript Programming Oracle database using Nashorn/OJVM
1
2
3
4
5
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
The State of JavaScript
6
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
JavaScript & Java: Top Two Popular Languages
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
The State of JavaScript – The Good
After 22 years since inception, Java Script has become an ecosystem
• Douglas Crockford: JavaScript the Good Parts & New Good Parts In ES6 “a beautiful, elegant, lightweight and highly expressive language” http://bit.ly/2xaxX9Q
• Tons of libraries, frameworks, and tools*
• Versatile: all programming models, styles and applications types
• JavaScript extended thru other Languages @ http://bit.ly/1VpgaOl
• Ubiquitous: browser, middle-tier, database
8
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
The State of JavaScript – The Bad and the Ugly
• “the JavaScript ecosystem is both thriving and confusing as all hell” In A Brief, Incomplete History of JavaScript @ http://bit.ly/2haZOAb
• Many flavors
‒ ECMAScript, TypeScript, CoffeScript, PureScript, etc.
• The JavaScript fatigue
‒ Overwhelming number of frameworks and libraries: jQuery, Node.js, Backbone.js, Angular.js, Require.js, Grunt.js, React.js, Redux, Webpack, Babel, Gulp, ….
‒ Frameworks wars: e.g., Node.js vs io.js
‒ The stress of being behind
• The problem with database access
9
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
JavaScript Database Access
Client-side JavaScript (browser) should not directly access database.
Server-side JavaScript database access
• Proprietary database APIs
– StrongLoop for node-oracledb
– ActiveXObject("ADODB.Connection") for SQL Server, Access,
• Node.js cross-db ORMs
– Sequelize: for Node.js and io.js; supports PostgreSQL, MySQL, MariaDB, SQLite and MS SQL
– Bookshelf.js (Node.js); supports PostgreSQL, MySQL, MariaDB, and SQLite
– CaminteJS (Node.js): adapters for mysql, sqlite3, riak, postgres, couchdb, mongodb, redis, neo4j, firebird, rethinkdb, tingodb
• Missing a standard database access library
10
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Program Agenda
The State of JavaScript
JavaScript Programming Oracle database using Nashorn/JVM
Oracle Database Embedded JVM (a.k.a. OJVM)
JavaScript Programming Oracle database using Nashorn/OJVM
1
2
3
4
11
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
JavaScript Programming Oracle database using Nashorn/JVM
12
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
JavaScript on the JVM - Nashorn
• Project Nashorn: replacement for Rhino https://blogs.oracle.com/nashorn/ https://wiki.openjdk.java.net/display/Nashorn/Nashorn+jsr223+engine+notes https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn
• Built into Java 8
• Interoperability bw Java & JavaScript
– Use Java libraries in JavaScript i.e., JDBC (standard Java API), jOOQ (open source)
• New features in Java 9
– JEP 236: Parser API for Nashorn
– JEP 292: Implement Selected ECMAScript 6 Features in Nashorn
13
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Invoking JavaScript Procedures/Functions with Nashorn
14
Function/Procedure
Function/Procedure
javax.script
wrapper
jjs
java
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Running JavaScript with the Nashorn Command-Line
$JAVA_HOME/bin/jjs hello.js
jjs> var hw = "Hello World“ #!/usr/bin/jdk8/bin/jjs
jjs> hw print(“Hello from JavaScript”);
Hello World Hello from JavaScript
jjs> exit()
hello2.js
var hellow = "Hello from JavaScript!";
return hellow;
}
var output = hello();
print(output);
jjs hello.js
Hello from JavaScript!
15
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Running JavaScript with Nashorn using javax.script
1. Create an instance of the script engine manager
ScriptEngineManager factory = new ScriptEngineManager();
2. Create an instance of a Nashorn engine
ScriptEngine engine = factory.getEngineByName("javascript");
3. Evaluate the JavaScript code
engine.eval(“load(‘foo/bar/myScript.js’)”););
4. Invoke the JavaScript function using invokeFunction
engine.invikeFunction(“myFunction”);
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
JavaScript Database Access Using JDBC
17
JDBC SQL
SQL
SQL
In JavaScript (with Nashorn) 1. Set the vendor driver 2. Set the connect string 3. Get a JDBC connection 4. Create and run JDBC
Statements or PrepStmt 5. Get ResultSets 6. Close Rset, Stmts & Conn
Nashorn
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
JavaScript getConnection() using JDBC var getConnection = function () {
var Properties = Java.type("java.util.Properties");
var System = Java.type("java.lang.System");
var Driver = Java.type("oracle.jdbc.OracleDriver");
var DM = Java.type("java.sql.DriverManager");
var oracleDriver = new Driver();
var conn = null;
var url = null; url = "jdbc:oracle:thin:@//localhost:1521/JdbcNashorn";
var props = new Properties();
props.setProperty("user", "hr");
props.setProperty("password", "hr");
conn = DM.getConnection(url, props);
return conn;
}
18
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
queryEmp using JDBC function queryEmp()
{
var conn1 = getConnection();
var output = "";
var Stmt = conn1.createStatement();
var output = "ID First Last \n";
var RSet = Stmt.executeQuery("SELECT employee_id, first_name,
last_name from employees where rownum < 10");
while (RSet.next()) {
output = output + RSet.getString(1) + " " + RSet.getString(2) +
" " + RSet.getString(3) + "\n";
}
RSet.close();Stmt.close();conn1.close();
return output;
}
var foo = queryEmp();
rint(foo);
19
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Calling queryEmp with JJS
20
jjs -cp ojdbc8.jar:. queryEmp.js
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
selectQuery Function var selectQuery = function(id)
{
var prepStmt;
var output = "";
var conn1 = getConnection()
if(id == 'all') { prepStmt = conn1.prepareStatement("SELECT a.data FROM jemployees a");
} else {
prepStmt = conn1.prepareStatement("SELECT a.data FROM jemployees a WHERE
a.data.EmpId = ?");
prepStmt.setInt(1, id);
}
// execute Query
var resultSet = prepStmt.executeQuery();
while(resultSet.next()) {
output = output + resultSet.getString(1) + "<br>";
}
resultSet.close(); prepStmt.close(); conn1.close(); return output;
}
21
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Calling selectQuery with javax.script public class InvokeScript2 {
public static void main (String[] args) {
String output = new String();
try {
// create a script engine manager
ScriptEngineManager factory = new ScriptEngineManager();
// create a JavaScript engine
ScriptEngine engine = factory.getEngineByName("javascript");
//read the script as a java resource
engine.eval(new InputStreamReader
(InvokeScript2.class.getResourceAsStream("select2.js")));
Invocable invocable = (Invocable) engine;
Object selectResult =
invocable.invokeFunction("selectQuery", args[0]);
output = selectResult.toString();
} catch(Exception e) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
e.printStackTrace(new PrintStream(baos));
output = e + baos.toString() + e.getMessage();
}
System.out.println(output);
} 22
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Calling JavaScript selectQuery with javax.script
java -cp ojdbc8.jar:. InvokeScript2 “100”
23
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Fluent JSON Processing in Oracle database
SODA for Java
• Fluent Java API for accessing JSON collections and documents without any knowledge of SQL.
• Navigate the JSON documents from a collection using unique document keys or QBEs.
• Software pieces
orajsoda-version.jar: the SODA jar
javax.json-1.0.4.jar: the JSR353 implementation
ojdbc8.jar: JDBC jar for Java 8
• Reuse SODA for Java in Java Script
24
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Fluent JavaScript with JSON in Oracle database
SODA.js var OracleRDBMSClient = Java.type("oracle.soda.rdbms.OracleRDBMSClient");
var cl = new OracleRDBMSClient(); var db = cl.getDatabase(conn1);
col = db.admin().createCollection("MyJSONCollection");
// Create JSON documents, representing users & the # of friends they have
var doc1 = db.createDocumentFromString(
"{ \"name\" : \"Alex\", \"friends\" : \"50\" }");
// Insert the documents into a collection, one-by-one.
var resultDoc1 = col.insertAndGet(doc1);
// Retrieve the first document using its auto-generated unique ID
var fetchedDoc = col.find().key(resultDoc1.getKey()).getOne();
print (fetchedDoc.getContentAsString());
// QBE
var f = db.createDocumentFromString("{ \"friends\" : { \"$gte\" : 300}}");
25
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Calling SODA.js
• jjs -cp orajsoda-1.0.4.jar:javax.json-1.0.4.jar:ojdbc8.jar:. SODA.js
26
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Program Agenda
The State of JavaScript
JavaScript Programming Oracle database using Nashorn/JVM
Oracle Database Embedded JVM (a.k.a. OJVM)
JavaScript Programming Oracle database using Nashorn/OJVM
1
2
3
4
27
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Common Uses of Java In RDBMS
• Store procedures • Invoked from SQL, PL/SQL
• Invoked from Database clients ( JavaScript, Ruby, Scala)
• Using JDBC Driver for Data Access
• Database triggers in Java
• DB Callouts • Extends Database with Java Libraries
• Navigating JSON documents using fluent programming using SODA for Java
• Custom Indexer with Lucerne, Solr
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Architecture ( Session Based Virtual JVM)
……
Session 1
Session 2
Session 3
Session N
Sess VM
Sess VM
Sess VM
Sess VM
Oracle Java VM (shared part)
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Memory-Managed Components
VM Services
Architecture (OJVM)
AEI
RDBMS
MMAN
Classloader
Execution Engine
Interpreter JIT Thread Subsystem
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
• Classes are divided into
• read-only sharable part (in SGA)
• read-write session-private part (in UGA)
• Share as much metadata as possible • Method tables
• Bytecodes
• Compiled (JIT) code
Optimizing Memory FootPrint
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Memory Areas (Dedicated Server)
Shared memory
Process memory
UGA
sessionspace
Shared pool
Java pool
Fixed SGA
PGA PGA
newspace oldspace
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Object Memories
UGA
SGA
PGA PGA PGA
Newspace Newspace Newspace
Oldspace Oldspace Oldspace
Class Objmems
UGA UGA Sessionspace Sessionspace Sessionspace
Internspace
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Threading Subsystem • Achieve parallelism with session-level concurrency
• Non-preemptive threading
• Threads must explicitly yield or block on a lock or I/O resource to be pre-empted
• Reduce GC complexity, no lock needed around allocation and GC, do not need check and set instruction
• 10 priority level from 1 (lowest) to 10 (highest)
• Round robin within the same priority level
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Nashorn Support
• OJVM 12C supports Java 8
• Nashorn JavaScript Engine built into OJVM
• DBJAVASCRIPT Role
• Call dbms_javascript.run(‘hello.js’)
• Future release- Class GC to collect private classes in JDK8
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Program Agenda
The State of JavaScript
JavaScript Programming Oracle database using Nashorn/JVM
Oracle Database Embedded JVM (a.k.a. OJVM)
JavaScript Programming Oracle database using Nashorn/OJVM
1
2
3
4
36
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
JavaScript Programming Oracle database using Nashorn/OJVM
37
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
JavaScript Programming with OJVM
38
SQL
• 1 call • Low network latency in WAN or Cloud environments • Can be invoked by SQL or any database client
JDBC (internal)
SQL
SQL
SQL
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
How to Run JavaScript In Oracle Database
39
1. Load your JavaScript code in your Schema as Java Resource loadjava –v –u yourschema myJS.js
2. Invoke JavaScript : 3 approaches 1) Using dbms_javascript.run()from SQL or PL/SQL SQL: call dbms_javascript.run(‘myJS.js'); PL/SQL: dbms_javascript.run(‘myJS.js'); 2) Using DbmsJavaScript.run()from Java in the database import oracle.aurora.rdbms.DbmsJavaScript;
…
DbmsJavaScript.run(“myJS.js");
3) using the standard JSR223 scripting APIs javax.script
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
JDBC Connect String - Portable Across Tiers
Setting the JDBC URL depending on where the code is running if (System.getProperty("oracle.jserver.version") !== null)
// your code is running within the database JVM
{
url = "jdbc:default:connection:";
conn = oracleDriver.defaultConnection ();
}
else
// your code is running in a client JVM (outside the database)
{
url = "jdbc:oracle:thin:@//localhost:1521/pdb1.localdomain";
var props = new Properties();
props.setProperty("user", "hr");
props.setProperty("password", "hr");
conn = DM.getConnection(url, props);
}
40
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Running empQuery.js using dbms_javascript.run in OJVM runEmp.sql set serveroutput on; call dbms_java.set_output(5000);
call dbms_javascript.run('queryEmp.js');
exit
41
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Running selectQuery using javax.script in OJVM
• Wrapper for javax.script in InvokeScript2.java
CREATE OR REPLACE procedure invokeScript(inputId varchar2) as language java
name 'InvokeScript2.main(java.lang.String[])';
/
• SQL*Plus script invokescript.sql
call invokescript('&1');
• Invocation with a parameter
sqlplus hr/hr @ invokescript.sql 100
42
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Running SODA.js in OJVM
• Load SODA.js, orajsoda-version.jar, and javax.json-1.0.4.jar in hr schema
loadjava -r –v –u hr/hr …
• SQLPLus script for invoking SODA.js set serveroutput on
call dbms_java.set_output(2000);
call dbms_javascript.run('SODA.js');
exit;
43
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
Wrap Up
44
• JavaScript programming with Oracle database leverages Java 8 Nashorn on client JVM and the embedded JVM (OJVM)
• The interoperability between Java and JavaScript allows
using JDBC for database access
using SODA for Java for fluent JavaScript navigation of JSON documents
• All the code samples will be posted on GitHub @ http://bit.ly/2yfZXcZ