Interface DatabaseConnections

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface DatabaseConnections
The database connections configured in Traffic Parrot's database-connections.json, from TrafficParrotServices.databaseConnections().

 DataSource dataSource = databaseConnections.dataSource(parameters.getString("connectionId"));
 try (Connection connection = dataSource.getConnection()) {
     ...
 }
 
The connection id comes from the mapping, for example its transformerParameters, as the dataSource Handlebars helper takes it inline. In a unit test, implement dataSource(java.lang.String) alone, for example over an in-memory database; the other methods have defaults. Methods added in later versions will have defaults too.
  • Method Summary

    Modifier and Type
    Method
    Description
    default Set<String>
     
    dataSource(String connectionId)
    A DataSource for the JDBC connection with this connectionId in database-connections.json, for a library that takes one.
  • Method Details

    • dataSource

      DataSource dataSource(String connectionId)
      A DataSource for the JDBC connection with this connectionId in database-connections.json, for a library that takes one.

      Each DataSource.getConnection() reads the file again and today opens a new connection the way the dataSource Handlebars helper does. A later release may pool them, so close every connection and leave it as you found it (auto-commit, isolation). It fails only with an SQLException: if the JDBC driver is not in lib/external, the database refuses the connection, or the file no longer has a JDBC connection with this id. The message names the connection id and, for a refused connection, the JDBC URL, with passwords masked; the driver's own exception is not attached. The user and password come from the file, so DataSource.getConnection(String, String) is not supported.

      Throws:
      IllegalArgumentException - if no connection has this id, it is not a JDBC connection, it has no driverClass, or database-connections.json is inconsistent (two connections with one id)
      UncheckedIOException - if database-connections.json cannot be read or parsed
      NullPointerException - if connectionId is null
    • connectionIds

      default Set<String> connectionIds()
      Returns:
      the ids of all connections in database-connections.json, of every type; dataSource(java.lang.String) accepts only the JDBC ones
      Throws:
      IllegalArgumentException - if database-connections.json is inconsistent (two connections with one id)
      UncheckedIOException - if database-connections.json cannot be read or parsed
      UnsupportedOperationException - if this implementation does not list them, as a test's may not