persistence within a flow
I implemented a small web-app with spring 3.0, spring-webflow 2.3, zk 5.0.7, zkspring 3.0 and hibernate.
One of the flows shows a taskboard (zk-borderlayout) with panels on it representing the tasks. If the user adds new task I start a new subflow with flow-managed persitence. The new task is persisted at the end of the flow. Everythings works fine.
Besides that the user is able to drag and drop the panel on the taskboard in order to change the status ("not startet开发者_开发百科d", "ongoing" ...) of the task. The new status of the task should be persisted within the mentioned flow not at the end. I realized this functionality by calling special update method of the DAO (see below) in the onDrop listener of the component. That works also fine.
public void updateNow(Task task) {
EntityManager em = getJpaTemplate().getEntityManagerFactory().createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.merge(task);
tx.commit();
}
I read this article about flow-managed persitence and I ask myself if this is the right way to persist changes prior to the of my flow.
Any suggestions?
According to your referenced article (Flow-managed persistence in Spring Web Flow 2), commit a transaction beofore the end of a webflow belongs to the category "Non-atomic flow". To implement an "Atomic flow" or "Non-atomic flow" depends on your use case. So it depends on your main flow should be "atomic" or not? If it's atomic in your use case, your should persist tasks and commit at the end of web flow. As the task status, I think flowscope variables can fulfuill your requirement. If it's NOT aotmic, then you surely can commit it before the end of flow. You still can utilize flow-managed context to persist your new task. Just set your method with @Transactional(readOnly = true) which is described in "Non-Atomic Web Flow" in "Flow-managed persistence in Spring Web Flow 2".
@Transactional(readOnly = false)
public Booking createBooking(Long hotelId, String username) {
Hotel hotel = em.find(Hotel.class, hotelId);
User user = findUser(username);
Booking booking = new Booking(hotel, user);
em.persist(booking);
return booking;
}
It is developer's decision on an "atomic" or "non-atomic" web flow.
For an atomic web flow, declare @Transactional(readOnly = true)
on all the action methods in the flow, and apply <end-state commit="true"/>
at the end of the flow.
For a non-atomic web flow, applying @Transactional(readOnly = false)
commits the transaction right at the end of the annotated method. There is no need to run user managed transaction.
精彩评论