FileDocCategorySizeDatePackage
Transaction.javaAPI DocExample6450Thu Aug 24 21:03:52 BST 2000com.imaginary.lwp

Transaction

public abstract class Transaction extends Object
An abstract representation of a data storage transaction. This class manages the life cycle of a data storage transaction. Applications can get a transaction instance by calling getCurrent. The transaction does not begin, however, until the begin method is called by an application.
Last modified $Date: 1999/11/20 17:33:19 $
version
$Revision: 1.7 $
author
George Reese (borg@imaginary.com)

Fields Summary
private static HashMap
transactions
private long
timestamp
private HashSet
toCreate
private HashSet
toRemove
private HashSet
toStore
private Identifier
userID
Constructors Summary
public Transaction()

    
      
        super();
    
Methods Summary
public final synchronized voidbegin()

        if( timestamp == -1L ) {
            timestamp = (new Date()).getTime();
        }
    
public abstract voidcommit()

public final synchronized voidend()

        try {
            Iterator obs;

            obs = toRemove.iterator();
            while( obs.hasNext() ) {
                BaseEntity p = (BaseEntity)obs.next();

                p.remove(this);
            }
            obs = toCreate.iterator();
            while( obs.hasNext() ) {
                BaseEntity p = (BaseEntity)obs.next();

                p.create(this);
            }
            obs = toStore.iterator();
            while( obs.hasNext() ) {
                BaseEntity p = (BaseEntity)obs.next();
                
                p.store(this);
            }
            commit();
            obs = toRemove.iterator();
            while( obs.hasNext() ) {
                BaseEntity p = (BaseEntity)obs.next();

                p.commit(this);
            }
            obs = toCreate.iterator();
            while( obs.hasNext() ) {
                BaseEntity p = (BaseEntity)obs.next();

                p.commit(this);
            }
            obs = toStore.iterator();
            while( obs.hasNext() ) {
                BaseEntity p = (BaseEntity)obs.next();
                
                p.commit(this);
            }
            toCreate.clear();
            obs = toRemove.iterator();
            while( obs.hasNext() ) {
                BaseEntity p = (BaseEntity)obs.next();
                
                //p.invalidate();
            }
            toRemove.clear();
            toStore.clear();
            Transaction.transactions.remove(userID);
        }
        catch( TransactionException e ) {
            Transaction trans;
            Iterator obs;

            e.printStackTrace();
            rollback();
            Transaction.transactions.remove(userID);
            // use a different transaction to reload everyone
            trans = Transaction.getCurrent(userID);
            obs = toRemove.iterator();
            while( obs.hasNext() ) {
                BaseEntity ob = (BaseEntity)obs.next();

                try {
                    ob.reload(trans);
                }
                catch( Exception disaster ) {
                    // remove it from the cache or something
                }
            }
            obs = toStore.iterator();
            while( obs.hasNext() ) {
                BaseEntity ob = (BaseEntity)obs.next();

                try {
                    ob.reload(trans);
                }
                catch( Exception disaster ) {
                    // remove it from the cache or something
                }
            }
            throw e;
        }
        catch( Exception e ) {
            rollback();
            throw new TransactionException(e.getMessage());
        }
        finally {
            timestamp = -1L;
        }
    
public booleanequals(java.lang.Object ob)

        if( !(ob instanceof Transaction) ) {
            return false;
        }
        else {
            Transaction trans = (Transaction)ob;

            return trans.userID.equals(userID);
        }
    
public static com.imaginary.lwp.TransactiongetCurrent(Identifier id)


         
        Transaction trans;
        String cname;

        if( id == null ) {
            cname = System.getProperty(LWPProperties.XACTION);
            try {
                trans = (Transaction)Class.forName(cname).newInstance();
                trans.userID = id;
            }
            catch( Exception e ) {
                throw new ConfigurationException(e.getMessage());
            }
        }            
        synchronized( transactions ) {
            if( transactions.containsKey(id) ) {
                trans = (Transaction)transactions.get(id);
                return trans;
            }
            cname = System.getProperty(LWPProperties.XACTION);
            try {
                trans = (Transaction)Class.forName(cname).newInstance();
                trans.userID = id;
            }
            catch( Exception e ) {
                throw new ConfigurationException(e.getMessage());
            }
            transactions.put(id, trans);
        }
        return trans;
    
public final synchronized IdentifiergetIdentifier()

        return userID;
    
public final synchronized longgetTimestamp()

        return timestamp;
    
public final synchronized booleanisInProcess()

        return (timestamp != -1L);
    
final synchronized voidprepareCreate(BaseEntity ob)

        if( toCreate.contains(ob) ) {
            return;
        }
        toCreate.add(ob);
    
final synchronized voidprepareRemove(BaseEntity ob)

        if( toRemove.contains(ob) ) {
            return;
        }
        if( toCreate.contains(ob) ) {
            toCreate.remove(ob);
            return;
        }
        if( toStore.contains(ob) ) {
            toStore.remove(ob);
        }
        toRemove.add(ob);
    
final synchronized voidprepareStore(BaseEntity ob)

        if( toStore.contains(ob) || toCreate.contains(ob) ) {
            return;
        }
        if( toRemove.contains(ob) ) {
            return;
        }
        toStore.add(ob);
    
public abstract voidrollback()