001: package demo.bank.transaction.implicit;
002:
003: import org.omg.CORBA.*;
004: import org.omg.CORBA.ORBPackage.*;
005: import org.omg.CosTransactions.*;
006: import org.omg.CosNaming.*;
007: import java.io.*;
008:
009: public class BankImpl extends TheBankPOA {
010: private ORB orb;
011: private org.omg.PortableServer.POA poa;
012:
013: private org.omg.CosTransactions.Current ts_current = null;
014:
015: public BankImpl(ORB orb, org.omg.PortableServer.POA poa) {
016: this .orb = orb;
017: this .poa = poa;
018:
019: try {
020: ts_current = org.omg.CosTransactions.CurrentHelper
021: .narrow(orb
022: .resolve_initial_references("TransactionCurrent"));
023: } catch (Exception e) {
024: e.printStackTrace();
025: }
026: }
027:
028: public Account open(String name, float initial_deposit) {
029: try {
030: AccountImpl acc = new AccountImpl(orb, name,
031: initial_deposit);
032: org.omg.CORBA.Object o = poa.servant_to_reference(acc);
033: return acc._this (orb);
034: } catch (Exception e) {
035: e.printStackTrace();
036: throw new org.omg.CORBA.UNKNOWN();
037: }
038: }
039:
040: public void transfer(Account source, Account destination,
041: float amount) throws InsufficientFunds, TransferFailed {
042: try {
043:
044: // start a new transaction
045: System.err.println("begin transaction");
046:
047: // obtain control object
048: //control = transactionFactory.create(20);
049: ts_current.set_timeout(20);
050: ts_current.begin();
051:
052: source.debit(amount);
053:
054: System.err.println("debited");
055:
056: destination.credit(amount);
057:
058: System.err.println("credited");
059:
060: // commit the transaction
061: System.err.println("commit transaction");
062: //control.get_terminator().commit( true );
063: ts_current.commit(true);
064: System.err.println("transaction comitted");
065: } catch (InsufficientFunds isf) {
066: System.err
067: .println("insufficient funds - rollback transaction: "
068: + isf);
069: try {
070: //control.get_terminator().rollback();
071: ts_current.rollback();
072: } catch (org.omg.CosTransactions.NoTransaction nt) {
073: System.err.println("No transaction - give up: " + nt);
074: System.exit(1);
075: }
076: throw (isf);
077: }
078:
079: catch (UserException ue) {
080: System.err
081: .println("user exception - rollback transaction: "
082: + ue);
083: try {
084: //control.get_terminator().rollback();
085: ts_current.rollback();
086: } catch (org.omg.CosTransactions.NoTransaction nt) {
087: System.err.println("No transaction - give up: " + nt);
088: System.exit(1);
089: }
090: throw new TransferFailed();
091: } catch (SystemException se) {
092: System.err
093: .println("system exception - rollback transaction: "
094: + se);
095: try {
096: //control.get_terminator().rollback();
097: ts_current.rollback();
098: } catch (org.omg.CosTransactions.NoTransaction nt) {
099: System.err.println("No transaction - give up: " + nt);
100: System.exit(1);
101: }
102: throw (se);
103: }
104: }
105: }
|