HibernateSession.java :  » Groupware » ivatagroupware » com » ivata » groupware » container » persistence » hibernate » Java Open Source

Java Open Source » Groupware » ivatagroupware 
ivatagroupware » com » ivata » groupware » container » persistence » hibernate » HibernateSession.java
/*
 * Copyright (c) 2001 - 2005 ivata limited.
 * All rights reserved.
 * -----------------------------------------------------------------------------
 * ivata groupware may be redistributed under the GNU General Public
 * License as published by the Free Software Foundation;
 * version 2 of the License.
 *
 * These programs are free software; you can redistribute them and/or
 * modify them under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2 of the License.
 *
 * These programs are distributed in the hope that they will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU General Public License in the file LICENSE.txt for more
 * details.
 *
 * If you would like a copy of the GNU General Public License write to
 *
 * Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330
 * Boston, MA 02111-1307, USA.
 *
 *
 * To arrange commercial support and licensing, contact ivata at
 *                  http://www.ivata.com/contact.jsp
 * -----------------------------------------------------------------------------
 * $Log: HibernateSession.java,v $
 * Revision 1.8  2005/10/12 18:36:46  colinmacleod
 * Standardized format of Logger declaration - to make it easier to find instances
 * which are not both static and final.
 *
 * Revision 1.7  2005/10/11 18:51:38  colinmacleod
 * Fixed some checkstyle and javadoc issues.
 *
 * Revision 1.6  2005/10/03 10:21:14  colinmacleod
 * Fixed some style and javadoc issues.
 *
 * Revision 1.5  2005/10/02 14:08:56  colinmacleod
 * Added/improved log4j logging.
 *
 * Revision 1.4  2005/09/15 10:23:24  colinmacleod
 * Upgraded Maven to 1.1 (beta-2).
 * Upgraded Hibernate to 3.0.5.
 *
 * Revision 1.3  2005/09/14 15:20:29  colinmacleod
 * Removed unused local and class variables.
 * Added serialVersionUID.
 *
 * Revision 1.2  2005/04/09 17:19:37  colinmacleod
 * Changed copyright text to GPL v2 explicitly.
 *
 * Revision 1.1  2005/03/10 19:23:04  colinmacleod
 * Moved to ivata groupware.
 *
 * Revision 1.3  2004/11/12 15:57:11  colinmacleod
 * Removed dependencies on SSLEXT.
 * Moved Persistence classes to ivata masks.
 *
 * Revision 1.2  2004/08/01 11:54:50  colinmacleod
 * Improved transaction handling.
 *
 * Revision 1.1  2004/07/13 19:42:44  colinmacleod
 * Moved project to POJOs from EJBs.
 * Applied PicoContainer to services layer (replacing session EJBs).
 * Applied Hibernate to persistence layer (replacing entity EJBs).
 * -----------------------------------------------------------------------------
 */
package com.ivata.groupware.container.persistence.hibernate;

import java.sql.Connection;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import org.apache.log4j.Logger;

import com.ivata.mask.persistence.PersistenceException;
import com.ivata.mask.persistence.PersistenceSession;

/**
 * <p>
 * Implementation of {@link com.ivata.mask.persistence.PersistenceSession} for
 * Hibernate.
 * </p>
 *
 * <copyDoc>Refer to {@link com.ivata.mask.persistence.PersistenceSession}.
 * </copyDoc>
 *
 * @author Colin MacLeod
 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
 * @since ivata groupware 0.10 (Mar 27, 2004)
 * @version $Revision: 1.8 $
 */
public class HibernateSession implements PersistenceSession {
    /**
     * Serialization version (for <code>Serializable</code> interface).
     */
    private static final long serialVersionUID = 1L;
    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger.getLogger(
            HibernateSession.class);
    /**
     * If <code>true</code> the transaction will be rolled back.
     */
    private boolean cancel = false;
    /**
     * <p>
     * The Hibernate session we're adapting.
     * </p>
     */
    private Session session;

    /**
     * <copyDoc>Refer to {@link #getSystemSession}.</copyDoc>
     */
    private Object systemSession;

    /**
     * Hibernate transaction being wrapped.
     */
    private Transaction transaction;

    /**
     * Creates a new instance wrapping the given Hibernate session, and
     * transaction.
     *
     * @param sessionParam The actual Hibernate session which this adaptor
     * class is wrapping.
     * @param transactionParam Current Hibernate transaction.
     * @param systemSessionParam
     * <copyDoc>Refer to {@link #getSystemSession}.</copyDoc>
     */
    public HibernateSession(
            final Session sessionParam,
            final Transaction transactionParam,
            final Object systemSessionParam) {
        this.session = sessionParam;
        this.transaction = transactionParam;
        this.systemSession = systemSessionParam;
    }

    /**
     * {@inheritDoc}
     *
     * @throws PersistenceException {@inheritDoc}
     */
    public void cancel() throws PersistenceException {
        cancel = true;
    }

    /**
     * {@inheritDoc}
     *
     * @throws PersistenceException {@inheritDoc}
     */
    public void close() throws PersistenceException {
        HibernateException hibernateException = null;
        if (cancel) {
            try {
                transaction.rollback();
            } catch (HibernateException e) {
                logger.error("("
                        + e.getClass().getName()
                        + ") ROLLING BACK TRANSACTION: "
                        + e.getMessage(), e);
            } finally {
                try {
                    session.close();
                } catch (HibernateException e) {
                    if (hibernateException != null) {
                        hibernateException = e;
                    }
                }
            }

        } else {
            try {
                if (!transaction.wasRolledBack()) {
                    transaction.commit();
                }
            } catch (HibernateException e) {
                hibernateException = e;
                try {
                    transaction.rollback();
                } catch (Exception eRollback) {
                    logger.error("("
                            + e.getClass().getName()
                            + ") ROLLING BACK TRANSACTION: "
                            + e.getMessage(), e);
                }
            } finally {
                try {
                    session.close();
                } catch (HibernateException e) {
                    if (hibernateException != null) {
                        hibernateException = e;
                    }
                }
            }
        }
        if (hibernateException != null) {
            throw new PersistenceException(
                    "Error closing hibernate persistence session: ",
                    hibernateException);
        }
    }

    /**
     * {@inheritDoc}
     *
     * @return {@inheritDoc}
     * @throws PersistenceException {@inheritDoc}
     */
    public final Connection getConnection() throws PersistenceException {
        try {
            return session.connection();
        } catch (HibernateException e) {
            throw new PersistenceException(e);
        }
    }

    /**
     * Get the Hibernate session this session adapts.
     *
     * @return valid hibernate session.
     */
    Session getSession() {
        return session;
    }

    /**
     * The system session is a system-specific security session - used for
     * validation. In <em>ivata groupware</em>, you should use
     * <code>SecuritySession</code> instances.
     *
     * @return Returns the system session.
     */
    public Object getSystemSession() {
        return systemSession;
    }

    /**
     * Get the current Hibernate transaction.
     *
     * @return The current Hibernate transaction.
     */
    public Transaction getTransaction() {
        return transaction;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.