(rontolisp) docs

A JDBC program on PostgreSQL

A program written against JDBC reaches a PostgreSQL server on every target of the subset that has sockets when its driver is one the subset can compile. jdbc-postgresql is one: a driver that needs nothing but the JDK's own classes, takes the URLs that start with jdbc:postgresql: and is found by DriverManager through its services file. It lives in this repository for now and depends on nothing of rontolisp.

The driver's sources and its resources go on the program's source path and are compiled with it:

public class Items {

    public static void main(String[] args) throws SQLException {
        String url = "jdbc:postgresql://127.0.0.1:5432/postgres";
        try (Connection connection = DriverManager.getConnection(url, "postgres", "secret");
                Statement statement = connection.createStatement()) {
            statement.execute("drop table if exists items");
            statement.execute("create table items (id integer primary key, name text)");
            try (PreparedStatement insert = connection.prepareStatement("insert into items values (?, ?)")) {
                insert.setInt(1, 1);
                insert.setString(2, "お茶");
                insert.executeUpdate();
            }
            try (ResultSet rows = statement.executeQuery("select id, name from items order by id")) {
                while (rows.next()) {
                    System.out.println(rows.getInt(1) + " " + rows.getString(2));
                }
            }
        }
    }

}

(the imports are six classes of java.sql)

LIBS="--java-source-path jdbc-postgresql/src/main/java:jdbc-postgresql/src/main/resources"
rontolisp Items.java $LIBS                                     # interpreter
rontolisp Items.java $LIBS -o Items.class && java Items        # JVM
rontolisp Items.java $LIBS --native -o items && ./items        # native executable
rontolisp Items.java $LIBS -o items.wasm --component && wasmtime run -S inherit-network=y items.wasm

Under a JDK the same program runs with the driver's jar on the class path, or with another PostgreSQL driver: the program names no class of either. A program holds what it reaches of the driver: a component that logs in and runs one query is about 780 KB, and one with a PreparedStatement and non-ASCII text about 810 KB. A plain Preview 1 module has no network: the connection ends in a SQLException whose cause is the socket's.

The driver logs in by trust, password, md5 and SCRAM-SHA-256; runs statements, prepared statements and batches; reads values as the java.lang, java.math, java.sql and java.time classes; keeps transactions and savepoints; answers what the server refuses as the SQLException class of its SQLSTATE; answers DatabaseMetaData's questions of the tables, columns, keys and indexes; and is a DataSource (PostgresDataSource) for a pool given a class name. What it has and what it refuses (TLS, CallableStatement, COPY, arrays, updatable result sets) is in its README.

examples/java/jdbc/ is a longer program -- the server's address from DATABASE_URL, rows read into a record, an update, a delete, a transaction committed and one rolled back -- with its output on the four targets and on a JDK. examples/java/shiori/ is a web application whose requests keep their connections for the next ones, as a pool does (A Servlet application).

A server that requires TLS is reached through a host that owns the connection, with a second driver: Under Spin.

What differs

  • A component needs the network of its host: -S inherit-network=y, and -S allow-ip-name-lookup=y when the URL names the server by name.
  • A large result is slower to walk outside a JDK. A megabyte of rows (10,000) is read in 15-40 ms on a native executable, a component and the JVM and in about 0.1 s on the interpreter, where a JDK takes about 10 ms; reading each value of every row takes another 0.1-0.15 s, and about 1 s on the interpreter. A small query takes under a millisecond on a native executable and a component, and a login about 5 ms.
  • A SCRAM-SHA-256 login takes about 80 ms on a .wasm, which is the key derivation the method asks for.

Under Spin

jdbc-spin-postgres is a second driver for the same URLs. It opens no socket: every statement runs on spin:postgres/postgres, a WIT interface of the host Spin (4.1.0 or later), which opens the connection, pools it and speaks TLS with the server's certificate verified. A JDBC program changes its driver and nothing else:

rontolisp App.java -o app.wasm --component \
  --java-source-path jdbc-spin-postgres/src/main/java:jdbc-spin-postgres/src/main/resources:rontolisp-java-api/src/main/java

The component is served by spin up, with the server it may reach named in spin.toml (allowed_outbound_hosts = ["postgres://host:port"]). sslmode=require in the URL asks for TLS, and the sslrootcert property is a certificate of your own, as PEM text. examples/java/spin-postgres/ is a servlet doing so, in the clear and over TLS; as a component it is 1.5 MB, against 1.6 MB for the same program compiled with the socket driver.

No other host implements the interface, and on a JDK, the interpreter and the JVM nothing does: there the driver takes no URL and DriverManager goes on to the next driver, so a program with both on its path runs through the host under Spin and over a socket elsewhere. A component is compiled with this driver alone (a native executable and a Preview 1 module cannot import the interface).

The interface takes a statement and answers its rows, which sets what the driver refuses, each with a SQLFeatureNotSupportedException:

  • A timestamptz column fails the query. The refusal names the type; cast it in the statement (ts AT TIME ZONE 'UTC', or ts::text, which getTimestamp and getObject(i, OffsetDateTime.class) read as the instant).
  • A parameter always has a type. setObject(i, x, Types.OTHER) sends a text, so a json or an enum is cast in the statement (?::jsonb); setTimestamp sends a timestamp without a time zone, and an OffsetDateTime or an Instant the instant.
  • No cancel, no statement of several commands, no catalog (getTables, getColumns), and a type the host has no case for (timetz, inet, an enum) is read after a cast to text.

The rest is in the driver's README.