Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public static final class Sessions.Db

  1. Object
  2. Sessions.Db

ImplementsSessionStore

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.

Constructors

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

Methods

public String getNamespace()The namespace this store reads and writes.
public HttpSession load(String id) throws IOExceptionThe session with this id, or null when there is none or it expired.
public void save(HttpSession session, String previousId) throws IOExceptionTwo 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.
public void delete(String id) throws IOExceptionForgets a session.
public int purgeExpired(long now) throws IOExceptionDrops every session that has expired by now; answers how many.
public int size()How many sessions are kept, or -1 when that is expensive to know.

Inherited methods

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
The session with this id, or null when there is none or it expired.

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.

delete

public void delete(String id) throws IOException
Forgets a session.

purgeExpired

public int purgeExpired(long now) throws IOException
Drops every session that has expired by now; answers how many.

size

public int size()
How many sessions are kept, or -1 when that is expensive to know.