The JDBC (Java Database Connectivity) plug-in provides the ability to access databases and execute SQL queries and statements. Database connections are automatically pooled and reused to improve resource efficiency and access speed for each database used.
In order for the JDBC plug-in to access a database, the corresponding JDBC driver (JAR file) must be installed on the RapidContext server. The plug-in comes bundled with drivers for MariaDB/MySQL and PostgreSQL. Other database vendors impose restrictions on driver distribution, so the JDBC driver must be installed locally. Follow the following steps to install a custom JDBC driver:
Restartbutton in the
Plug-intab in the Admin app
The next step is to create one or more connections using JDBC. The screenshot below shows how to create a simple test connection to a local MySQL database. Please consult your database vendor documentation for information about the correct connection URL (and possibly class name).
Only the drivers URL formats listed in the connection help are
recognized by the JDBC plug-in. Press the Show all parameters
checkbox to be able to set the custom JDBC driver Java class needed for
other data sources.
The JDBC plug-in provides a number of built-in procedures and predefined procedure types:
SELECT. Supports inserting procedure arguments into the SQL text.
INSERT,
UPDATEor
DELETE(among others). Supports inserting procedure arguments into the SQL text.
SELECT. This procedure should normally only be used for testing and development.
INSERT,
UPDATEor
DELETE(among others). This procedure should normally only be used for testing and development.
JDBC query procedures can map the database result set to a JSON data structure in various ways. By default, all rows are returned in an array and each row is mapped to an object with properties corresponding to the column names:
jdbc/query('test','SELECT * FROM fruits')
--> [{'id': 1, 'value': 'apple'},
{'id': 2, 'value': 'banana'},
{'id': 3, 'value': 'clementine'}]
This default mapping can be modified using the flags
parameter when editing or creating a procedure. The available flags are as
follows:
BINARY,
BLOB,
VARBINARYor
LONGcolumns) as a byte array instead of a string. This is sometimes necessary for processing data without encoding issues.
Date and timestamp values are currently always converted into ISO
datetime strings without timezone (e.g. 2009-01-31 23:59:59
). This
mapping will likely change in future versions, so use string conversion in
the SQL query if a specific date and time format is desired.
In contrast to SQL queries, the SQL statements (i.e.
INSERT
, UPDATE
or DELETE
)
normally have no results. When inserting new table rows however, sometimes
unique keys are automatically generated by the database engine. If that
happens, the first generated key will be returned as the procedure result
(depends on JDBC driver).
jdbc/statement('jdbc.example','INSERT INTO test VALUES (1,2,3)')
--> '4711'
The SQL text may contain procedure call arguments (for
jdbc.query and jdbc.statement procedures). These are
substituted with the procedure argument values at call time. Each procedure
argument name must be prefixed with a :
character in the
SQL text in order to be replaced:
-- Substituting a 'name' procedure argument
SELECT *
FROM customer
WHERE name = :name
The procedure argument value will always be properly escaped and quoted
(for string values) for insertion into the SQL text. When using parameter
substitution inside a WHERE
block, the comparison operator
preceding the replacement value will also be analysed and possibly
replaced for increased flexibility. See the list below for the SQL operators
recognized and processed in this way:
IS NULL. If the value is an array, it is replaced by the corresponding
IN (...)construct.
IS NOT NULL. If the value is an array, it is replaced by the corresponding
NOT IN (...)construct.
1=1test). Otherwise similar to the processing for the
=operator.
1=1
test).
Otherwise similar to the processing for the !=operator.
Connections created with the JDBC plug-in are normally pooled in the server. This means that connections are not closed immediately, but kept alive for a period of time in order to be reused. This greatly speeds up access times to most data sources, since connection creation, handshake and destruction can be relatively costly. This behavior can be fine-tuned as follows:
Connectionstab
Show all parameterscheckbox
Max Openvalue to modify the size of the connection pool (defaults to 4 connections)
Max Idle Secsvalue to modify the time-to-live for a connection (defaults to 10 minutes)
Transactions are automatically used by RapidContext when the underlying JDBC driver and database support them. The process works as follows:
Transaction commits or rollbacks can also be forced from JavaScript procedures, by calling the commit() or rollback() methods directly on the JDBC connection object.