KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > bank > transaction > explicit > BankImpl


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