Class LazyTableCreation

java.lang.Object
com.iizix.server.db.LazyTableCreation

public final class LazyTableCreation extends Object
One lazy table creation at a time in a server (CC-FIX-LAZY-TABLES-CREATE-DEADLOCK, the defect IIZI-SRV-LAZY-TABLES-CREATE-DEADLOCK): the turn every such creation of a server takes, server().

Why. Several stores create their table when it is first used, and those tables have a foreign key to IZS.USERS. To create such a table Derby first READS the row of the referenced primary key in its catalog SYS.SYSCONSTRAINTS (a shared lock) and then UPDATES that row, which counts the references to the key (an exclusive lock). Two such statements in two transactions at the same moment both hold the shared lock and both wait for the exclusive one. Derby looks for a deadlock only when its deadlock timeout has passed, 20 seconds, and then fails one of the two with SQLState 40001. The first refused sign-in on a database that had neither IZS.SECURITY_EVENTS nor IZS.FAILED_SIGNINS created both within milliseconds, on two threads: the server waited 20 seconds, logged a SEVERE with a stack trace, and did not keep that first security event.

Measured (the order's probe: a Derby network server, two connections, both statements started at the same moment). In the cycle: two CREATE TABLE with a foreign key to IZS.USERS (deadlocked in 30 of 30 rounds with nothing else in either transaction), and such a CREATE TABLE beside the DROP TABLE of a table with that key (7 of 30). Not in it, 0 of 10 rounds or more each: tables without a foreign key, two creations of the SAME table (the second waits, then fails with "already exists"), ALTER TABLE ... ADD COLUMN, CREATE INDEX, an insert into a table with the key, a delete of a user. The existence check the stores run first is not the cause: with a commit between the check and the statement the two still deadlocked in 24 of 25 rounds.

What takes the server's turn. Every statement of the server that creates or drops a table with a foreign key, when the server is running: the stores that create their table at first use (SecurityEventStore, FailedSignInStore, SignInRecordStore, DeviceTokenStore, SubscriptionStore), and every script DBUtilities.createTables(java.sql.Connection,java.io.InputStream) runs (the tables of the second factor and of the passkeys, created at their first use too). WebEngineHistory creates its tables at first use as well, but they reference no other table: it takes no turn. A new table that is created lazily and has a foreign key must.

What it does not cover. A turn is one JVM's. Two SERVERS on one Derby database (the members of a hub) that each create a different one of these tables in the same moment still wait the 20 seconds once, and one of them fails that statement; its store creates the table at its next use.

Another server's turn. The creations of two servers do not wait for each other, and the stores are written for that: a table another server created first is used as found. A test in which two stores of ONE JVM stand for two servers gives one of them a turn of its own (LazyTableCreation()); a server never makes one.

The work takes no pooled connection. It runs on a connection its caller already holds. DBUtilities.createTables(java.sql.Connection,java.io.InputStream) comes here holding the monitor of DBUtilities, which DBUtilities.getPooledConnection() takes: a creation that asked for a pooled connection in here could deadlock with it.

Author:
Christopher Mindus
  • Constructor Details

    • LazyTableCreation

      public LazyTableCreation()
      Makes the turn of ANOTHER server: for a test in which a second store of one JVM stands for a second server on the same database. A server never makes one, it takes server().
  • Method Details

    • server

      public static LazyTableCreation server()
      Gets the turn of this server, which every lazy creation of the server takes.
      Returns:
      The turn, the same at every call.
    • run

      public <T> T run(LazyTableCreation.Creation<T> creation) throws SQLException
      Runs a creation in its turn: when no other creation of this turn runs. The caller looks for its table first and comes here only when it is absent, so that nothing waits for a turn once the tables exist.
      Type Parameters:
      T - What the creation answers.
      Parameters:
      creation - The creation.
      Returns:
      The creation's answer.
      Throws:
      NullPointerException - If the creation is null.
      SQLException - The creation's own.