Наконец мы удалили Stripersist и создали свой собственный стек. Мы не пробуем решение, предложенное Kdeveloper.
Сначала мы написали перехватчик Stripe:
@Intercepts({LifecycleStage.RequestInit, LifecycleStage.RequestComplete})
public class HibernateTxInterceptor implements net.sourceforge.stripes.controller.Interceptor {
private SessionFactory getUserSessionFactory(ExecutionContext context) {
return (SessionFactory) context.getActionBeanContext().getServletContext().getAttribute(MyInitListener.CTX_SESSION_USERS);
}
private SessionFactory getDocumentSessionFactory(ExecutionContext context) {
return (SessionFactory) context.getActionBeanContext().getServletContext().getAttribute(MyInitListener.CTX_SESSION_DOCUMENTS);
}
public Resolution intercept(ExecutionContext context) throws Exception {
LifecycleStage stage = context.getLifecycleStage();
if (stage == LifecycleStage.RequestInit) {
// Init transactions
Transaction userTx = getUserSessionFactory(context).getCurrentSession().beginTransaction();
Transaction documentTx = getDocumentSessionFactory(context).getCurrentSession().beginTransaction();
} else if (stage.equals(LifecycleStage.RequestComplete)) {
// Commit Transactions
Transaction userTx = getUserSessionFactory(context).getCurrentSession().getTransaction();
userTx.commit();
...
// Idem for documentTx...
}
}
И мы создали слушатель сервлета для внедрения двух фабрик сессий Hibernate с помощью Spring:
public class Reef4iInitListener implements ServletContextListener {
public static final String CTX_SESSION_USERS = "user";
public static final String CTX_SESSION_DOCUMENTS = "document";
private ServletContext servletContext;
public void contextInitialized(ServletContextEvent e) {
servletContext = e.getServletContext();
final WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(e.getServletContext());
SessionFactory usersSessionFactory = (SessionFactory) springContext.getBean("usersSessionFactory");
... // do the same with for document factory
}
public void contextDestroyed(ServletContextEvent e) {
SessionFactory userTx = (SessionFactory) e.getServletContext().getAttribute(CTX_SESSION_USERS);
userTx.close();
.... // do the same for document factory
}