1 package demo.bank.concurrency; 2 3 4 5 12 13 14 15 import org.omg.CORBA.*; 16 17 import org.omg.CosTransactions.*; 18 19 import org.omg.CosConcurrencyControl.*; 20 21 import org.omg.CosConcurrencyControl.*; 22 23 24 25 public class AccountImpl 26 27 extends AccountPOA 28 29 { 30 31 private float balance; 32 33 private float newBalance; 34 35 private TransactionalLockSet lock_set; 36 37 private String name; 38 39 private int id; 40 41 private Coordinator current_transaction = null; 42 43 private boolean prepared = false; 44 45 46 47 public AccountImpl( TransactionalLockSet lock_set, String name, float deposit, int id ) 48 49 { 50 51 this.name = name; 52 53 this.lock_set = lock_set; 54 55 this.id = id; 56 57 balance = deposit; 58 59 newBalance = balance; 60 61 } 62 63 64 65 public synchronized float get_balance( Control control ){ 66 67 try { 68 69 Coordinator coord = control.get_coordinator(); 70 71 73 if( lock_set.try_lock( coord, lock_mode.read ) ) { 74 75 float r = newBalance; 76 77 lock_set.unlock( coord, lock_mode.read ); 78 79 return r; 80 81 } else { 82 83 85 return balance; 86 87 } 88 89 } catch ( Exception e ) { 90 91 e.printStackTrace(); 92 93 throw new org.omg.CORBA.INTERNAL (); 94 95 } 96 97 } 98 99 100 101 public synchronized void debit( float amount, Control control ) 102 103 throws InsufficientFunds 104 105 { 106 107 try 108 109 { 110 111 Coordinator coord = control.get_coordinator(); 112 113 114 115 117 while( ! lock_set.try_lock( coord, lock_mode.write ) ){ 118 119 try { 120 121 wait(); 122 123 } catch ( Exception e ){ 124 125 e.printStackTrace(); 126 127 } 128 129 } 130 131 132 133 135 if( amount > newBalance ) { 136 137 lock_set.unlock( coord, lock_mode.write ); 138 139 System.out.println("Resource: " + name + " account unlocked"); 140 141 throw new InsufficientFunds(); 142 143 } 144 145 newBalance -= amount; 146 147 if( current_transaction == null ){ 148 149 current_transaction = coord; 150 151 coord.register_resource( _this() ); 152 153 }; 154 155 } catch( Unavailable u ) { 156 157 System.err.println("Account " + name + "::debit: exception: " + u ); 158 159 } catch( LockNotHeld h ) { 160 161 System.err.println("Account " + name + "::debit: exception: " + h ); 162 163 } catch( Inactive i ) { 164 165 System.err.println("Account " + name + "::debit: exception: " + i ); 166 167 } catch( SystemException se ) { 168 169 System.err.println("Account " + name + "::debit: exception: " + se ); 170 171 }; 172 173 } 174 175 176 177 public synchronized void credit( float amount, Control control ) 178 179 { 180 181 try 182 183 { 184 185 Coordinator coord = control.get_coordinator(); 186 187 189 while( ! lock_set.try_lock( coord, lock_mode.write ) ){ 190 191 try { 192 193 wait(); 194 195 } catch ( Exception e ){ 196 197 e.printStackTrace(); 198 199 } 200 201 } 202 203 204 205 newBalance += amount; 206 207 if( current_transaction == null ){ 208 209 current_transaction = coord; 210 211 coord.register_resource( _this() ); 212 213 }; 214 215 } 216 217 catch( Unavailable u ) { 218 219 System.err.println("Account " + name + "::credit: exception: " + u ); 220 221 } 222 223 catch( Inactive i ) { 224 225 System.err.println("Account " + name + "::credit: exception: " + i ); 226 227 } 228 229 catch( SystemException se ) { 230 231 System.err.println("Account " + name + "::credit: exception: " + se ); 232 233 } 234 235 } 236 237 238 239 public TransactionalLockSet get_lock_set(){ 240 241 return lock_set; 242 243 }; 244 245 246 247 public java.lang.String owner(){ 248 249 return name; 250 251 }; 252 253 254 255 public int account_code(){ 256 257 return id; 258 259 }; 260 261 262 263 265 266 267 public synchronized Vote prepare() 268 269 { 270 271 if( balance == newBalance ) 272 273 return Vote.VoteReadOnly; 274 275 return Vote.VoteCommit; 276 277 } 278 279 280 281 public synchronized void rollback() { 282 283 newBalance = balance; 284 285 current_transaction = null; 286 287 notifyAll(); 288 289 } 290 291 292 293 public synchronized void commit() { 294 295 balance = newBalance; 296 297 current_transaction = null; 298 299 notifyAll(); 300 301 } 302 303 304 305 public synchronized void commit_one_phase() 306 307 { 308 309 if(prepare() == Vote.VoteCommit) { 310 311 commit(); 312 313 } 314 315 } 316 317 318 319 public synchronized void forget() 320 321 { 322 323 System.out.println("Resource " + name + " : forget()"); 324 325 } 326 327 } 328 329 | Popular Tags |