You can use the
policy to query a remote database and return a (single or multiple) record set.gravitee-policy-jdbc-query
Note
|
It should go without saying that database queries are expensive operations and heavily dependent on the network connection between the API Gateway and the database service. For the best experience, implement an API Gateway close to the database service, and only deploy APIs to those API Gateways (that are close to the database service). |
Note
|
Before using this policy (and in addition to the above note), you may want to think of other options such as storing typical responses as API Properties or using the Gravitee Dictionary feature. |
The Connection String (
) supports the standard JDBC connection syntax, e.g.: jdbcConnectionString
jdbc:mysql://hostname:3306/database
If your query returns no results, then the policy does nothing except adding the X-JDBC: no-results
header.
If your query has a SQL syntax error, then the policy also does nothing except adding
header. Further error information can be discovered in the logs, or by enabling the "Debug to Headers" toggle.X-JDBC: Error processing JDBC - SQL Syntax error
Else, a
object will be created from the results of your SQL statement and the header jdbcResponse
X-JDBC: success
` will be added. Use the
object to extract desired field values into Context Variables.jdbcResponse
Example queries:
SELECT * FROM some_table WHERE id=1 SELECT firstName, lastName FROM some_table WHERE id=1 SELECT firstName, lastName, id, some_column FROM some_table WHERE id={#request.headers['myUserId']}
The
object includes a jdbcResponse
array (regardless of the number of records retrieved). Each record is an item in the array. Each field (or column) specified in your SELECT statement is a key/value pair.content
Example
object:jdbcResponse
{
"message": "success",
"content": [
{
"id": 1,
"firstName": "Fred",
"lastName": "Durst",
"some_column": "some_text1"
},
{
"id": 2,
"firstName": "Serj",
"lastName": "Tankian",
"some_column": "some_text2"
}
]
}
For debugging purposes, you can enable Debug to Headers (
) and either the full record set results or SQL Exception will be added to the jdbcDebugHeaders
X-JDBC-DEBUG-RESPONSE
header.
You can configure the policy with the following options:
Property | Required | Description | Type | Default |
---|---|---|---|---|
jdbcConnectionString |
X |
Specify the JDBC connection string. (Supports EL) |
string |
|
jdbcConnectionUsername |
X |
Specify the JDBC connection username. (Supports EL) |
string |
|
jdbcConnectionPassword |
X |
Specify the JDBC connection password. (Supports EL) |
string |
|
jdbcQuery |
X |
Specify the SQL query string. (Supports EL) |
string |
|
variables |
The variables to set in the execution context when a valid recordset is returned by your SQL query. (Supports EL) |
List of variables |
- |
|
jdbcHeaders |
If enabled, Gravitee will populate the |
boolean |
|
|
jdbcDebugHeaders |
If enabled, Gravitee will populate the |
boolean |
|
Example configuration:
{
"policy": "gravitee-policy-jdbc-query",
"configuration": {
"jdbcConnectionString": "jdbc:mysql://db4free.net:3306/dbtest_database",
"jdbcConnectionUsername": "dbtest_user",
"jdbcConnectionPassword": "dbtest_pass",
"jdbcQuery": "SELECT firstName, lastName FROM some_table WHERE id={#request.headers['myUserId']}",
"variables": [
{
"name": "FirstName",
"value": "{#jsonPath(#jdbcResponse.content[0], '$.firstName')}"
}
]
}
}