MyOodbTransaction.java :  » Database-DBMS » myoodb-2.2.1 » org » myoodb » Java Open Source

Java Open Source » Database DBMS » myoodb 2.2.1 
myoodb 2.2.1 » org » myoodb » MyOodbTransaction.java
///////////////////////////////////////////////////////////////////////////////
//
//   Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
//
//                          All Rights Reserved
//
//   This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License and GNU Library
//   General Public License as published by the Free Software Foundation;
//   either version 2, or (at your option) any later version.
//
//   This program is distributed in the hope that it 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 and GNU Library General Public License
//   for more details.
//
//   You should have received a copy of the GNU General Public License
//   and GNU Library General Public License along with this program; if
//   not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
//   MA 02139, USA.
//
///////////////////////////////////////////////////////////////////////////////
package org.myoodb;

public final class MyOodbTransaction
{
    private MyOodbDatabase m_database;
    private java.util.LinkedList m_transactions;

    private long m_transactionIdentifier;
    private java.lang.Thread m_transactionThread;
    private org.myoodb.core.AbstractConnection m_databaseConnection;
    
    public MyOodbTransaction(MyOodbDatabase database)
    {
        m_database = database;
        m_transactions = new java.util.LinkedList();

        m_transactionIdentifier = -1;
        m_transactionThread = null;
        m_databaseConnection = null;
    }

    protected void setTransactionThread(java.lang.Thread thread)
    {
        m_transactionThread = thread;
    }

    protected void setTransactionIdentifier(long transactionId)
    {
        m_transactionIdentifier = transactionId;
    }

    protected void setDatabaseConnection(org.myoodb.core.AbstractConnection databaseConnection)
    {
        m_databaseConnection = databaseConnection;
    }

    protected org.myoodb.core.AbstractConnection getDatabaseConnection()
    {
        return m_databaseConnection;
    }

    public MyOodbDatabase getDatabase()
    {
        return m_database;
    }

    public long getTransactionIdentifier()
    {
        long transactionId = -1;

        if (m_transactions.size() == 0)
        {
            throw new org.myoodb.exception.TransactionException("Transaction has no id");
        }
        else if (m_transactions.size() == 1)
        {
            transactionId = m_transactionIdentifier;
        }
        else
        {
            MyOodbTransaction subTx = (MyOodbTransaction) m_transactions.getLast();
            transactionId = subTx.getTransactionIdentifier();
        }

        return transactionId;
    }

    public java.lang.Thread getTransactionThread()
    {
        return m_transactionThread;
    }

    public java.util.ArrayList getTransactions()
    {
        return new java.util.ArrayList(m_transactions);
    }

    public synchronized void begin() throws Exception
    {
        if (m_transactions.size() == 0)
        {
            m_transactions.add(this);
            m_database.beginTransaction(this);
        }
        else
        {
            MyOodbTransaction subTx = m_database.createTransaction();
            m_transactions.add(subTx);
            subTx.begin();
        }
    }

    public synchronized void join() throws Exception
    {
        if (m_transactions.size() == 0)
        {
            throw new org.myoodb.exception.TransactionException("Transaction has nothing to join");
        }
        else if (m_transactions.size() == 1)
        {
            m_database.leaveTransaction(this);
            m_database.joinTransaction(this);
        }
        else
        {
            MyOodbTransaction subTx = (MyOodbTransaction) m_transactions.getLast();
            subTx.join();
        }
    }

    public synchronized void transfer(Thread thread) throws Exception
    {
        if (m_transactions.size() == 0)
        {
            throw new org.myoodb.exception.TransactionException("Transaction has nothing to transfer");
        }
        else if (m_transactions.size() == 1)
        {
            m_database.transferTransaction(this, thread);
        }
        else
        {
            MyOodbTransaction subTx = (MyOodbTransaction) m_transactions.getLast();
            subTx.transfer(thread);
        }
    }

    public synchronized void leave() throws Exception
    {
        if (m_transactions.size() == 0)
        {
            throw new org.myoodb.exception.TransactionException("Transaction has nothing to leave");
        }
        else if (m_transactions.size() == 1)
        {
            m_transactions.removeLast();
            m_database.leaveTransaction(this);
        }
        else
        {
            MyOodbTransaction subTx = (MyOodbTransaction) m_transactions.removeLast();
            subTx.leave();
        }
    }

    public synchronized void commit() throws Exception
    {
        if (m_transactions.size() == 0)
        {
            throw new org.myoodb.exception.TransactionException("Transaction has nothing to commit");
        }
        else if (m_transactions.size() == 1)
        {
            m_transactions.removeLast();
            m_database.commitTransaction(this);
        }
        else
        {
            MyOodbTransaction subTx = (MyOodbTransaction) m_transactions.removeLast();
            subTx.commit();
        }
    }

    public synchronized void rollback() throws Exception
    {
        if (m_transactions.size() == 0)
        {
            throw new org.myoodb.exception.TransactionException("Transaction has nothing to rollback");
        }
        else if (m_transactions.size() == 1)
        {
            m_transactions.removeLast();
            m_database.rollbackTransaction(this);
        }
        else
        {
            MyOodbTransaction subTx = (MyOodbTransaction) m_transactions.removeLast();
            subTx.rollback();
        }
    }

    public synchronized int getStatus() throws Exception
    {
        if (m_transactions.size() == 0)
        {
            throw new org.myoodb.exception.TransactionException("Transaction has no status");
        }

        return m_database.getStatusTransaction((MyOodbTransaction) m_transactions.getLast());
    }
}
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.