?? hibernatefilterlong.java
字號:
package cn.hxex.exam.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.context.ThreadLocalSessionContext;
import cn.hxex.exam.persistence.HibernateUtil;
/**
* A servlet filter that disconnects and reconnects a Hibernate Session for each request.
* <p>
* This filter should be used if your <tt>hibernate.current_session_context_class</tt>
* configuration is set to <tt>thread</tt>
* <p>
* This filter guarantees a sane state, committing any pending database
* transaction once all other filters (and servlets) have executed.
* <p>
* Use this filter for the <b>session-per-application-transaction</b> pattern
* with a <i>Long Session</i>. Don't forget to set conversation boundaries
* in your code, as described in Hibernate in Action. One way to do this is to
* put a marker attribute in the request, and let the filter handle flushing and
* closing of the Session at the end of a conversation. Another way is to not
* use this filter at all but the Seam framework with built-in support for
* conversations.
* <p>
* This filter only works in resource-local environments
* and should not be used with CMT/JTA or BMT/JTA. You need to subclass the
* <tt>JTASessionContext</tt> for this, not use <tt>ThreadLocalSessionContext</tt>.
*
* @see org.hibernate.context.ThreadLocalSessionContext
* @see org.hibernate.context.JTASessionContext
*
* @author Christian Bauer <christian@hibernate.org>
*/
public class HibernateFilterLong
implements Filter {
private static Log log = LogFactory.getLog(HibernateFilterLong.class);
private SessionFactory sf = HibernateUtil.getSessionFactory();
private static final String HTTPSESSIONKEY = "HibernateSession";
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// Try to get a Hibernate Session from the HttpSession
HttpSession userSession =
((HttpServletRequest) request).getSession();
Session hibernateSession =
(Session) userSession.getAttribute(HTTPSESSIONKEY);
if (hibernateSession != null) {
log.debug("Binding stored Session to thread.");
ThreadLocalSessionContext.bind(hibernateSession);
}
// Always disable flushing for Long Session pattern
log.debug("Disabling automatic flushing of the current Session.");
sf.getCurrentSession().setFlushMode(FlushMode.NEVER);
try {
// Do the work...
chain.doFilter(request, response);
if (request.getAttribute("END_OF_APP_TX_MARKER") != null) {
// Begin a database transaction if none is active
sf.getCurrentSession().beginTransaction();
log.debug("Flushing Session.");
sf.getCurrentSession().flush();
log.debug("Committing the database transaction");
sf.getCurrentSession().getTransaction().commit();
log.debug("Removing Session from HttpSession.");
userSession.setAttribute(HTTPSESSIONKEY, null);
} else {
// Commit any pending database transaction
log.debug("Committing database transaction.");
sf.getCurrentSession().getTransaction().commit();
// Disconnect the Session from the thread
log.debug("Unbinding Session from thread");
hibernateSession = ThreadLocalSessionContext.unbind(sf);
// and store it in the users HttpSession.
userSession.setAttribute(HTTPSESSIONKEY, hibernateSession);
}
} catch (Throwable ex) {
// Rollback only
try {
log.debug("Trying to rollback database transaction after exception");
sf.getCurrentSession().getTransaction().rollback();
} catch (Throwable rbEx) {
log.error("Could not rollback transaction after exception!", rbEx);
}
// Let others handle it... maybe another interceptor for exceptions?
throw new ServletException(ex);
} finally {
// Cleanup
log.debug("Closing and unbinding Session from thread");
sf.getCurrentSession().close(); // Unbind is automatic here
log.debug("Removing Session from HttpSession.");
userSession.setAttribute(HTTPSESSIONKEY, null);
}
}
public void init(FilterConfig filterConfig) throws ServletException { }
public void destroy() {}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -