ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException {
log.trace( "===> opening session for request [" + request.hashCode() + "]" );
// Start the session to be used for this request
Session session = HibernateUtil.getSessionFactory().openSession();
try {
// make the session available to the session factory's "current context"
ManagedSessionContext.bind( session );
// pass control along to the rest of the processing chain
chain.doFilter( request, response );
}
finally {
log.trace( "===> cleaning-up session for request [" + request.hashCode() + "]" );
// remove session from "current context"
ManagedSessionContext.unbind( HibernateUtil.getSessionFactory() );
try {
session.close();
}
catch( Throwable t ) {
log.warn( "was unable to properly close session for request [" + request.hashCode() + "]" );
}
}