-
Oct 21st, 2004, 01:28 AM
#1
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 'org.springframework.orm.hibernate.support.OpenSes 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 {
log.info("Servlet filter init, now disconnecting/reconnecting a Session for each request.");
}
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
log.debug("HibernateFilter.doFilter()");
// Try to get a Hibernate Session from the HttpSession
HttpSession userSession =
((HttpServletRequest) request).getSession();
Session hibernateSession =
(Session) userSession.getAttribute(HTTPSESSIONKEY);
if (hibernateSession != null)
HibernateSession.reconnect(hibernateSession);
// 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.
HibernateSession.commitTransaction();
log.debug("Committed Transaction!");
}catch(Throwable t){
t.printStackTrace();
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:
<filter>
<filter-name>HibernateFilter</filter-name>
<filter-class>com.shopbotz.servlet.HibernateFilter2</filter-class>
</filter>
<filter-mapping>
<filter-name>HibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Note: the Transaction is started in the Dao constructor as such:
public HibernateDao() {
HibernateSession.beginTransaction();
}
-
Oct 21st, 2004, 02:27 AM
#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 "http://www.springframework.org/docs/reference/orm.html" and have a look at OpenSessionInViewFilter or maybe even OpenSessionInViewInterceptor if you are planning to use Spring's MVC.
Regards, Ales
-
Oct 23rd, 2004, 09:32 AM
#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...
-
Nov 2nd, 2004, 09:12 PM
#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.
-
Nov 3rd, 2004, 02:47 AM
#5
Osiv
There's been a lot already written on OSIV:
(check Colin's debate at the bottom)
http://forum.springframework.org/showthread.php?t=11123
But if there are some ideas?
Rgds, Ales
Last edited by robyn; May 14th, 2006 at 11:15 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules