⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transactionhibernateimpl.java

📁 A Java web application, based on Struts and Hibernate, that serves as an online running log. Users m
💻 JAVA
字号:
/* @LICENSE_COPYRIGHT@ */
package net.sf.irunninglog.hibernate;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

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

import net.sf.irunninglog.transaction.ITransaction;
import net.sf.irunninglog.transaction.IUnitOfWork;
import net.sf.irunninglog.transaction.TransactionException;
import net.sf.irunninglog.util.IdGenerator;

/**
 * Hibernate implementation of the <code>ITransaction</code> interface.  This
 * class uses the Hibernate API to provide a transaction implementation.
 *
 * @author <a href="mailto:allan_e_lewis@yahoo.com">Allan Lewis</a>
 * @version $Revision: 1.4 $ $Date: 2005/06/29 03:50:39 $
 * @since iRunningLog 1.0
 * @see ITransaction
 */
public final class TransactionHibernateImpl implements ITransaction {

    /** Log instance for this class. */
    private static final Log LOG =
                              LogFactory.getLog(TransactionHibernateImpl.class);

    /** Hibernate session used to create a transaction. */
    private Session mSession;
    /** Hibernate transaction used to provide transactional support. */
    private Transaction mTransaction;
    /** Unit of work for interacting with the persistent store. */
    private IUnitOfWork mUnitOfWork;
    /** Value uniquely identifying the transaction. */
    private long mId;
    /** Flag indicating whether or not the transaction is active. */
    private boolean mActive;

    /**
     * Create a new transaction.  Has private access; transactions are created
     * and managed by classes in the net.sf.irunninglog.transaction package.
     *
     */
    private TransactionHibernateImpl() {
        super();
        mSession = SessionFactory.openSession();
        mActive = false;
        mId = IdGenerator.generateId();

        if (LOG.isDebugEnabled()) {
            LOG.debug("Created the following transaction " + this);
        }
    }

    /**
     * Determine whether or not this transaction is active.  An active
     * transaction is one that has been begun, but not yet committed or
     * rolled back.
     *
     * @return True if the transaction is active, false otherwise
     */
    public boolean isActive() {
        return mActive;
    }

    /**
     * Get the id of the transaction.  This will return a value that uniquely
     * identifies the transaction within the context of the currently running
     * JVM.
     *
     * @return The id of this transaction
     */
    public long getId() {
        return mId;
    }

    /**
     * Initiate a transaction.  This will make the transaciton active, and
     * will establish a unit of work that may be obtained using the <code>
     * getUnitOfWork</code> method.
     *
     * @throws TransactionException If the transaction cannot be initiated
     * @see #getUnitOfWork()
     */
    public void begin() throws TransactionException {
        if (LOG.isDebugEnabled()) {
            LOG.debug("begin: Begin invoked for this transaction " + this);
        }

        try {
            mTransaction = mSession.beginTransaction();
            mUnitOfWork = new UnitOfWorkHibernateImpl(mSession);
            mActive = true;
        } catch (HibernateException ex) {
            LOG.error("Hibernate exception while beginning a transaction", ex);
            throw new TransactionException("Unable to begin transaction", ex);
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug("begin: This transaction has successfully begun " + this);
        }
    }

    /**
     * Commit a transaction.  Any work done using the transaction's unit of
     * work will be committed to the persistent store, and the transaction will
     * be made inactive.
     *
     * @throws TransactionException If the transaciton cannot be committed
     */
    public void commit() throws TransactionException {
        if (LOG.isDebugEnabled()) {
            LOG.debug("commit: Commit invoked for this transaction " + this);
        }

        try {
            mTransaction.commit();
            mActive = false;
            mSession.close();
        } catch (HibernateException ex) {
            LOG.error("Hibernate exception while committing a transaction", ex);
            throw new TransactionException("Unable to commit transaction", ex);
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug("commit: This transaction has been committed " + this);
        }
    }

    /**
     * Roll back a transaction.  Any work done using the transaction's unit of
     * work will be discarded, and the transaction will be made inactive.
     *
     * @throws TransactionException If the transaciton cannot be rolled back
     */
    public void rollback() throws TransactionException {
        if (LOG.isDebugEnabled()) {
            LOG.debug("rollback: Rollback invoked for this transaction "
                      + this);
        }

        try {
            mTransaction.rollback();
            mActive = false;
            mSession.close();
        } catch (HibernateException ex) {
            LOG.error("Hibernate exception while rolling back a transaction",
                      ex);
            throw new TransactionException("Unable to rollback transaction",
                                           ex);
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug("rollback: This transaction has been rolled back "
                      + this);
        }
    }

    /**
     * Retrieve the unit of work for this transaction.  This will return an
     * <code>IUnitOfWork</code> instance that can be used to perform actions
     * against the application's persistent store.
     *
     * @return The transaction's unit of work
     * @throws TransactionException If the unit of work cannot be obtained
     * @see IUnitOfWork
     */
    public IUnitOfWork getUnitOfWork() throws TransactionException {
        if (LOG.isDebugEnabled()) {
            LOG.debug("getUnitOfWork: Getting the unit of work for this"
                      + " transaction " + this);
        }

        return mUnitOfWork;
    }

    /**
     * Custom toString override to provide a meaningful <code>String</code>
     * representation of this object.
     *
     * @return A <code>String</code> representation of this object
     */
    public String toString() {
        StringBuffer buff = new StringBuffer();

        buff.append("TransactionHibernateImpl[");
        buff.append("id='");
        buff.append(getId());
        buff.append("'");
        buff.append(" active='");
        buff.append(isActive());
        buff.append("'");
        buff.append(" session={");
        buff.append(mSession);
        buff.append("}");
        buff.append(" transaction={");
        buff.append(mTransaction);
        buff.append("}");
        buff.append(" unitOfWork={");
        buff.append(mUnitOfWork);
        buff.append("}");
        buff.append("]");

        return buff.toString();
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -