Class Db

java.lang.Object
com.codename1.backend.Db

public final class Db extends Object

SQLite persistence for server-side binaries, on the engine the translator already bundles. Not com.codename1.db.Database, which needs a CodenameOneImplementation for every call.

Parameters are always bound, never interpolated: string concatenation into SQL is how injection happens, and a server parses input it did not write.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    A unit of work run inside transaction(Db.Work).
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Holds the connection gate (see enter()) for closeLocked().
    void
    Switches the database to write-ahead logging, which is what lets readers run while a writer is active.
    int
    execute(String sql, Object[] params)
    Holds the connection gate (see enter()) for executeLocked(String, Object[]).
    long
    Holds the connection gate (see enter()) for lastInsertIdLocked().
    static Db
    open(String path)
    Opens (or creates) the database at the given path.
    query(String sql, Object[] params)
    Holds the connection gate (see enter()) for queryLocked(String, Object[]).
    void
    setBusyTimeout(int millis)
    How long a blocked writer waits for a competing one before giving up.
    Holds the connection gate (see enter()) for transactionLocked(Db.Work).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • open

      public static Db open(String path) throws IOException
      Opens (or creates) the database at the given path. ":memory:" gives a process-lifetime database, which is what a stateless function usually wants for a cache.
      Throws:
      IOException
    • execute

      public int execute(String sql, Object[] params) throws IOException
      Holds the connection gate (see enter()) for executeLocked(String, Object[]).
      Throws:
      IOException
    • query

      public List query(String sql, Object[] params) throws IOException
      Holds the connection gate (see enter()) for queryLocked(String, Object[]).
      Throws:
      IOException
    • transaction

      public Object transaction(Db.Work body) throws Exception
      Holds the connection gate (see enter()) for transactionLocked(Db.Work).
      Throws:
      Exception
    • lastInsertId

      public long lastInsertId()
      Holds the connection gate (see enter()) for lastInsertIdLocked().
    • close

      public void close()

      Holds the connection gate (see enter()) for closeLocked().

      NEVER WAITS FOR ANOTHER CALLER. When the connection is in use, the close is recorded and the user runs it as its outermost operation ends -- after a transaction, not in the middle of one. Waiting was a hang at shutdown: HttpServer.stop() lets go of a virtual thread whose query outlived the drain window while its sqlite3_step is still on the I/O pool, and that thread holds the gate and never runs again, so a pool.close() that waited for it waited for ever. Such a connection is left to the process exit.

    • enableWriteAheadLog

      public void enableWriteAheadLog() throws IOException
      Switches the database to write-ahead logging, which is what lets readers run while a writer is active. Worth doing once after open for anything that serves concurrent requests; pointless for :memory:.
      Throws:
      IOException
    • setBusyTimeout

      public void setBusyTimeout(int millis) throws IOException
      How long a blocked writer waits for a competing one before giving up. Without this, two connections writing at once produce SQLITE_BUSY immediately rather than queueing.
      Throws:
      IOException