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/30 00:19:21 UTC
svn commit: r491131 - in
/portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/ws:
./ axis/ axis/HibernateSessionAxisHandler.java
Author: dlouzan
Date: Fri Dec 29 15:19:20 2006
New Revision: 491131
URL: http://svn.apache.org/viewvc?view=rev&rev=491131
Log:
Added a new Axis handler for hibernate management.
Added:
portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/ws/
portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/ws/axis/
portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/ws/axis/HibernateSessionAxisHandler.java
Added: portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/ws/axis/HibernateSessionAxisHandler.java
URL: http://svn.apache.org/viewvc/portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/ws/axis/HibernateSessionAxisHandler.java?view=auto&rev=491131
==============================================================================
--- portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/ws/axis/HibernateSessionAxisHandler.java (added)
+++ portals/wsrp4j/trunk/persistence-db/src/java/org/apache/wsrp4j/persistence/db/ws/axis/HibernateSessionAxisHandler.java Fri Dec 29 15:19:20 2006
@@ -0,0 +1,177 @@
+/**
+ * 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.ws.axis;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.apache.axis.AxisFault;
+import org.apache.axis.MessageContext;
+import org.apache.axis.handlers.BasicHandler;
+
+import org.hibernate.FlushMode;
+import org.hibernate.SessionFactory;
+import org.hibernate.classic.Session;
+import org.hibernate.context.ManagedSessionContext;
+
+import org.apache.wsrp4j.persistence.db.driver.util.HibernateUtil;
+
+
+/**
+ * This class is an Axis handler that enables Hibernate Session management
+ * and transaction demarcation services for any service using Axis.
+ *
+ * This handler 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).
+ *
+ * This handler must be loaded on Axis server-config.wsdd file.
+ *
+ * @version $Id$
+ */
+public class HibernateSessionAxisHandler extends BasicHandler {
+
+ /* The log object */
+ private static final Log log =
+ LogFactory.getLog(HibernateSessionAxisHandler.class);
+
+ /* The Hibernate SessionFactory used for session management */
+ private SessionFactory sf;
+
+ /**
+ * This method is called on handler initialization.
+ */
+ public void init() {
+ if (log.isDebugEnabled()) {
+ log.debug("Initializing filter...");
+ log.debug("Obtaining SessionFactory from static HibernateUtil singleton");
+ }
+ sf = HibernateUtil.getSessionFactory();
+ }
+
+ /**
+ * This method is called whenever a service is requested.
+ *
+ * @param msgContext An Axis MessageContext containing the current message
+ *
+ * @throws AxisFault
+ */
+ public void invoke(MessageContext msgContext) throws AxisFault {
+
+ if (log.isDebugEnabled()) {
+ log.debug("Entering invoke");
+ }
+
+ if (!msgContext.getPastPivot()) {
+
+ /* Request phase */
+
+ /* 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();
+
+ } else {
+
+ /* Response phase */
+
+ /* 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();
+
+ }
+
+ if (log.isDebugEnabled()) {
+ log.debug("Exiting invoke");
+ }
+
+ }
+
+ /**
+ * This method is called whenever the underlying service or handler
+ * throws an exception.
+ *
+ * @param msgContext An Axis MessageContext containing the current message
+ */
+ public void onFault(MessageContext msgContext) {
+
+ if (log.isDebugEnabled()) {
+ log.debug("Entering onFault");
+ }
+
+ /* 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();
+ }
+
+ if (log.isDebugEnabled()) {
+ log.debug("Exiting onFault");
+ }
+
+ }
+
+}