001: package demo.bank.transaction.explicit;
002:
003: /**
004:
005: * Simple Transaction Service example, code taken and adapted
006:
007: * from http://www.wiley.com/compbooks/vogel/ejb/code.html
008:
009: */
010:
011: import org.omg.CORBA.*;
012:
013: import org.omg.CosTransactions.*;
014:
015: public class AccountImpl
016:
017: extends AccountPOA
018:
019: {
020:
021: private float balance;
022:
023: private float newBalance;
024:
025: private float amount;
026:
027: private boolean credit;
028:
029: private ORB orb;
030:
031: private String name;
032:
033: private Lock lock;
034:
035: public AccountImpl(ORB orb, String name, float deposit)
036:
037: {
038:
039: this .name = name;
040:
041: this .orb = orb;
042:
043: balance = deposit;
044:
045: lock = new Lock();
046:
047: }
048:
049: public float balance()
050:
051: {
052:
053: return balance;
054:
055: }
056:
057: public synchronized void credit(float amount, Control control)
058:
059: {
060:
061: try
062:
063: {
064:
065: // lock account
066:
067: lock.lock();
068:
069: // memorize current activitity
070:
071: this .amount = amount;
072:
073: credit = true;
074:
075: System.err.println("Account " + name
076: + "::credit: get coordinator");
077:
078: Coordinator coordinator = control.get_coordinator();
079:
080: // register resource
081:
082: System.out.println("Account " + name +
083:
084: "::credit: register resource (Account) with ITS");
085:
086: RecoveryCoordinator recCoordinator =
087:
088: coordinator.register_resource(_this ());
089:
090: newBalance = balance + amount;
091:
092: System.out.println(" credit $" + amount);
093:
094: System.out.println(" new balance is $" + newBalance);
095:
096: }
097:
098: catch (Exception ex)
099:
100: {
101:
102: System.err.println("Account " + name
103: + "::credit: exception: " + ex);
104:
105: }
106: ;
107:
108: }
109:
110: public synchronized void debit(float amount, Control control)
111:
112: throws InsufficientFunds
113:
114: {
115:
116: try
117:
118: {
119:
120: // lock account
121:
122: lock.lock();
123:
124: // memorize current activitity
125:
126: this .amount = amount;
127:
128: credit = false;
129:
130: // obtain current object
131:
132: System.err
133: .println("Accont::debit: resolve transaction current");
134:
135: System.err.println("Account " + name
136: + "::debit: get coordinator");
137:
138: Coordinator coordinator = control.get_coordinator();
139:
140: // register resource
141:
142: System.out.println("Account " + name +
143:
144: "::debit: register resource (Account) with ITS");
145:
146: RecoveryCoordinator recCoordinator =
147:
148: coordinator.register_resource(_this ());
149:
150: System.out.println("Account " + name
151: + "::debit: resource registered");
152:
153: if (amount > balance)
154:
155: {
156:
157: System.out.println("no sufficient funds");
158:
159: lock.unlock();
160:
161: System.out.println("Resource: " + name
162: + " account unlocked");
163:
164: throw new InsufficientFunds();
165:
166: }
167:
168: newBalance = balance - amount;
169:
170: System.out.println(" debit $" + amount);
171:
172: System.out.println(" new balance is $" + newBalance);
173:
174: }
175:
176: catch (Unavailable u) {
177:
178: System.err.println("Account " + name
179: + "::debit: exception: " + u);
180:
181: }
182:
183: catch (Inactive i) {
184:
185: System.err.println("Account " + name
186: + "::debit: exception: " + i);
187:
188: }
189:
190: catch (SystemException se) {
191:
192: System.err.println("Account " + name
193: + "::debit: exception: " + se);
194:
195: }
196:
197: }
198:
199: // implement methods of the Resource interface
200:
201: public Vote prepare()
202:
203: {
204:
205: System.out.println("Resource " + name + " : prepare()");
206:
207: if (balance == newBalance)
208:
209: return Vote.VoteReadOnly;
210:
211: return Vote.VoteCommit;
212:
213: }
214:
215: public void rollback()
216:
217: {
218:
219: // remove data from temporary storage
220:
221: System.out.println("Resource " + name + " : rollback()");
222:
223: System.out.println("Resource " + name +
224:
225: " : original balance restored: $" + balance);
226:
227: lock.unlock();
228:
229: System.out.println("Resource " + name + " account unlocked");
230:
231: }
232:
233: public void commit()
234:
235: {
236:
237: // move data to final storage
238:
239: System.out.println("Resource " + name + " : commit()");
240:
241: balance = newBalance;
242:
243: lock.unlock();
244:
245: System.out.println("Resource " + name + " account unlocked");
246:
247: }
248:
249: public void commit_one_phase()
250:
251: {
252:
253: // store data immediately at final destination
254:
255: System.out
256: .println("Resource " + name + " : commit_one_phase()");
257:
258: if (prepare() == Vote.VoteCommit)
259:
260: {
261:
262: commit();
263:
264: }
265:
266: }
267:
268: public void forget()
269:
270: {
271:
272: System.out.println("Resource " + name + " : forget()");
273:
274: }
275:
276: }
|