FileDocCategorySizeDatePackage
Transaction.javaAPI DocExample5793Sat Feb 01 07:26:12 GMT 1997imaginary.persist

Transaction

public abstract class Transaction extends Object
The Transaction class is an abstract class implemented by different persistence packages for managing persistence operations on groups of objects.

Fields Summary
public static Properties
properties
The properties to use to initiate the transaction.
public static String
url
The URL used to initiate the transaction
private Vector
objects
Instance attributes
Constructors Summary
public Transaction()
Creates a new transaction object.


      
              
      
        super();
    
Methods Summary
public voidabort()
Aborts any persistence operations in process and allows persistent objects which have already done their operation to take it back.

        Persistent[] obs = getPersistents();

        for(int i=0; i<obs.length; i++) {
            try {
                if( obs[i].isSaving() ) {
                    obs[i].abort();
                }
            }
            catch( PersistenceException e ) {
                e.printStackTrace();
            }
        }
    
synchronized voidaddPersistent(Persistent p)
Ties a new Persistent instance to this transactioon.

param
p the persistent associated with this transaction

        if( !objects.contains(p) ) {
            objects.addElement(p);
        }
    
public voidcommit()
This method should be extended in data store specific transaction objects for sending a commit to the data store. Within the Transaction class, it lets all locked objects know that any data store commit was successful.

exception
imaginary.persist.PersistenceException An error occurred trying to commit.

        Persistent[] obs = getPersistents();

        for(int i=0; i<obs.length; i++) {
            if( obs[i].isSaving() ) {
                obs[i].commit();
            }
        }
        // commit done, prepare for new set of modifications
        objects = new Vector();
    
synchronized Persistent[]getPersistents()

return
an array of persistent objects associated with this transaction

        Persistent[] obs = new Persistent[objects.size()];

        objects.copyInto(obs);
        return obs;
    
public static imaginary.persist.TransactiongetTransaction()
Gets an instance of a Transaction subclass based on the URL set by the application. Currently, only database persistence is supported so all URL's are assumed to be JDBC URL's.

return
a new Transaction instance


                                             
          
        return new DatabaseTransaction(url, properties);
    
synchronized voidremovePersistent(Persistent p)
Removes the specified persistent from this transaction

param
p the persistent to be removed

        if( objects.contains(p) ) {
            objects.removeElement(p);
        }
    
public voidrestore(PersistentSet set, java.util.Hashtable data)
Calls for the restoration of the specified PersistentSet using a hashtable of query parameters.

parameter
set a set to be restored
param
data the query parameters for the restore
exception
imaginary.persist.PersistenceException An error occurred during restore.

        set.restore(this, data);
    
public voidrestore(Persistent p)
Calls for the restoration of a particular object using this Transaction.

param
p the object to restore using this Transaction

        p.restore(this);
    
public voidrestore(Persistent p, java.util.Hashtable data)
Calls for the restoration of the specified Persistent using a hashtable of query parameters.

param
p the Persistent to restore
param
data the query parameters
exception
imaginary.persist.PersistenceException An error occurred during restore.

        p.restore(this, data);
    
public voidsave()
This method goes through and triggers a save on all locked objects. If the saves are all successful, then a commit() is triggered. If any one of the saves fails, however, then an abort() is triggered.

exception
imaginary.persist.PersistenceException An error triggering an abort occurred.

        Persistent[] obs = getPersistents();
        
        for(int i=0; i<obs.length; i++) {
            try {
                if( obs[i].isModified() ) { // only save modified objects
                    obs[i].save();
                }
            }
            catch( PersistenceException e ) {
                abort(); // abort on error
                throw e;
            }
        }
        try {
            commit(); // commit on success
        }
        catch( PersistenceException e ) {
            abort(); // abort if the commit failed
            throw e;
        }