Announcement Announcement Module
No announcement yet.
HibernateFilter Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • HibernateFilter

    As per the new Hibernate In Action I implemented a HibernateFilter to utilize a request scoped transaction.

    Weird issue is that it appears the filter only commits after a second request. i.e. click link 'saveStuff'
    check database (nada)
    click any other link
    save from step 1 is commited from filter

    Did I do something obvious? I did some searching and it appears the new SpringFramework version has a built in ' sionInViewFilter' which appears to be the same thing. Must I use this? OR should this be correct:

    public class HibernateFilter
    implements Filter {

    private static final String HTTPSESSIONKEY = "HibernateSession";
    private static Log log = LogFactory.getLog(HibernateFilter.class);

    public void init(FilterConfig filterConfig) throws ServletException {"Servlet filter init, now disconnecting/reconnecting a Session for each request.");

    public void doFilter(ServletRequest request,
    ServletResponse response,
    FilterChain chain)
    throws IOException, ServletException {
    // Try to get a Hibernate Session from the HttpSession
    HttpSession userSession =
    ((HttpServletRequest) request).getSession();
    Session hibernateSession =
    (Session) userSession.getAttribute(HTTPSESSIONKEY);

    if (hibernateSession != null)

    // If there is no Session, the first call to
    // HibernateSession.beginTransaction in application code will open
    // a new Session for this thread.
    try {
    chain.doFilter(request, response);

    // Commit any pending database transaction.
    log.debug("Committed Transaction!");
    }catch(Throwable t){
    log.fatal("Fatal error closing transaction in filter", t);

    finally {
    // TODO: The Session should be closed if a fatal exceptions occurs

    // No matter what happens, disconnect the Session.
    hibernateSession = HibernateSession.disconnectSession();
    log.debug("Disconnected Session!");
    // and store it in the users HttpSession
    userSession.setAttribute(HTTPSESSIONKEY, hibernateSession);

    public void destroy() {


    Along with:



    Note: the Transaction is started in the Dao constructor as such:

    public HibernateDao() {

  • #2
    Session utils

    I'm not aware how 'HiA' implemented this thing, but I certainly recommend using Spring's Hibernate utilities. This 'Session per thread' pattern is written in a very more mature way. Not to mention that it is integrated in all sort of other Hibernate related stuff - TransactionManager, DaoSupport, Template, Interceptor, ...
    Simply read "" and have a look at OpenSessionInViewFilter or maybe even OpenSessionInViewInterceptor if you are planning to use Spring's MVC.

    Regards, Ales


    • #3
      I would concur on using Spring's own OpenSessionInViewFilter (or interceptor) if you want to use this idiom. It can actaully work in both a singleSession=true mode where there is one session the whole time, or a singleSession=false mode (cleaner, I think), where all it does is defer closing of sessions until the request is done. In either case, there is full intergration with Spring's Hibernate utility classes such that they pick up the session from the thread, where it has been placed...


      • #4
        No transaction handling/commit occur in OpenSessionInView

        No transaction handling/commit occur in OpenSessionInView. I guess what we need is something that does the same but starts and ends a transaction.


        • #5

          There's been a lot already written on OSIV:
          (check Colin's debate at the bottom)

          But if there are some ideas?

          Rgds, Ales
          Last edited by robyn; May 14th, 2006, 11:15 AM.