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

📄 ozoneodmgtransaction.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
字号:
// You can redistribute this software and/or modify it under the terms of// the Ozone Library License version 1 published by ozone-db.org.//// The original code and portions created by SMB are// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.//// $Id: OzoneODMGTransaction.java,v 1.1 2001/12/18 10:31:31 per_nyfelt Exp $package org.ozoneDB.odmg;import org.odmg.*;import org.ozoneDB.*;import org.ozoneDB.DxLib.*;/** * Implementation of the ODMG {@link Transaction} interface. A transaction works * against all databases that are known by the <code>OzoneODMG</odmg> factory * object when this transaction is created. *  *  * @author <a href="http://www.softwarebuero.de/">SMB</a> * @version $Revision: 1.1 $Date: 2001/12/18 10:31:31 $ * @see org.odmg.Transaction */public final class OzoneODMGTransaction implements Transaction {        /**     * Holds the ExternalTransaction objects that this ODMG transaction has     * to handle.     */    private ExternalTransaction[] subTxs;        /**     * Maps Threads into OzoneODMGTransaction.     */    private static DxMap txTable = new DxHashMap();            protected static OzoneODMGTransaction current() {        return (OzoneODMGTransaction)txTable.elementForKey( Thread.currentThread() );    }             /**     * Remove all threads that have joined the specified transaction.     * @param tx Transaction to remove from the global thread table.     */    protected static void remove( OzoneODMGTransaction tx ) {        DxIterator it = txTable.iterator();        while (it.next() != null) {            OzoneODMGTransaction cursor = (OzoneODMGTransaction)it.object();            if (cursor.equals( tx )) {                it.removeObject();            }         }     }             public OzoneODMGTransaction( DxCollection _dbs ) {        boolean readOnly = false;                if (_dbs.count() == 0) {            throw new DatabaseClosedException( "There is no open database." );        }                 subTxs = new ExternalTransaction[_dbs.count()];        DxIterator it = _dbs.iterator();        for (int i = 0; it.next() != null; i++) {            OzoneODMGDatabase odb = (OzoneODMGDatabase)it.object();            subTxs[i] = odb.underlying().newTransaction();                        if (odb.mode() == org.odmg.Database.OPEN_READ_ONLY) {                readOnly = true;            }         }                 // set all transactions to roll back only if one is opened in        // OPEN_READ_ONLY mode        try {            if (readOnly) {                for (int i = 0; i < subTxs.length; i++) {                    subTxs[i].setRollbackOnly();                }             }         } catch (Exception e) {            subTxs = null;            throw new ODMGRuntimeException( e.toString() );        }     }            public void begin() {        if (current() != null) {            throw new TransactionInProgressException( "Thread already joined." );        }                 if (subTxs == null) {            throw new TransactionNotInProgressException( "Transaction already ended." );        }                 try {            for (int i = 0; i < subTxs.length; i++) {                subTxs[i].begin();            }             txTable.addForKey( this, Thread.currentThread() );        } catch (Exception e) {            subTxs = null;            throw new ODMGRuntimeException( e.toString() );        }     }             public void join() {        if (subTxs == null) {            throw new TransactionNotInProgressException( "Transaction already ended." );        }                 leave();                if (!txTable.addForKey( this, Thread.currentThread() )) {            throw new ODMGRuntimeException( "Unable to join thread." );        }                 try {            for (int i = 0; i < subTxs.length; i++) {                subTxs[i].join();            }         } catch (Exception e) {        }     }             public void leave() {        if (subTxs == null) {            throw new TransactionNotInProgressException( "Transaction already ended." );        }                 txTable.removeForKey( Thread.currentThread() );                try {            for (int i = 0; i < subTxs.length; i++) {                subTxs[i].leave();            }         } catch (Exception e) {            throw new ODMGRuntimeException( e.toString() );        }     }             public boolean isOpen() {        return subTxs != null;    }             public void commit() {        // ODMG 3.0 lets us commit only if the threads has joined the transaction        if (!this.equals( current() )) {            throw new TransactionNotInProgressException( "Thread has not joined the transaction." );        }                 if (subTxs == null) {            throw new TransactionNotInProgressException( "Thread already ended." );        }                 // make one-phase commit if there is only one sub-transaction        if (subTxs.length == 1) {            try {                subTxs[0].commit( true );            } catch (Exception e) {                // if something was wrong the transaction is rolled back                // ODMG does not specify which exception to throw, so...                throw new ODMGRuntimeException( e.toString() );            } finally {                subTxs = null;                remove( this );            }         } else {            try {                for (int i = 0; i < subTxs.length; i++) {                    subTxs[i].prepare();                }                                 for (int i = 0; i < subTxs.length; i++) {                    subTxs[i].commit( false );                }             } catch (Exception e) {                abort();                throw new ODMGRuntimeException( e.toString() );            } finally {                subTxs = null;                remove( this );            }         }     }             public void abort() {        // ODMG 3.0 lets us commit only if the threads has joined the transaction        if (!this.equals( current() )) {            throw new TransactionNotInProgressException( "Thread has not joined the transaction." );        }                 // do not throw an exception to allow multiple calls of abort()        if (subTxs == null) {            return;        }                 try {            for (int i = 0; i < subTxs.length; i++) {                subTxs[i].rollback();            }         } catch (Exception e) {            // we can only throw the exception here            throw new ODMGRuntimeException( e.toString() );        } finally {            subTxs = null;            remove( this );        }     }             public void checkpoint() {        // ODMG 3.0 lets us commit only if the threads has joined the transaction        if (!this.equals( current() )) {            throw new TransactionNotInProgressException( "Thread has not joined the transaction." );        }                 if (subTxs == null) {            throw new TransactionNotInProgressException( "Thread already ended." );        }                 try {            for (int i = 0; i < subTxs.length; i++) {                subTxs[i].checkpoint();            }         } catch (Exception e) {            // Hmmm... should we abort here?            abort();            throw new ODMGRuntimeException( e.toString() );        }     }             /**     * Upgrade the lock on the given object to the given lock mode. The call     * has no effect if the object's current lock is already at or above that     * level of lock mode.     * <p>     * This method does nothing currently. We rely on the class descriptor for     * lock modes for now.     */    public void lock( Object obj, int lockMode ) throws LockNotGrantedException {    }             /**     * Upgrade the lock on the given object to the given lock mode.     * <p>     * This method does nothing currently. We rely on the class descriptor for     * lock modes for now.     */    public boolean tryLock( Object obj, int lockMode ) {        // we have to implement something here; for now rely on the class        // descriptor for lock modes        return true;    }             //    protected void finalize throws Throwable {    //        abort();    //        }}

⌨️ 快捷键说明

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