Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin


Wiki Markup
{float:right|background=#eee}
{contentbylabel:title=Related Articles|showLabels=false|showSpace=false|space=@self|labels=persistence}
{float}

Persistent Page Data

Info

The use of the term "persistence" here refers to page-level persistence, NOT database persistence.

Most instance variables in Tapestry are automatically cleared at the end of each request. This is important, as it pertains to how Tapestry pages are shared, over time, by many users.

Div
stylefloat:right
titleRelated Articles
classaui-label
Content by Label
showLabelsfalse
showSpacefalse
titleRelated Articles
cqllabel = "persistence" and space = currentSpace()

However, you often want to store some data on a single page, and have access to it in later requests to that same page, without having to store it in a database between requests. (To store values across multiple pages, see Session Storage.)

Making page data persist across requests to a single page is accomplished with the @Persist annotation. This annotation is applied to private instance fields of components:

Code Block
java
languagejava

  @Persist
  private int value;

...

The value for each field is the strategy used to store the field between requests.

Session Strategy


Wiki Markup
{float:right|background=#eee|padding=0 1em}
    *JumpStart Demo:*
    [Storing Data in a Page|https://tapestry-jumpstart.org/jumpstart/examples/state/storingdatainapage]
    [Passing Data Between Pages|https://tapestry-jumpstart.org/jumpstart/examples/state/passingdatabetweenpages]
{float}

The session strategy stores field changes into the session; the session is created as necessary. Session strategy is the default strategy used unless otherwise overridden.


A suitably long session attribute name is used; it incorporates the name of the page, the nested component id, and the name of the field.

Session strategy is the default strategy used unless otherwise overridden.

code
Code Block
languagejava
titleExample: Session Strategy

  @Persist
  private int value;

...

The flash is typically used to store temporary messages that should only be displayed to the user once.

Code Block
languagejava
titleExample: Flash Strategy

  @Persist(PersistenceConstants.FLASH)
  private int value;

...

Use client persistence with care, and store a minimal amount of data. Try to store the identity (that is, primary key) of an object, rather than the object itself.

Code Block
languagejava
titleExample: Client Strategy

  @Persist(PersistenceConstants.CLIENT)
  private int value;

Hibernate Entity Strategy

Entity persistence is provided by the tapestry-hibernate module (which extends Tapestry with new features).

In Entity persistence, the field should store a Hibernate entity instance.

Code Block
languagejava
title"Hibernate Entity Strategy"
  @Persist(HibernatePersistenceConstants.ENTITY)
  private User user;


The value stored in the HttpSession is a token for the entity: its Java class name and primary key. When the field is restored in a later request, the entity is re-instantiated using that data.

What is not stored is any changes to the persistent entity that are not committed to the external datastore (the database).

Starting in Tapestry 5.4, it is possible to store a non-persistent entity (a transient entity). A transient entity is stored directly into the HttpSession, and should be Serializable if the application is clustered.

JPA Entity Strategy

The tapestry-jpa module uses a similar strategy. However, at the current time it can only store a persisted entity (one that has been saved to the database and has a primary key).

Code Block
languagejava
title"Example: JPA Entity Strategy"
  @Persist(JpaPersistenceConstants.ENTITY)
  private Account account;

Persistence Strategy Inheritance

By default the value for the Persist annotation is the empty string. When this is true, then the actual strategy to be used is determined by a search up the component hierarchy.

For each component, the meta-data property tapestry.persistence-strategy is checked. This can be specified using the @Meta annotation.

If the value is non-blank, then that strategy is used. This allows a component to control the persistence strategy used inside any sub-components (that don't explicitly use a different strategy).

...

The method discardPersistentFieldChanges() of ComponentResources will discard all persistent fields for the page, regardless of which strategy is used to store the property. This will not affect the page in memory, but takes effect for subsequent requests.

Clustering Issues

The Servlet API was designed with the intention that there would be only a modest amount of server-side state, and that the stored values would be individual numbers and strings, and thus, immutable.

However, many web applications do not use the HttpSession this way, instead storing large, mutable objects in the session. This is not a problem for single servers, but in a cluster, anything stored in the session must be serialized to a bytestream and distributed to other servers within the cluster, and restored there.

Most application servers perform that serialization and distribution whenever HttpSession.setAttribute() is called. This creates a data consistency problem for mutable objects, because if you read a mutable session object, change its state, but don't invoke setAttribute(), the changes will be isolated to just a single server in the cluster.

Tapestry attempts to solve this: any session-persisted object that is read during a request will be re-stored back into the HttpSession at the end of the request. This ensures that changed internal state of those mutable objects is properly replicated around the cluster.

But while this solution solves the data consistency problem, it does so at the expense of performance, since all of those calls to setAttribute() result in extra session data being replicated needlessly if the internal state of the mutable object hasn't changed.

Tapestry has solutions to this, too:

@ImmutableSessionPersistedObject Annotation

Tapestry knows that Java's String, Number and Boolean classes are immutable. Immutable objects do not require a re-store into the session.

You can mark your own session objects as immutable (and thus not requiring session replication) using the ImmutableSessionPersistedObject annotation.

OptimizedSessionPersistedObject Interface

The OptimizedSessionPersistedObject interface allows an object to control this behavior. An object with this interface can track when its mutable state changes. Typically, you should extend from the BaseOptimizedSessionPersistedObject base class.

SessionPersistedObjectAnalyzer Service

...

Include Page
Clustering Issues
Clustering Issues

Code Block
languagejava
titleExample: Entity Session Strategy
  @Persist(HibernatePersistenceConstants.ENTITY)
  private User user;


Code Block
languagejava
title"Example:JAP Session Strategy"
  @Persist(JpaPersistenceConstants.ENTITY)
  private Account account;