7.7.6 BranchingSessionMixin

##
7.7.6 BranchingSessionMixin

A persistent problem with server-side sessions is the browser state getting out of synchronisation with the application state. This occurs when the browser "back" button is used, or when a form is reloaded (this is logically equivilent to a "back" then a resubmission of the old form state).

One solution to this problem is to maintain a server-side session for each interaction with the browser, rather than a single session per client that is recycled for each interaction. A unique session identifier is stored in a hidden form field, which allows us to retrieve the appropriate version of the session on form submission (the hidden field value is rolled back with the browser state when the "back" button is used, unlike a cookie). This class provides a drop-in replacement for the `SessionServerContextMixin` and implements this session-per-interaction behaviour.

`__init__`( | )

  When you inherit from the `BranchingSessionMixin` class you must call this constructor.

`sesid`( | )

  Returns the session id.

`load_session`( | )

  This is usually called from the `Application` class `load_session()` method. Retrieves the session id and then either retrieves an existing session or creates a new session via the application object.

  If an existing session is retrieved it is passed to the `decode_session()` method (inherited from the superclass). If an exception is raised during `decode_session()` then the session will be deleted from the server and a new session will be created via the application object `new_session()` method.

`save_session`( | )

  This is called from the `Application` class `save_session()` method at the end of the request processing sequence. If the session save flag has been cleared via the `set_save_session()` method then the session is not saved.

  Before saving a session the method calls the superclass `encode_session()` then calls the `put_session()` application method to save the session.

`remove_session`( | )

  This is called from the `Application` class `remove_session()` method. The method calls the superclass `remove_session()` then calls the `del_session()` application method to remove the session at the server.

`form_close`( | )

  Called just before the `<al-form>` tag is closed. If the session is flagged to be saved a hidden field named `__albsessid__` containing the session identifier is written to the output.

  Note that this method is also present in the `RecorderMixin`, so if you inherit from the `BranchingSessionMixin` class you must define a `form_close()` method in the derived class which calls this method in both of the super classes.