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/29 01:43:09 UTC

svn commit: r490887 - in /portals/wsrp4j/trunk/persistence-db: pom.xml src/java/org/apache/wsrp4j/persistence/db/portlet/ src/java/org/apache/wsrp4j/persistence/db/portlet/HibernateSessionPortletFilter.java

Author: dlouzan
Date: Thu Dec 28 16:43:09 2006
New Revision: 490887

URL: http://svn.apache.org/viewvc?view=rev&rev=490887
Log:
- Added a dependency on portlet-api for compiling
- Created a portlet filter for managing hibernate session

Added:
    portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/portlet/
    portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/portlet/HibernateSessionPortletFilter.java
Modified:
    portals/wsrp4j/trunk/persistence-db/pom.xml

Modified: portals/wsrp4j/trunk/persistence-db/pom.xml
URL: http://svn.apache.org/viewvc/portals/wsrp4j/trunk/persistence-db/pom.xml?view=diff&rev=490887&r1=490886&r2=490887
==============================================================================
--- portals/wsrp4j/trunk/persistence-db/pom.xml (original)
+++ portals/wsrp4j/trunk/persistence-db/pom.xml Thu Dec 28 16:43:09 2006
@@ -59,6 +59,12 @@
       <scope>provided</scope>
     </dependency>
     <dependency>
+      <artifactId>portlet-api</artifactId>
+      <groupId>portlet-api</groupId>
+      <version>${portlet-api.version}</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
         <artifactId>hibernate</artifactId>
         <groupId>org.hibernate</groupId>
         <version>${hibernate.version}</version>

Added: portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/portlet/HibernateSessionPortletFilter.java
URL: http://svn.apache.org/viewvc/portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/portlet/HibernateSessionPortletFilter.java?view=auto&rev=490887
==============================================================================
--- portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/portlet/HibernateSessionPortletFilter.java (added)
+++ portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/portlet/HibernateSessionPortletFilter.java Thu Dec 28 16:43:09 2006
@@ -0,0 +1,229 @@
+/**
+ * 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.portlet;
+
+import java.io.IOException;
+
+import javax.portlet.ActionRequest;
+import javax.portlet.ActionResponse;
+import javax.portlet.PortletConfig;
+import javax.portlet.PortletException;
+import javax.portlet.RenderRequest;
+import javax.portlet.RenderResponse;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.hibernate.FlushMode;
+import org.hibernate.SessionFactory;
+import org.hibernate.classic.Session;
+import org.hibernate.context.ManagedSessionContext;
+
+import org.apache.wsrp4j.commons.consumer.util.portlet.PortletFilter;
+import org.apache.wsrp4j.commons.consumer.util.portlet.PortletFilterChain;
+
+import org.apache.wsrp4j.persistence.db.driver.util.HibernateUtil;
+
+
+/**
+ * Filter that provides session management and transaction demarcation
+ * services for any Portlet that wishes to use persistence-db module, using
+ * Hibernate. The filter assumes the little portlet filter framework on the
+ * package org.apache.commons.consumer.util.portlet.
+ *
+ * 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 HibernateSessionPortletFilter implements PortletFilter {
+    
+    /* The log object */
+    private static Log log = LogFactory.getLog(HibernateSessionPortletFilter.class);
+    
+    /* The Hibernate SessionFactory used for session management */
+    private SessionFactory sf;
+    
+    public void preHibernate() throws Exception {
+        
+        /* Create a new session */
+        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();
+            
+    }
+    
+    public void postHibernate() throws Exception {
+        
+        /* Unbind session after processing */
+        if (log.isDebugEnabled()) {
+            log.debug("Unbinding Session after processing");
+        }
+        Session 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();
+        
+    }
+    
+    public void hibernateException(Throwable t) throws PortletException {
+        
+        /* 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("Unbinding Session after exception");
+            }
+            Session session = ManagedSessionContext.unbind(sf);
+
+            /* Close Session after exception */
+            if (log.isDebugEnabled()) {
+                log.debug("Closing Session after exception");
+            }
+            session.close();
+        }
+
+        /* Forward exception */
+        throw new PortletException(t);
+        
+    }
+    
+    public void doInit(PortletFilterChain chain, PortletConfig config) 
+    throws PortletException {
+        
+        /* The hibernate session object */
+        Session session;
+        
+        try {
+            
+            /* Initialization of Hibernate session */
+            preHibernate();
+            
+            /* Delegate to the next processing */
+            chain.doInit(config);
+
+            /* Finalization of Hibernate session */
+            postHibernate();
+            
+        } catch (Throwable t) {
+            /* Handle exception and rollback */
+            hibernateException(t);
+        }
+        
+    }
+
+    public void doRender(PortletFilterChain chain, RenderRequest request, 
+            RenderResponse response)
+    throws PortletException, IOException {
+        
+        /* The hibernate session object */
+        Session session;
+        
+        try {
+            
+            /* Initialization of Hibernate session */
+            preHibernate();
+            
+            /* Delegate to the next processing */
+            chain.doRender(request, response);
+
+            /* Finalization of Hibernate session */
+            postHibernate();
+            
+        } catch (Throwable t) {
+            /* Handle exception and rollback */
+            hibernateException(t);
+        }
+
+    }
+    
+    public void doProcessAction(PortletFilterChain chain, ActionRequest request, 
+            ActionResponse response)
+    throws PortletException, IOException {
+        
+        /* The hibernate session object */
+        Session session;
+        
+        try {
+            
+            /* Initialization of Hibernate session */
+            preHibernate();
+            
+            /* Delegate to the next processing */
+            chain.doProcessAction(request, response);
+
+            /* Finalization of Hibernate session */
+            postHibernate();
+            
+        } catch (Throwable t) {
+            /* Handle exception and rollback */
+            hibernateException(t);
+        }
+
+    }
+    
+    /**
+     * Initialization method for the filter.
+     *
+     * @param config Filter configuration
+     * @throws PortletException
+     */
+    public void init(PortletConfig config) throws PortletException {
+        if (log.isDebugEnabled()) {
+            log.debug("Initializing filter...");
+            log.debug("Obtaining SessionFactory from static HibernateUtil singleton");
+        }
+        sf = HibernateUtil.getSessionFactory();
+    }
+    
+}