/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.tm;

import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

/**
 * ServerSideUserTransaction 工作在服务器端，比如用在 ejb 中的 UserTransaction
 * <p/>
 * 还有一种 UserTransaction 完全工作在客户端，比如一般应用程序中的 UserTransaction
 * 或者一个 ejb 客户端(非ejb)中使用的UserTransaction
 *
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class ServerUserTransaction implements UserTransaction {
    private static ServerUserTransaction me = null;
    private static TxManager tm = null;

    private ServerUserTransaction() {
        tm = TxManager.getInstance();
    }

    public static ServerUserTransaction getInstance() {
        // when de-serialize, must re initial me
        if(me == null) {
            me = new ServerUserTransaction();
        }
        return me;
    }

    public void begin() throws NotSupportedException, SystemException {
        tm.begin();
    }

    public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException {
        tm.commit();
    }

    public void rollback() throws IllegalStateException, SecurityException, SystemException {
        tm.rollback();
    }

    public void setRollbackOnly() throws IllegalStateException, SystemException {
        tm.setRollbackOnly();
    }

    public int getStatus() throws SystemException {
        return tm.getStatus();
    }

    public void setTransactionTimeout(int timeout) throws SystemException {
        tm.setTransactionTimeout(timeout);
    }

    public int getNumTransactions() {
        return tm.getNumTransactions();
    }

    public int getNumSuspendedTransactions() {
        return tm.getNumSuspendedTransactions();
    }

    public static void main(String[] args) {

    }
}

