Following this issue, here is the UserTransactionImpl.java. Attention to
begin/commit/rollback and incrementTransaction and decrementTransaction. 

- Someone needs to set the TrackedConnectionAssociator to
UserTransactionImpl sometime. When? 
- Should ConnectionTrackingCoordinator be a singleton?
- UserTransactionImpl could actively obtain a
ConnectionTrackingCoordinator.getInstance() as the default
TrackedConnectionAssociator.
- I didn't follow the "TODO this needs to migrate to using
TransactionContext."

Need some help on this.

--
hammett
http://jroller.com/page/hammett/
http://www.apache.org/~hammett/


/**
 * TODO this needs to migrate to using TransactionContext.
 * TODO this needs to notify the TrackedConnectionAssociator when a tx
starts.
 *
 * @version $Revision: 1.5 $ $Date: 2004/03/10 09:59:37 $
 */
public class UserTransactionImpl implements UserTransaction, Serializable
{
        ///
        /// Instance members
        ///
        
    private transient TransactionManager transactionManager;
    
    private transient TrackedConnectionAssociator
trackedConnectionAssociator;
    
    private transient TransactionContext oldContext;

        ///
        /// Constructors
        ///

    public UserTransactionImpl()
    {
    }

        // 
        // UserTransaction implementation
        // 

    public TransactionManager getTransactionManager()
    {
        return transactionManager;
    }

    public void setTransactionManager(TransactionManager transactionManager)
    {
        this.transactionManager = transactionManager;
    }

    public TrackedConnectionAssociator getTrackedConnectionAssociator()
    {
        return trackedConnectionAssociator;
    }

    public void setTrackedConnectionAssociator(TrackedConnectionAssociator
trackedConnectionAssociator)
    {
        this.trackedConnectionAssociator = trackedConnectionAssociator;
    }

    public void begin() throws NotSupportedException, SystemException
    {
        checkState();
        transactionManager.begin();
        
        // In the case of exception here shall we rollback the
        // brand new transaction?
        incrementTransaction();
    }

    public void commit()
        throws
            HeuristicMixedException,
            HeuristicRollbackException,
            IllegalStateException,
            RollbackException,
            SecurityException,
            SystemException
    {
        checkState();
        transactionManager.commit();
        
        decrementTransaction();
    }

    public int getStatus() throws SystemException
    {
        checkState();
        return transactionManager.getStatus();
    }

    public void rollback() throws IllegalStateException, SecurityException,
SystemException
    {
        checkState();
        transactionManager.rollback();
        
                decrementTransaction();
    }

    public void setRollbackOnly() throws IllegalStateException,
SystemException
    {
        checkState();
        transactionManager.setRollbackOnly();
    }

    public void setTransactionTimeout(int timeout) throws SystemException
    {
        checkState();
        transactionManager.setTransactionTimeout(timeout);
    }
    
    // 
    // Private implementation
    // 
    
        private void checkState()
        {
                //        if (transactionManager == null ||
trackedConnectionAssociator == null) {
                //            throw new
IllegalStateException("UserTransaction is disabled");
                //        }
        }

    private void incrementTransaction() throws SystemException
    {
                if (trackedConnectionAssociator == null)
                {
                        return;
                }
                
                try
                {
                        Transaction transaction =
transactionManager.getTransaction();
                        DefaultTransactionContext context = new
DefaultTransactionContext(transaction);
                        
                        // What to do with old context? 
                        // Will we support nested transactions?
                        oldContext =
trackedConnectionAssociator.setTransactionContext(context);
                }
                catch(Exception ex)
                {
                        throw new SystemException(ex.getMessage());
                }
    }
    
        private void decrementTransaction() throws SystemException
        {
                if (trackedConnectionAssociator == null)
                {
                        return;
                }
                
                try
                {
        
trackedConnectionAssociator.resetTransactionContext(oldContext);
                }
                catch(Exception ex)
                {
                        throw new SystemException(ex.getMessage());
                }
        }
}

Reply via email to