I am using Hibernate and jsp to write a blog system.
I want to use a filter to manage the session and transactions. now i write a filter:
public class SessionFilter implements Filter {
FilterConfig config;
SessionFactory sessionFactory;
Session session;
public void destroy() {
session.getTransaction().commit();
this.config=null;
System.out.println("session Filter is destroyed.");
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
sessionFactory = (SessionFactory) config.getServletContext().getAttribute("sessionFactory");
session = sessionFactory.getCurrentSession();
session.beginTransaction();
chain.doFilter(request, response);
}
public void init(FilterConfig config) throws ServletException {
this.config=config;
System.out.println("session Filter is inited.");
}
}
now the result is my form fields are not saved to mysql .
I believe the
destroy()will only get called when the container shuts down. If you want to do this approach then you probably need to put thecommit()at the end ofdoFilter()