-->
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.  [ 4 posts ] 
Author Message
 Post subject: HibernateFilter and Springframework
PostPosted: Thu Oct 21, 2004 2:29 am 
Regular
Regular

Joined: Mon Nov 03, 2003 6:10 am
Posts: 75
Hibernate version: 2


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.OpenSessionInViewFilter' 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();
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 21, 2004 2:34 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The stuff shown in HiA is a framework-independent way of implementing the same pattern. Spring is a framework-dependent implementation, both do the same.

P.S. You are not using the Open Session in View pattern, but the Long Session pattern with reconnect/disconnect. This is much more difficult to handle, it requires you to design the rest of your application around the "Application Transaction" concept, as explained in the book. If you reread chapter 8, you are propably looking for the "Detached Object" version and the simple Filter (both filters are in CaveatEmptor).

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Simple way out
PostPosted: Thu Oct 21, 2004 3:32 am 
Regular
Regular

Joined: Mon Nov 03, 2003 6:10 am
Posts: 75
Christian thanks for the prompt reply.

I just discovered 'as you say' that I was biting off more than I could chew.

A simplier approach of:

try {
HibernateSession.beginTransaction();
} catch (Exception e) {
throw new ServletException("Unable to access current Hibernate session");
}
try {
chain.doFilter(request, response);
} finally {
try {
HibernateSession.commitTransaction();
HibernateSession.closeSession();
} catch (Exception e) {
}
}

works fine.

The issue was that some 'historical' code was not correctly beginning the transaction and thus no tx to commit.



Just out of curiousity how would adding the begin transaction call as below:

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 {
HibernateSession.beginTransaction(); // <--- begin transaction! ???
chain.doFilter(request, response);

// Commit any pending database transaction.
HibernateSession.commitTransaction();
log.info("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.info("Disconnected Session!");
// and store it in the users HttpSession
userSession.setAttribute(HTTPSESSIONKEY, hibernateSession);
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 21, 2004 3:43 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Uhm, please use the "[code]" formatting element in your postings, I can't read anything.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.