package ejava.transactions.demo;

import javax.ejb.SessionContext;
import javax.ejb.SessionBean;
import javax.ejb.SessionSynchronization;
import java.rmi.RemoteException;

public class SessionSynchronizedTellerEJB 
   implements SessionBean, SessionSynchronization {

   public void reset(AccountRemote account) throws AccountException {
      try {
         account.reset();
      }
      catch (Exception ex) {
         ctx_.setRollbackOnly();
	 throw new AccountException("reset failed");
      }
   }

   public void transfer(
      AccountRemote fromAccount, AccountRemote toAccount, float amount) 
      throws AccountException {

      try {
         toAccount.deposit(amount);
	 fromAccount.withdraw(amount);
      }
      catch (Exception ex) {
         ctx_.setRollbackOnly();
	 throw new AccountException("transfer failed");
      }
   }

   public void afterBegin() {}
   public void beforeCompletion() {}
   public void afterCompletion(boolean committed) { }
   
   public void ejbCreate() {}
   public void ejbRemove() {}
   public void ejbActivate() {}
   public void ejbPassivate() {}
   public void setSessionContext(SessionContext ctx) { ctx_ = ctx; }
   protected SessionContext ctx_;
}
