KickJava   Java API By Example, From Geeks To Geeks.

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


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