FileDocCategorySizeDatePackage
DatabaseTransaction.javaAPI DocExample6499Sat Feb 01 07:25:26 GMT 1997imaginary.persist

DatabaseTransaction.java

/**
 * The DatabaseTransaction class basically servers as a wrapper
 * around the JDBC Connection class.  It allows peers access to
 * the JDBC Connection in addition to triggering commits and rollbacks
 * as needed by the persistence package.
 */
package imaginary.persist;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.Properties;

public class DatabaseTransaction extends Transaction {
    /****************** Instance attributes *****************/
    private Connection connection = null;
    private Properties props      = null;
    private String     url        = null;

    /********************* Constructors *********************/
    /**
     * Creates a DatabaseTransaction using the specified URL and
     * properties.
     * @param holder the remote lock holder
     * @param url the JDBC URL for this DatabaseTransaction
     * @param p the properties, generally containing user name and password
     * @exception imaginary.persist.PersistenceException An error occurred
     * finding a JDBC driver for the URL
     */
    public DatabaseTransaction(String u, Properties p)
    throws PersistenceException {
        super();
        url = u;
        props = p;
    }

    /**************** Attribute Accessors *******************/ 
    /**
     * @return the JDBC Connection for this DatabseTransaction
     */
    public synchronized Connection getConnection() {
        return connection;
    }

    /************** Persistence operations *****************/
    /**
     * Upon a successful JDBC rollback, this will trigger the
     * inherited abort() method.
     * @see imaginary.persist.Transaction#abort
     */
    public synchronized void abort() {
        try {
            connection.rollback();
            super.abort();
        }
        catch( SQLException e ) {
        }
    }

    /**
     * Upon a successful JDBC commit, this will call the inherited
     * commit in order to let all committed objects know the commit
     * was successful.
     * @exception imaginary.persist.PersistenceException An error occurred
     * attempting to commit the transaction
     */
    public synchronized void commit() throws PersistenceException {
        try {
            connection.commit();
            super.commit();
        }
        catch( SQLException e ) {
            abort();
            throw new PersistenceException(e);
        }
    }

    /**
     * Restores a specific persistent object.
     * @param p the Persistent to restore
     * @exception imaginary.persist.PersistenceException An error occurred
     * during restore.
     */
    public synchronized void restore(Persistent p)
    throws PersistenceException {
        if( connection == null ) {
            try {
                connection = DriverManager.getConnection(url, props);
                // comment out setAutoCommit() for mSQL
                // which does not handle transaction logic
                //              connection.setAutoCommit(false);
            }
            catch( SQLException e ) {
                throw new PersistenceException(e);
            }
        }
        try {
            super.restore(p);
        }
        finally {
            try {
                connection.close();
            }
            catch( SQLException e ) {
                e.printStackTrace();
            }
            connection = null;
        }
    }

    /**
     * Restores a persistent with the specified data.
     * @param p the persistent object to restore
     * @param data the data to use for restoring the object
     * @exception imaginary.persist.PersistenceException An error occurred
     * during restore.
     */
    public synchronized void restore(Persistent p, Hashtable data)
    throws PersistenceException {
        if( connection == null ) {
            try {
                connection = DriverManager.getConnection(url, props);
            }
            catch( SQLException e ) {
                throw new PersistenceException(e);
            }
        }
        try {
            super.restore(p, data);
        }
        finally {
            try {
                connection.close();
            }
            catch( SQLException e ) {
                e.printStackTrace();
            }
            connection = null;
        }
    }

    /**
     * Restores a PersistentSet based on the specified query criteria.
     * @param set the set to be restored
     * @param data the query parameter to use for the restore
     * @exception imaginary.persist.PersistenceException An error occurred
     * during restore.
     */
    public synchronized void restore(PersistentSet set, Hashtable data)
    throws PersistenceException {
        if( connection == null ) {
            try {
                connection = DriverManager.getConnection(url, props);
                //              connection.setAutoCommit(false);
            }
            catch( SQLException e ) {
                throw new PersistenceException(e);
            }
        }
        try {
            super.restore(set, data);
        }
        finally {
            try {
                connection.close();
            }
            catch( SQLException e ) {
                e.printStackTrace();
            }
            connection = null;
        }
    }

    /**
     * Saves all of the objects associated with this object using
     * database persistence.
     * @exception imaginary.persist.PersistenceException An error occurred
     * during save.
     */
    public synchronized void save() throws PersistenceException {
        if( connection == null ) {
            try {
                connection = DriverManager.getConnection(url, props);
                //              connection.setAutoCommit(false);
            }
            catch( SQLException e ) {
                throw new PersistenceException(e);
            }
        }
        try {
            super.save();
        }
        finally {
            try {
                if( connection != null ) {
                    connection.close();
                }
            }
            catch( SQLException e ) {
                e.printStackTrace();
            }
            connection = null;
        }
    }
}