JDBC Extension
The JDBC extension enables reading from and writing to databases with a JDBC interface in Grainite applications.
Setup
In order to be able to use the JDBC extension, first include it as a dependency in your application's pom.xml
file.
Replace {GRAINITE-VERSION}
with the version of Grainite you are also using for libgrainite (the Grainite Client library for Java).
Contents
The JDBC Extension includes:
JDBCReaderTask
: Task to continuously call a stored procedure on a given database, with a timestamp parameter, to get incremental changes.JDBCWriterHandler
: Handler that will write its payload to a given table in the database
JDBCReaderTask
The reader assumes that there is a stored procedure it can invoke with a timestamp parameter, that will return changed rows along with their timestamp. It can then invoke this continuously to establish a "poor-man’s change data capture (CDC)."
Also, it is expected that the returned resultset should have a column recordId
that represents the ID of the record, when found, it is expected that the row is being updated and inserted when recordId
is not found in the record. Similarly, there is an expected entityName
on the returned row, this is then used as the key for the topic or grain message.
The JDBCReaderTask also backs-off when it does not get any results - this backoff is exponential with a max of 60 seconds.
Usage
To include this task in your application, you must specify the taskClass ext.grainite.tasks.jdbcreader.JDBCReaderTask
and taskInstanceClass ext.grainite.tasks.jdbcreader.JDBCReaderInstance
in your application's configuration YAML file.
Below are the configuration options that can be passed in under config
:
Property | Required? | Value | Description |
---|---|---|---|
| REQUIRED | Example: | JDBC Connection URL for the database |
| Optional | Example: | User ID to pass in when connecting with the JDBC interface |
| Optional | Example: | Password to pass in when connecting with the JDBC interface |
| REQUIRED | Example: | Provide this so that the Handler can check if the driver class exists. |
| REQUIRED | Example: | Stored procedure to call from the database. This procedure must accept one parameter of type timestamp and is expected to return rows updated after the given timestamp. |
| REQUIRED | Example: | Field name in the query result representing the name of the record, which will be used as the key. |
| REQUIRED | Example: | Field name in the query result representing the ID of the record. |
| Optional | Example: | Field name in the query result that can be used as the timestamp for the returned row. |
| Optional | Example: | Grainite topic to emit output of this task to |
| Optional | Example: | Grainite table to emit output of this task to via Grain message ( |
| Optional | Example: | Given action to invoke via Grain Message to the table specified in |
JDBCWriterHandler
changeType
is expected in each record with a value of ‘CREATE’
, ‘UPDATE’
or ‘DELETE’
.
Usage
To include this handler in your application, you must specify the class_name ext.grainite.handlers.jdbcwriter.JDBCWriterHandler
in your application's configuration YAML file.
Below are the configuration options that can be passed in under config
:
Property | Required? | Value | Description |
---|---|---|---|
| REQUIRED | Example: | JDBC Connection URL for the database |
| Optional | Example: | User ID to pass in when connecting with the JDBC interface |
| Optional | Example: | Password to pass in when connecting with the JDBC interface |
| Optional |
| When |
| Optional |
| When |
| Required if | Example: | Table name that this handler writes to. When not provided, the tableColumn property is used to identify the table from each record. |
| Required if | Example: | Field name in payload that provides the name of table to write to. When provided the tableColumn property is preferred over a fixed table provided in the config. |
| Required if record | Example: | field name in payload that is the key for the record (used for UPDATE and DELETE statements) |
| Optional |
| When When |
| REQUIRED | Example: | Provide this so that the Handler can check if the driver class exists. |
| Optional |
| When true, the handler produces extra output in the gxapps and jetty logs |
JDBC is a trademark of ORACLE AMERICA, INC.
Last updated