(window.webpackJsonp=window.webpackJsonp||[]).push([[347],{771:function(e,t,n){"use strict";n.r(t);var a=n(56),s=Object(a.a)({},(function(){var e=this,t=e.$createElement,n=e._self._c||t;return n("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[n("h1",{attrs:{id:"_7-flow-managed-persistence"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_7-flow-managed-persistence"}},[e._v("#")]),e._v(" 7. Flow Managed Persistence")]),e._v(" "),n("h2",{attrs:{id:"_7-1-introduction"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_7-1-introduction"}},[e._v("#")]),e._v(" 7.1. Introduction")]),e._v(" "),n("p",[e._v('Most applications access data in some way.\nMany modify data shared by multiple users and therefore require transactional data access properties.\nThey often transform relational data sets into domain objects to support application processing.\nWeb Flow offers "flow managed persistence" where a flow can create, commit, and close a object persistence context for you.\nWeb Flow integrates both Hibernate and JPA object persistence technologies.')]),e._v(" "),n("p",[e._v("Apart from flow-managed persistence, there is the pattern of fully encapsulating PersistenceContext management within the service layer of your application.\nIn that case, the web layer does not get involved with persistence, instead it works entirely with detached objects that are passed to and returned by your service layer.\nThis chapter will focus on the flow-managed persistence, exploring how and when to use this feature.")]),e._v(" "),n("h2",{attrs:{id:"_7-2-flowscoped-persistencecontext"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_7-2-flowscoped-persistencecontext"}},[e._v("#")]),e._v(" 7.2. FlowScoped PersistenceContext")]),e._v(" "),n("p",[e._v("This pattern creates a "),n("code",[e._v("PersistenceContext")]),e._v(" in "),n("code",[e._v("flowScope")]),e._v(" on flow startup,\nuses that context for data access during the course of flow execution, and commits changes made to persistent entities at the end.\nThis pattern provides isolation of intermediate edits by only committing changes to the database at the end of flow execution.\nThis pattern is often used in conjunction with an optimistic locking strategy to protect the integrity of data modified in parallel by multiple users.\nTo support saving and restarting the progress of a flow over an extended period of time, a durable store for flow state must be used.\nIf a save and restart capability is not required, standard HTTP session-based storage of flow state is sufficient.\nIn that case, session expiration or termination before commit could potentially result in changes being lost.")]),e._v(" "),n("p",[e._v("To use the FlowScoped PersistenceContext pattern, first mark your flow as a "),n("code",[e._v("persistence-context")]),e._v(":")]),e._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[e._v('\n\n\n \n\n\n\n')])])]),n("p",[e._v("Then configure the correct "),n("code",[e._v("FlowExecutionListener")]),e._v(" to apply this pattern to your flow.\nIf using Hibernate, register the "),n("code",[e._v("HibernateFlowExecutionListener")]),e._v(". If using JPA, register the "),n("code",[e._v("JpaFlowExecutionListener")]),e._v(".")]),e._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[e._v('\n \n \n \n\n\n\n \n \n\n\n')])])]),n("p",[e._v("To trigger a commit at the end, annotate your end-state with the commit attribute:")]),e._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[e._v('\n\n')])])]),n("p",[e._v("That is it. When your flow starts, the listener will handle allocating a new "),n("code",[e._v("EntityManager")]),e._v(" in "),n("code",[e._v("flowScope")]),e._v(".\nReference this EntityManager at anytime from within your flow by using the special "),n("code",[e._v("persistenceContext")]),e._v(" variable.\nIn addition, any data access that occurs using a Spring managed data access object will use this EntityManager automatically.\nSuch data access operations should always execute non transactionally or in read-only transactions to maintain isolation of intermediate edits.")]),e._v(" "),n("h2",{attrs:{id:"_7-3-flow-managed-persistence-and-sub-flows"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_7-3-flow-managed-persistence-and-sub-flows"}},[e._v("#")]),e._v(" 7.3. Flow Managed Persistence And Sub-Flows")]),e._v(" "),n("p",[e._v("A flow managed "),n("code",[e._v("PersistenceContext")]),e._v(" is automatically extended\n(propagated) to subflows assuming the subflow also has the "),n("code",[e._v("")]),e._v("variable. When a subflow re-uses the "),n("code",[e._v("PersistenceContext")]),e._v(" started by its parent it ignores\ncommit flags when an end state is reached thereby deferring the final decision (to commit or not) to\nits parent.")])])}),[],!1,null,null,null);t.default=s.exports}}]);