KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > bank > concurrency > BankImpl


1 package demo.bank.concurrency;
2
3 import org.omg.CORBA.*;
4 import org.omg.CORBA.ORBPackage.*;
5 import org.omg.CosTransactions.*;
6 import org.omg.CosConcurrencyControl.*;
7 import org.omg.CosNaming.*;
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 TransactionFactory transactionFactory;
16     private LockSetFactory lockSetFactory;
17
18     public BankImpl( ORB orb, org.omg.PortableServer.POA JavaDoc poa )
19     {
20     this.orb = orb;
21     this.poa = poa;
22         try
23         {
24             // obtain transaction factory object from naming service
25
NamingContextExt nc =
26                 NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));
27             NameComponent [] name = new NameComponent[1];
28             name[0] =
29                 new NameComponent( "TransactionService", "service");
30             transactionFactory =
31                 TransactionFactoryHelper.narrow( nc.resolve(name));
32
33             // obtain LockSet factory object from naming service
34
name[0] =
35                 new NameComponent( "ConcurrencyControlService",
36                                    "service");
37             lockSetFactory =
38                 LockSetFactoryHelper.narrow( nc.resolve(name) );
39         }
40         catch( Exception JavaDoc n )
41         {
42             n.printStackTrace();
43             System.exit(1);
44         }
45     }
46
47     public Account open(String JavaDoc name, float initial_deposit)
48     {
49     try
50     {
51             TransactionalLockSet lock_set =
52                 lockSetFactory.create_transactional();
53         AccountImpl acc =
54                   new AccountImpl( lock_set, name,
55                         initial_deposit, 0);
56         org.omg.CORBA.Object JavaDoc o = poa.servant_to_reference(acc);
57         return acc._this(orb);
58     }
59     catch( Exception JavaDoc e )
60     {
61             e.printStackTrace();
62         throw new org.omg.CORBA.UNKNOWN JavaDoc();
63     }
64     }
65
66     public void transfer( Account source, Account destination, float amount )
67     throws InsufficientFunds
68     {
69         Control control = null;
70     try
71     {
72         // start a new transaction
73
System.err.println("begin transaction");
74
75         // obtain control object
76
control = transactionFactory.create(20);
77             Coordinator coord = control.get_coordinator();
78
79             // Lock in upgrade mode for prevent dead lock
80
TransactionalLockSet lock_set = source.get_lock_set();
81             lock_set.lock( coord, lock_mode.upgrade );
82             lock_set = destination.get_lock_set();
83             lock_set.lock( coord, lock_mode.upgrade );
84
85         source.debit( amount, control );
86
87         System.err.println("debited");
88
89         destination.credit( amount, control );
90
91         System.err.println("credited");
92
93         // commit the transaction
94
System.err.println("commit transaction");
95         control.get_terminator().commit( true );
96         System.err.println("transaction comitted");
97     }
98     catch( InsufficientFunds isf )
99     {
100         try
101         {
102         control.get_terminator().rollback();
103         }
104         catch( org.omg.CosTransactions.Unavailable nt )
105         {
106         System.err.println("No transaction - give up: " + nt );
107         System.exit( 1 );
108         }
109         throw( isf );
110     }
111     catch( UserException ue )
112     {
113         System.err.println("transactional failure - give up: " + ue );
114         System.exit( 1 );
115     }
116     catch( SystemException se )
117     {
118         System.err.println("system exception - rollback transaction: " + se );
119         try
120         {
121         control.get_terminator().rollback();
122         }
123         catch( org.omg.CosTransactions.Unavailable nt )
124         {
125         System.err.println("No transaction - give up: " + nt );
126         System.exit( 1 );
127         }
128         throw( se );
129     }
130     }
131
132
133 }
134
135
136
137
Popular Tags