PL/SQL Invocation from the MLE JavaScript SQL Driver
Most of the Oracle Database's API is provided in PL/SQL. This is not a problem; you can easily call PL/SQL from JavaScript. Invoking PL/SQL using the MLE JavaScript SQL driver is similar to calling SQL statements.
Example 7-12 Calling PL/SQL from JavaScript
CREATE OR REPLACE MLE MODULE plsql_js_mod
LANGUAGE JAVASCRIPT AS
/**
* Read the current values for module and action and return them as
* a JavaScript object. Typically set before processing starts to
* allow you to restore the values if needed.
* @returns an object containing module and action
*/
function preserveModuleAction(){
//Preserve old module and action. DBMS_APPLICATION_INFO provides
// current module and action as OUT binds
let result = session.execute(
`BEGIN
DBMS_APPLICATION_INFO.READ_MODULE(
:l_module,
:l_action
);
END;`,
{
l_module: {
dir: oracledb.BIND_OUT,
type: oracledb.STRING
},
l_action: {
dir: oracledb.BIND_OUT,
type: oracledb.STRING
}
}
);
// Their value can be assigned to JavaScript variables
const currentModule = result.outBinds.l_module;
const currentAction = result.outBinds.l_action;
// ... and returned to the caller
return {
module: currentModule,
action: currentAction
}
}
/**
* Set module and action using DBMS_APPLICATION_INFO
* @param theModule the module name to set
* @param theAction the name of the action to set
*/
function setModuleAction(theModule, theAction){
session.execute(
`BEGIN
DBMS_APPLICATION_INFO.SET_MODULE(
:module,
:action
);
END;`,
[
theModule,
theAction
]
);
}
/**
* The only public function in this module simulates some heavy
* processing for which module and action are set using the built-in
* DBMS_APPLICATION_INFO package.
*/
export function plsqlExample(){
// preserve the values for module and action before we begin
const moduleAction = preserveModuleAction();
// set the new values to reflect the function's execution
// within the module
setModuleAction(
'plsql_js_mod',
'plsqlExample()'
)
// Simulate some intensive processing... While this is ongoing
// module and action in v$session should have changed to the
// values set earlier. You can check using
// SELECT module, action FROM v$session WHERE module = 'plsql_js_mod'
session.execute(
`BEGIN
DBMS_SESSION.SLEEP(60);
END;`
);
// and finally reset the values to what they were before
setModuleAction(
moduleAction.module,
moduleAction.action
);
}
/
This example is a little more elaborate than previous ones, separating
common functionality into their own (private) functions. You can see the use of
OUT
variables in preserveModuleAction()
's call
to DBMS_APPLICATION_INFO
. The values can be retrieved using
result.outBinds
.
After storing the current values of module and action in local variables,
additional anonymous PL/SQL blocks are invoked, first setting module and action
before entering a 60-second sleep cycle simulating complex data processing. Once the
simulated data processing routine finishes, the module and action are reset to their
original values using named IN
bind variables. Using bind variables
is more secure than string concatenation.
Setting module and action is an excellent way of informing the database about ongoing activity and allows for better activity grouping in performance reports.