Class HibernateFlowExecutionListener

java.lang.Object
org.springframework.webflow.persistence.HibernateFlowExecutionListener
All Implemented Interfaces:
FlowExecutionListener

public class HibernateFlowExecutionListener extends Object implements FlowExecutionListener
A FlowExecutionListener that implements the Flow Managed Persistence Context (FMPC) pattern using the native Hibernate API.

The general pattern is as follows:

  • When a flow execution starts, create a new Hibernate Session and bind it to flow scope under the name PERSISTENCE_CONTEXT_ATTRIBUTE.
  • Before processing a flow execution request, expose the conversationally-bound session as the "current session" for the current thread.
  • When an existing flow pauses, unbind the session from the current thread.
  • When an existing flow ends, commit the changes made to the session in a transaction if the ending state is a commit state. Then, unbind the context and close it.
The general data access pattern implemented here is:
  • Create a new persistence context when a new flow execution with the 'persistenceContext' attribute starts
  • Load some objects into this persistence context
  • Perform edits to those objects over a series of requests into the flow
  • On successful flow completion, commit and flush those edits to the database, applying a version check if necessary.

Note: All data access except for the final commit will, by default, be non-transactional. However, a flow may call into a transactional service layer to fetch objects during the conversation in the context of a read-only system transaction. In that case, the session's flush mode will be set to Manual and no intermediate changes will be flushed.

Care should be taken to prevent premature commits of flow data while the flow is in progress. You would generally not want intermediate flushing to happen, as the nature of a flow implies a transient, isolated resource that can be canceled before it ends. Generally, the only time a read-write transaction should be started is upon successful completion of the conversation, triggered by reaching a 'commit' end state.

Author:
Keith Donald, Juergen Hoeller, Ben Hale
  • Field Details

    • PERSISTENCE_CONTEXT_ATTRIBUTE

      public static final String PERSISTENCE_CONTEXT_ATTRIBUTE
      The name of the attribute the flow persistence context is indexed under.
      See Also:
  • Constructor Details

    • HibernateFlowExecutionListener

      public HibernateFlowExecutionListener(org.hibernate.SessionFactory sessionFactory, org.springframework.transaction.PlatformTransactionManager transactionManager)
      Create a new Hibernate Flow Execution Listener using giving Hibernate session factory and transaction manager.
      Parameters:
      sessionFactory - the session factory to use
      transactionManager - the transaction manager to drive transactions
  • Method Details

    • setEntityInterceptor

      public void setEntityInterceptor(org.hibernate.Interceptor entityInterceptor)
      Sets the entity interceptor to attach to sessions opened by this listener.
      Parameters:
      entityInterceptor - the entity interceptor
    • sessionStarting

      public void sessionStarting(RequestContext context, FlowSession session, MutableAttributeMap<?> input)
      Description copied from interface: FlowExecutionListener
      Called after a new flow session has been created but before it starts. Useful for setting arbitrary attributes in the session before the flow starts.
      Specified by:
      sessionStarting in interface FlowExecutionListener
      Parameters:
      context - the current flow request context
      session - the session that was created
      input - a mutable input map - attributes placed in this map are eligible for input mapping by the flow definition at startup
    • paused

      public void paused(RequestContext context)
      Description copied from interface: FlowExecutionListener
      Called when a flow execution is paused, for instance when it is waiting for user input (after event processing).
      Specified by:
      paused in interface FlowExecutionListener
      Parameters:
      context - the current flow request context
    • resuming

      public void resuming(RequestContext context)
      Description copied from interface: FlowExecutionListener
      Called after a flow execution is successfully reactivated after pause (but before event processing).
      Specified by:
      resuming in interface FlowExecutionListener
      Parameters:
      context - the current flow request context
    • sessionEnding

      public void sessionEnding(RequestContext context, FlowSession session, String outcome, MutableAttributeMap<?> output)
      Description copied from interface: FlowExecutionListener
      Called when the active flow execution session has been asked to end but before it has ended.
      Specified by:
      sessionEnding in interface FlowExecutionListener
      Parameters:
      context - the current flow request context
      session - the current active session that is ending
      outcome - the outcome reached by the ending session, generally the id of the terminating end-state
      output - the flow output produced by the ending session, this map may be modified by this listener to affect the output returned
    • sessionEnded

      public void sessionEnded(RequestContext context, FlowSession session, String outcome, AttributeMap<?> output)
      Description copied from interface: FlowExecutionListener
      Called when a flow execution session ends. If the ended session was the root session of the flow execution, the entire flow execution also ends.
      Specified by:
      sessionEnded in interface FlowExecutionListener
      Parameters:
      context - the current flow request context
      session - ending flow session
      outcome - the outcome reached by the ended session, generally the id of the terminating end-state
      output - the flow output returned by the ending session
    • exceptionThrown

      public void exceptionThrown(RequestContext context, FlowExecutionException exception)
      Description copied from interface: FlowExecutionListener
      Called when an exception is thrown during a flow execution, before the exception is handled by any registered handler.
      Specified by:
      exceptionThrown in interface FlowExecutionListener
      Parameters:
      context - the current flow request context
      exception - the exception that occurred