Class Sessions.Db

java.lang.Object
com.codename1.backend.Sessions.Db
All Implemented Interfaces:
SessionStore
Enclosing class:
Sessions

public static final class Sessions.Db extends Object implements SessionStore

Sessions in the server's database, in cn1_http_session, so every instance of a server sees every session. Attributes are stored as JSON.

Every row carries a namespace, and every query is limited to the store's own. Empty unless cn1.session.namespace is set, so by default every server using the database shares its sessions -- as Spring Session's JDBC store shares one table. Browsers do not scope cookies by port, so two DIFFERENT servers on one host sharing a database then accept each other's session, sign-in included; giving each its own namespace keeps them apart, the way Spring's table-name setting does, while replicas of one server keep a shared one.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Db(DataSource pool)
    A store in the empty namespace.
    Db(DataSource pool, String namespace)
    A store whose sessions only servers with the same namespace see.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Forgets a session.
    The namespace this store reads and writes.
    The session with this id, or null when there is none or it expired.
    int
    purgeExpired(long now)
    Drops every session that has expired by now; answers how many.
    void
    save(HttpSession session, String previousId)
    Two requests of one client load separate copies of the session, and a copy written back whole would replace whatever the other request saved meanwhile -- its attributes, and a newer last use with an older one.
    int
    How many sessions are kept, or -1 when that is expensive to know.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Db

      public Db(DataSource pool)
      A store in the empty namespace.
    • Db

      public Db(DataSource pool, String namespace)
      A store whose sessions only servers with the same namespace see.
  • Method Details

    • getNamespace

      public String getNamespace()
      The namespace this store reads and writes.
    • load

      public HttpSession load(String id) throws IOException
      Description copied from interface: SessionStore
      The session with this id, or null when there is none or it expired.
      Specified by:
      load in interface SessionStore
      Throws:
      IOException
    • save

      public void save(HttpSession session, String previousId) throws IOException
      Two requests of one client load separate copies of the session, and a copy written back whole would replace whatever the other request saved meanwhile -- its attributes, and a newer last use with an older one. So an existing session is saved by re-reading the row, applying only the attributes THIS request set or removed, and writing it back only if its version has not moved; the last use only ever goes forward.
      Specified by:
      save in interface SessionStore
      Throws:
      IOException
    • delete

      public void delete(String id) throws IOException
      Description copied from interface: SessionStore
      Forgets a session.
      Specified by:
      delete in interface SessionStore
      Throws:
      IOException
    • purgeExpired

      public int purgeExpired(long now) throws IOException
      Description copied from interface: SessionStore
      Drops every session that has expired by now; answers how many.
      Specified by:
      purgeExpired in interface SessionStore
      Throws:
      IOException
    • size

      public int size()
      Description copied from interface: SessionStore
      How many sessions are kept, or -1 when that is expensive to know.
      Specified by:
      size in interface SessionStore