2014年4月28日月曜日

Hibernate Session スコープ戦略 3/3 (FMP設定編)

 Flow Managed Persistence (FMP) の設定方法の説明です。
 この戦略は Spring Web Flow (SWF)を利用します。 FMP を使う場合は、 OSIV の設定は不要です。 設定方法は、 SWF の Listener を設定している xml ファイルに、以下を記述すれば良いです。 JPA の場合は、以下のように記述を追加します。
  1. <webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
  2. <webflow:flow-execution-listeners>
  3. <webflow:listener ref="jpaFlowExecutionListener" />
  4. </webflow:flow-execution-listeners>
  5. </webflow:flow-executor>
  6. <bean id="jpaFlowExecutionListener"
  7. class="org.springframework.webflow.persistence.JpaFlowExecutionListener">
  8. <constructor-arg ref="entityManagerFactory" />
  9. <constructor-arg ref="transactionManager" />
  10. </bean>
JPA を使わず Hibernate のみの場合は、以下のように記述を追加します。
  1. <webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
  2. <webflow:flow-execution-listeners>
  3. <webflow:listener ref="hibernateFlowExecutionListener" />
  4. </webflow:flow-execution-listeners>
  5. </webflow:flow-executor>
  6. <bean id="hibernateFlowExecutionListener"
  7. class="org.springframework.webflow.persistence.HibernateFlowExecutionListener">
  8. <constructor-arg ref="sessionFactory" />
  9. <constructor-arg ref="transactionManager" />
  10. </bean>
あとは、各々のスコープを利用するフローの xml に、
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <flow xmlns="http://www.springframework.org/schema/webflow"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/webflow
  5. http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
  6.  
  7. <persistence-context />
  8. ....
  9. </flow>
のように、そのフローのスコープ内で Hibernate の Session が生きていることを設定します。 そして、スコープの終わりには、
  1. <end-state id="bookingConfirmed" commit="true" />
  2.  
のように記述することで、トランザクションのコミットと同時に Session を閉じるようにします。
 以上で、 FMP の設定は終わりです。

 まとめ
 SWF の FMP を使うことで、手動で Hibernate  と同期すること、つまり分離(detach)と付加(attach)を操作することなく、そのフロースコープを定義することで自動で同期してくれます。 References
Open Session in View by Hibernate Community https://community.jboss.org/wiki/OpenSessionInView
Spring Web Flow 2 でのフロー・マネージド・パーシスタンス by IBM
OSIV by Spring Forum
OSIVF vs OSIVI
Session Strategy by Hibernate Reference

0 件のコメント:

コメントを投稿

Scala on VSCode

Scala on VSCode ===================== Docker を利用した VSCode remote development 上で Scala 開発をする方法 Links ========== ## Scala on VSCode remote...