You are viewing a plain text version of this content. The canonical link for it is here.
Posted to wsrp4j-dev@portals.apache.org by dl...@apache.org on 2006/12/25 22:54:03 UTC
svn commit: r490183 - in
/portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/servlet:
./ HibernateSessionFilter.java
Author: dlouzan
Date: Mon Dec 25 13:54:02 2006
New Revision: 490183
URL: http://svn.apache.org/viewvc?view=rev&rev=490183
Log:
New servlet filter for servlet-based environments; it manages the lifecycle of transactions and sessions.
Added:
portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/servlet/
portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/servlet/HibernateSessionFilter.java
Added: portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/servlet/HibernateSessionFilter.java
URL: http://svn.apache.org/viewvc/portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/servlet/HibernateSessionFilter.java?view=auto&rev=490183
==============================================================================
--- portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/servlet/HibernateSessionFilter.java (added)
+++ portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/servlet/HibernateSessionFilter.java Mon Dec 25 13:54:02 2006
@@ -0,0 +1,172 @@
+/**
+ * Copyright 2003-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wsrp4j.persistence.db.servlet;
+
+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 org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.hibernate.FlushMode;
+import org.hibernate.HibernateException;
+import org.hibernate.SessionFactory;
+import org.hibernate.classic.Session;
+import org.hibernate.context.ManagedSessionContext;
+
+import org.apache.wsrp4j.persistence.db.driver.util.HibernateUtil;
+
+
+/**
+ * Servlet-filter that provides session management and transaction demarcation
+ * services for any Servlet that wishes to use persistence-db module, using
+ * Hibernate.
+ *
+ * This servlet filter creates a new hibernate session on each request and
+ * manages its lifetime instead of relying on a session-per-thread strategy.
+ * This is to prevent errors on environments like Tomcat, where a thread pool
+ * is used for incoming requests, so the same session could potentially be
+ * used for two different requests (the two sharing the same pooled request
+ * object).
+ *
+ * @version $Id$
+ */
+public class HibernateSessionFilter implements Filter {
+
+ /* The log object */
+ private static Log log = LogFactory.getLog(HibernateSessionFilter.class);
+
+ /* The Hibernate SessionFactory used for session management */
+ private SessionFactory sf;
+
+ /**
+ * Filter method used for Hibernate session management and transaction
+ * demarcation.
+ *
+ * @param request The servlet request
+ * @param response The servlet response
+ * @param chain The filter chain for delegating the processing to the next
+ * object
+ * @throws IOException
+ * @throws ServletException
+ */
+ public void doFilter(ServletRequest request, ServletResponse response,
+ FilterChain chain)
+ throws IOException, ServletException {
+
+ /* The hibernate session object */
+ Session session;
+
+ try {
+
+ /* Create a new session */
+ session = sf.openSession();
+
+ /* Bind this new session for accessing it later */
+ if (log.isDebugEnabled()) {
+ log.debug("Binding the current Session");
+ }
+ ManagedSessionContext.bind(session);
+ session.setFlushMode(FlushMode.MANUAL);
+
+ /* Start a new db transaction */
+ if (log.isDebugEnabled()) {
+ log.debug("Starting a new db transaction");
+ }
+ session.beginTransaction();
+
+ /* Delegate to the next processing */
+ chain.doFilter(request, response);
+
+ /* Unbind session after processing */
+ if (log.isDebugEnabled()) {
+ log.debug("Unbinding Session after processing");
+ }
+ session = ManagedSessionContext.unbind(sf);
+
+ /* Flush session, committ transaction and close session */
+ if (log.isDebugEnabled()) {
+ log.debug("Flushing, committing transaction and closing session");
+ }
+ session.flush();
+ session.getTransaction().commit();
+ session.close();
+
+ } catch (Throwable t) {
+ /* Rollback all operations */
+ try {
+ if (sf.getCurrentSession().getTransaction().isActive()) {
+ if (log.isDebugEnabled()) {
+ log.debug("Trying to rollback database transaction " +
+ "after exception");
+ }
+ sf.getCurrentSession().getTransaction().rollback();
+ }
+ } catch (Throwable rbT) {
+ if (log.isErrorEnabled()) {
+ log.error("Could not rollback transaction after exception!",
+ rbT);
+ }
+ } finally {
+ if (log.isErrorEnabled()) {
+ log.error("Cleanup after exception!");
+ }
+
+ /* Unbind Session after exception */
+ if (log.isDebugEnabled()) {
+ log.debug("Closing Session after exception");
+ }
+ session = ManagedSessionContext.unbind(sf);
+
+ /* Close Session after exception */
+ if (log.isDebugEnabled()) {
+ log.debug("Closing session after exception");
+ }
+ session.close();
+ }
+
+ /* Forward exception */
+ throw new ServletException(t);
+ }
+
+ }
+
+ /**
+ * Initialization method for the filter.
+ *
+ * @param filterConfig Filter configuration
+ * @throws ServletException
+ */
+ public void init(FilterConfig filterConfig) throws ServletException {
+ if (log.isDebugEnabled()) {
+ log.debug("Initializing filter...");
+ log.debug("Obtaining SessionFactory from static HibernateUtil singleton");
+ }
+ sf = HibernateUtil.getSessionFactory();
+ }
+
+ /**
+ * Destroy method for the filter.
+ */
+ public void destroy() {}
+
+}