pjs.getConnection()
Description
pjs.getConnection() returns the underlying connection object that Profound.js has established for a database connection. This gives a module direct access to the properties and methods of the database driver in use, for cases that the Profound.js SQL APIs do not cover.
What is returned depends on the driver configured for the connection in databaseConnections:
IBM i Db2 - The connection handle used by the Profound.js Connector for the current module.
MySQL/MariaDB and Oracle - A connection taken from the driver's connection pool.
Microsoft SQL Server - A driver
Requestobject created from the connection pool.ODBC - A connection taken from the driver's connection pool.
Connections taken from a pool are not released automatically. When the returned object provides a release or close method, call it when the module has finished with the connection so that it is returned to the pool.
Syntax
var connection = pjs.getConnection(); // default database connection
var connection = pjs.getConnection(pjs.getDB("crm")); // named database connection "crm"Parameters
Database connection (Object, optional) - A database connection definition object returned by pjs.getDB(). If omitted, the default database connection is used.
Return Value
The driver-specific connection object for the selected database connection.
Examples
Example 1: Run a Statement Directly Through the MySQL Driver
This example retrieves the pooled connection for a MySQL/MariaDB database connection, runs a statement with the driver's own query method, and releases the connection back to the pool. It is written as a web service so that the rows can be returned to the caller.
Prerequisite: a connection named mysqldb in the databaseConnections configuration option that uses the mysql driver, and a table customers in that database. Substitute your own names.
Use the driver's connection object directly
function getCustomers(request, response) {
// The pooled MySQL connection for the "mysqldb" database connection.
var connection = pjs.getConnection(pjs.getDB("mysqldb"));
// Use the driver's own API. The callback receives the rows when the query finishes.
connection.query("SELECT id, name FROM customers ORDER BY name", function (error, rows) {
// Return the connection to the pool as soon as the query has finished.
connection.release();
if (error) {
response.status(500).send(error.message);
return;
}
response.json(rows);
});
}
exports.default = getCustomers;