Rob -
I'm also trying to set up the session-per-request and transaction-per-request pattern. I've implemented a controller that extends SingleFormController as described in Pro Spring and I made a custom form object. My constructor looks like:
Code:
public EditCategoryFormController() {
setSessionForm(true); // needed to maintain the same form backing object
setBindOnNewForm(true);
setCommandName("categoryForm");
setFormView("category-edit");
}
I advised this controller to use a TransactionProxyFactoryBean, with the HibernateTransactionManager.
My question is:
Since I'm using the session to store the "working copy" of my form-backing object, and I'm binding one session per request (with OpenSessionInViewInterceptor), do I need to reattach the form-backing object to the new request's session each time into the controller? Can I do that declaratively somehow?
If not, I found this code in another post, but I think I will have to tweak it:
Code:
public static org.hibernate.Session reassociateEntity(ServletContext sc,
Object obj) {
// Remember to call sessionFactoryUtils.closeSessionIfNecessary(
SessionFactory sessionFactory = (SessionFactory) WebApplicationContextUtils
.getWebApplicationContext(sc).getBean("sessionFactory");
org.hibernate.Session ses = SessionFactoryUtils.getSession(
sessionFactory, true);
ses.lock(obj, LockMode.NONE);
return ses;
}
Is this a good pattern to use, or should I try to wrap each of my service methods in their own transactions, and then use singleSessionMode = false in OSIVInterceptor? The javadoc (Juergen) implies there will be a penalty for using this method since the first level cache is disabled.
BTW, excellent book. I think you could have doubled the length of the MVC chapter, though... specifically regarding the relationship (and associated tasks) of SimpleFormController's class ancestry.