Skip to content

A fake JDBC driver to make Payara read text based configuration files (TOML/YAML/...)

License

Notifications You must be signed in to change notification settings

gdcc/jdbc-conffile-driver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fake JDBC driver to read configuration files

This is a workaround to compensate for the lack of a TOML/YAML/... Config Source in Payara. Using a fake JDBC driver, we can leverage the JDBC Config Source already present at early initialization stages. Any Config Source provided by an application is late to the party for any variable substitution using ${MPCONFIG=...}.

IMPORTANT: THIS IS EXPERIMENTAL AND STILL UNDER CONSTRUCTION. USE AT YOUR OWN RISK AND PERIL.

Attribution

Thank you, @TheElectronWill for night-config, the LGPLv3 licensed TOML lib we use. The library is shaded into the JAR to allow single-file distribution and usage as well as avoiding classpath conflicts.

Notes

TODO

  • Look into using Jackson instead of night-config (more lightweight, larger maintainer community, core libs already present)
  • Add more tests
  • Add more logging options
  • Decide how to deal with a non-existing file (fail or ignore). Maybe make it configurable via JDBC property and/or URL parameter?

Upstream

There is an inquiry to add a TOML Config Source to upstream: payara/payara#6822

Usage

IMPORTANT: For now, this driver supports only reading TOML files.

  1. After packaging (mvn package), put the JAR under glassfish/lib folder (see also Class Loading)
  2. You should restart your appserver now to make it pick up the library.
  3. Create a JDBC connection and configure the Config Source.
    asadmin create-jdbc-connection-pool --restype java.sql.Driver --driverclassname io.gdcc.jdbc.conffile.ConfFileDriver confFilePool
    # Workaround because of defunct colon escaping in create command...
    asadmin set resources.jdbc-connection-pool.confFilePool.property.url='jdbc:conffile:toml://${ENV=CONFIG_DIR}'
    asadmin create-jdbc-resource --connectionpoolid confFilePool java/db/conffile
    asadmin set-jdbc-config-source-configuration --jndiname "java/db/conffile" --tablename "dataverse" --keycolumnname "key" --valuecolumnname "value"

Hints:

  • You can replace the ${ENV=CONFIG_DIR} part with a path or other env var name, just let the (resolved) value point to a Payara user readable directory.
  • The table name will determine the files' basename. Using the example configuration above, the driver will try to read from the file at ${CONFIG_DIR}/dataverse.toml.
  • Profiles are supported, too. Just provide a file ${CONFIG_DIR}/<table name>-<profile>.toml.
  • You can provide arbitrary values for --keycolumname and --valuecolumname, they are not used as a TOML file is not a real SQL table.

Caching

Payara caches values, by default for 60s.

The consequence: when you change the TOML file, they will not be picked up until <duration> seconds have passed since they have last been read from the same file. Keep your cool with the edits.

Cache timeout can be configured in two ways:

  1. Use asadmin set-config-cache --duration=x, where x is in seconds. This is a live change!
  2. Provide a value for the key mp.config.cache.duration in any early read MP config source. This is a near-live change depending on the source you use (e.g. env vars cannot easily be changed after Payara has started).