I have some doubts regarding use of fiilter
1)As per the document on Open Session in View, the init method does a look up for the SessionFactory. From what I have seen, the SessionFactory binds only when I do a new Configuration().configure().buildSessionFactory() which is not done here.
public void init(FilterConfig filterConfig) throws ServletException
{
// Initialize hibernate
try
{
new Configuration().configure();
}
catch (HibernateException ex) { throw new ServletException(ex); }
// As good a place as any to initialize the factory
String factoryJndiName = filterConfig.getInitParameter(HIBERNATE_FACTORY_JNDI_PARAM);
if (factoryJndiName == null)
factoryJndiName = HIBERNATE_FACTORY_JNDI_DEFAULT;
try
{
Context ctx = new InitialContext();
factory = (SessionFactory)ctx.lookup(factoryJndiName);
}
catch (NamingException ex) { throw new ServletException(ex); }
}
2)The session ic closed in the finally method of doFilter. When is this method called. Is it called explicitly somewhere
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
if (hibernateHolder.get() != null)
throw new IllegalStateException(
"A session is already associated with this thread! "
+ "Someone must have called getSession() outside of the context "
+ "of a servlet request.");
try
{
chain.doFilter(request, response);
}
finally
{
Session sess = (Session)hibernateHolder.get();
if (sess != null)
{
hibernateHolder.set(null);
try
{
sess.close();
}
catch (HibernateException ex) { throw new ServletException(ex); }
}
}
}
3)If I call ActionFilter.getSession() to perform some method in the action class as shown below, where and how will I close the session
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
UserForm userForm = (UserForm) form;
// Exceptions are caught by ActionExceptionHandler
Session ses = ActionFilter.getSession();
UserManager mgr = new UserManagerImpl(getDAOType());
userForm = mgr.getUser(ses, userForm.getUsername());
mgr.removeUser(ses, userForm);
// return a forward to searching users
return mapping.findForward("viewUsers");
}
|