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

DatabaseTransaction

public class DatabaseTransaction extends Transaction
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.

Fields Summary
private Connection
connection
Instance attributes
private Properties
props
private String
url
Constructors Summary
public DatabaseTransaction(String u, Properties p)
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


      
                                                      
        
      
        super();
        url = u;
        props = p;
    
Methods Summary
public synchronized voidabort()
Upon a successful JDBC rollback, this will trigger the inherited abort() method.

see
imaginary.persist.Transaction#abort

        try {
            connection.rollback();
            super.abort();
        }
        catch( SQLException e ) {
        }
    
public synchronized voidcommit()
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

        try {
            connection.commit();
            super.commit();
        }
        catch( SQLException e ) {
            abort();
            throw new PersistenceException(e);
        }
    
public synchronized java.sql.ConnectiongetConnection()

return
the JDBC Connection for this DatabseTransaction

        return connection;
    
public synchronized voidrestore(Persistent p)
Restores a specific persistent object.

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

        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;
        }
    
public synchronized voidrestore(Persistent p, java.util.Hashtable data)
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.

        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;
        }
    
public synchronized voidrestore(PersistentSet set, java.util.Hashtable data)
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.

        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;
        }
    
public synchronized voidsave()
Saves all of the objects associated with this object using database persistence.

exception
imaginary.persist.PersistenceException An error occurred during save.

        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;
        }