01: package demo.bank.transaction.explicit;
02:
03: import org.omg.CORBA.*;
04: import org.omg.CORBA.ORBPackage.*;
05: import org.omg.CosTransactions.*;
06: import org.omg.CosNaming.*;
07:
08: import java.io.*;
09:
10: public class BankImpl extends TheBankPOA {
11: private ORB orb;
12: private org.omg.PortableServer.POA poa;
13: private Control control = null;
14:
15: public BankImpl(ORB orb, org.omg.PortableServer.POA poa) {
16: this .orb = orb;
17: this .poa = poa;
18: }
19:
20: public Account open(String name, float initial_deposit) {
21: try {
22: AccountImpl acc = new AccountImpl(orb, name,
23: initial_deposit);
24: org.omg.CORBA.Object o = poa.servant_to_reference(acc);
25: return acc._this (orb);
26: } catch (Exception e) {
27: e.printStackTrace();
28: throw new org.omg.CORBA.UNKNOWN();
29: }
30: }
31:
32: public void transfer(Account source, Account destination,
33: float amount) throws InsufficientFunds {
34: try {
35: // obtain transaction factory object from naming service
36: NamingContextExt nc = NamingContextExtHelper.narrow(orb
37: .resolve_initial_references("NameService"));
38: NameComponent[] name = new NameComponent[1];
39: name[0] = new NameComponent("TransactionService", "service");
40:
41: TransactionFactory transactionFactory = TransactionFactoryHelper
42: .narrow(nc.resolve(name));
43:
44: // start a new transaction
45: System.err.println("begin transaction");
46:
47: // obtain control object
48: control = transactionFactory.create(20);
49:
50: source.debit(amount, control);
51:
52: System.err.println("debited");
53:
54: destination.credit(amount, control);
55:
56: System.err.println("credited");
57:
58: // commit the transaction
59: System.err.println("commit transaction");
60: control.get_terminator().commit(true);
61: System.err.println("transaction comitted");
62: } catch (InsufficientFunds isf) {
63: try {
64: control.get_terminator().rollback();
65: } catch (org.omg.CosTransactions.Unavailable nt) {
66: System.err.println("No transaction - give up: " + nt);
67: System.exit(1);
68: }
69: throw (isf);
70: } catch (InvalidName in) {
71: System.err.println("Initialization failure: " + in);
72: System.exit(1);
73: } catch (UserException ue) {
74: System.err
75: .println("transactional failure - give up: " + ue);
76: System.exit(1);
77: } catch (SystemException se) {
78: System.err
79: .println("system exception - rollback transaction: "
80: + se);
81: try {
82: control.get_terminator().rollback();
83: } catch (org.omg.CosTransactions.Unavailable nt) {
84: System.err.println("No transaction - give up: " + nt);
85: System.exit(1);
86: }
87: throw (se);
88: }
89: }
90:
91: }
|