-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Open Session in View
PostPosted: Wed Sep 10, 2003 7:28 am 
Newbie

Joined: Wed Aug 27, 2003 4:39 am
Posts: 10
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");
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2003 9:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
The filter provides the session opening (and session placement in the thread context) ready for you to process the servlet request. It will then close the session for you. The call sequence is:

1) The doFilter method is called by the container before your servlet is called,
2) the servlet is processed as a part of the chain and
3) then the session is closed which is the end of the doFilter call.

Maybe this is not a clear explanation. Filters are a part of the 2.3 servlet spec.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.