Make your own free website on Tripod.com

Java Data Objects (JDO)

Creating a Simple Banking Application

Teller : Developing Persistence-Aware Applications

Gopalan Suresh Raj

Note
To work with any of these samples, you will need the following:
.........................................JDK 1.2 or higher (I use JDK 1.3.1)
.........................................PE:JTM - The Productivity Environment for JavaTM (from HYWY Software Corporation)

Persistence-aware applications are applications that make use of persistence-capable objects. In theory, programmers responsible for developing persistence-aware applications can be different from programmers responsible for constructing the persistence-capable classes. The former deal with business logic while the latter deal with data modeling. In practice, depending on the situation, these two groups of programmers could be one and the same.

The PE:J JDO runtime allows application programmers to manipulate persistence-capable objects the same way they manipulate common Java objects. In addition, functions are provided to enable transactional persistence (storage of objects in a data-store). Application programmers can have a consistent Java-centric view of persistent data without the need to learn a detailed data-store-specific query language (SQL, etc.)  or many dialect variations of the query language of different database vendors. As far as the application programmers are concerned, the actual interactions between the data-store and the data are transparent. They do not need to know how the actual interactions are performed behind the scenes, or even what database system is being used.

The java interfaces of JDO are defined in the package javax.jdo. Persistence-aware applications make use of these interface to access persistence-capable objects. Furthermore, the PE:J JDO Runtime also provides convenience classes like the JDOAdapter to pool Persistence Manager Factory instances, and Oid to generate unique UUIDs. These are found in the 
com.hywy.pej.adapter.JDOAdapter class and the com.hywy.pej.persistence.oidserver.Oid class respectively.  
 

Note: This article assumes that the reader has already created the simple bank project using PE:J and JDO.

The following demonstrates how to develop a Persistence-Aware Application that makes use of the JDO Enhanced Persistence-Capable classes and the JDO API.

All persistence-aware applications will need to perform certain common steps. They are:

  1. Obtain a Configured Persistence Manager Factory (PMF) from the JDOAdapter

  2. Use the configured PMF to get a Persistence Manager

  3. Use the Persistence Manager (PM) to obtain a Transaction object 

  4. Inserting, selecting, updating or deleting a persistence-capable object

  5. Commiting or rolling back a transaction

  6. Closing the Persistence Manager

  7. Release the Persistence Manager Factory

1. Obtain a Configured PersistenceManagerFactory (PMF) from the JDOAdapter

The Persistence Manager Factory is responsible for creating persistence managers according to pre-configured options and closing them. All options of the PE:J JDO runtime are stored in the Persistence Manager Factory. In addition, pooling of persistence managers is also implemented in the PE:J JDO runtime.

Lines 83 to 93 in the code listing of the Teller class below, provide an example of how to obtain a pre-configured PMF. (It has been reproduced here for your convenience)

    JDOAdapter adapter_ = new JDOAdapter(Teller.PROJECT_NAME_,
                              Teller.INITIAL_POOL_CAPACITY_,
                              Teller.MAXIMUM_POOL_CAPACITY_,
                              Teller.POOL_CAPACITY_INCREMENT_);

    try{
      pmFactory_ = this.getAdapter().obtainPMF(Teller.PROJECT_NAME_,
                                               Teller.DATASTORE_NAME_);
    }catch(Exception exception){
      exception.printStackTrace();
    }

2. Use the configured PMF to get a PersistenceManager

A persistence-aware application must create a persistence manager. The Persistence Manager is a high-level programming interface for managing the states of persistence objects, transactions and queries. An instance of the persistence manager can be obtained from the persistence manager factory. The persistence manager obtained will use the options and connections defined by the persistence manager factory.

Line 125 in the code listing of the Teller class below, provides an example of how to obtain a persistence manager from the PMF. (It has been reproduced here for your convenience)

      // Obtain a Persistence Manager from the Factory Object
      PersistenceManager pManager = pmFactory_.getPersistenceManager();

3. Use the Persistence Manager (PM) to obtain a Transaction object 

Usually several operations on persistence-capable objects are grouped as a transaction. A transaction object can be accessed by the method currentTransaction(). A transaction will be started by calling the begin() method.

Line 128 in the code listing of the Teller class below, provides an example of how to obtain a transaction object from the PM, and Line 131 provides an example of how to begin a transaction. (It has been reproduced here for your convenience)

      // Retrieve a Transaction object from the Persistence Manager
      Transaction transaction = pManager.currentTransaction();

      // Begin a Transaction
      transaction.begin();

4. Inserting, selecting, updating or deleting a persistence-capable object

Persistence-capable objects are obtained from the persistence manager. The values of their fields can be modified thereafter. Once the objects are loaded, they are saved inside the object cache of the persistence manager. Objects are loaded on demand. Application programmers do not usually need to manage the object cache explicitly. Any unused/unreferenced objects are automatically garbage-collected by the Java runtime.

Line 134 in the code listing of the Teller class below, provides an example of how to insert a new object. (It has been reproduced here for your convenience)

      // Invoke Make Persistent on the object
      pManager.makePersistent(account);

Line 196 in the code listing of the Teller class below, provides an example of how to delete a persistence-capable object. (It has been reproduced here for your convenience)

        // Invoke Delete Persistent on the object
        pManager.deletePersistent(account);

5. Commiting or rolling back a transaction

When a transaction commits, all the operations on the persistence objects will be flushed to the data-store. Updates, insertions and deletions of data in the data-store will be performed if necessary. The data-store operations can also be aborted by rolling back a transaction. After committing the transaction, the persistence manager can be re-used and another new transaction can be started. At any one time, a persistence manager can deal with only one transaction.
 

Lines 136 to 148 in the code listing of the Teller class below, provide an example of how to commit or rollback a transaction. (It has been reproduced here for your convenience)

      // Commit the transaction
      transaction.commit();
    }catch(Exception exception) {

      // Something bad happened.
      // If the transaction object was obtained
      if(transaction != null) {
        // Rollback the transaction
        transaction.rollback();
      }

      exception.printStackTrace();
    }

6. Closing the Persistence Manager

Close the persistence manager by calling the close() method. The closed persistence manager is collected by the factory into its pool of available persistence managers for future use.

Line 153 in the code listing of the Teller class below, provides an example of how to close a persistence manager. (It has been reproduced here for your convenience)

       // Close the Persistence Manager
       if(pManager != null) {
         pManager.close();
       }

7. Release the Persistence Manager Factory

Release the persistence manager factory by calling the close() method on the JDOAdapter instance. The released persistence manager factory is collected by the JDOAdapter into its pool of available persistence manager factories for future use.

Line 75 in the code listing of the Teller class below, provides an example of how to close a persistence manager. (It has been reproduced here for your convenience)

    this.adapter_.close(datastoreName, pmFactory);

The class diagram of the Teller class below, lists the attributes, and public methods of the Teller Application.

The code listing of the Teller class below, demonstrates how to develop a Persistence-Aware Application that makes use of the JDO Enhanced Persistence-Capable classes.

 Place the following file in the Enhanced Folder that you identified in PE:J

com/hywy/samples/bank/Teller.java
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
446:
447:
448:
449:
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
470:
471:
472:
473:
474:
475:
476:
477:
478:
479:
480:
481:
482:
483:
484:
485:
486:
487:
488:
489:
490:
491:
492:
493:
494:
495:
496:
497:
498:
499:
500:
501:
502:
503:
504:
505:
506:
507:
508:
509:
510:
511:
512:
513:
514:
/**
  * author: Gopalan Suresh Raj
  * Copyright (c), 2002. All Rights Reserved.
  * URL: http://gsraj.tripod.com/
  * email: gopalan@gmx.net
  *
  *
  * To Run the Application
  * java -classpath
  * .;C:\PEJ\lib\jdo.jar;C:\PEJ\lib\j2ee.jar;C:\PEJ\lib\pej.jar;C:\PEJ\lib\tools.jar
  * com.hywy.samples.bank.Teller
  */


package com.hywy.samples.bank;

import com.hywy.pej.adapter.JDOAdapter;
import com.hywy.pej.persistence.oidserver.Oid;

// Standard JDO Class Library Imports
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;
import javax.jdo.Extent;
import javax.jdo.spi.PersistenceCapable;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.text.SimpleDateFormat;

import com.hywy.samples.bank.Account;
import com.hywy.samples.bank.Checking;
import com.hywy.samples.bank.Savings;

public class Teller {
  /** Project Name that was configured using the Console */
  public static final String PROJECT_NAME_ = "bank";

  /** Datastore Name that was configured using the Console */
  public static final String DATASTORE_NAME_ = "bank_datastore";

  /** PMF Object pool initial capacity */
  public static final int INITIAL_POOL_CAPACITY_ = 1;

  /** PMF Object pool maximum capacity */
  public static final int MAXIMUM_POOL_CAPACITY_ = 10;

  /** PMF Object pool capacity increment */
  public static final int POOL_CAPACITY_INCREMENT_ = 1;

  /** PersistenceManagerFactory Object */
  PersistenceManagerFactory pmFactory_ = null;

  /** A JDOAdapter instance */
  JDOAdapter adapter_;

  /**
   * Retrieve the JDOAdapter that holds the PersistenceManagerFactory Object Pool
   *
   * @return a JDOAdapter instance that holds the PMF object pool
   */

  public JDOAdapter getAdapter() {
    return this.adapter_;
  }

  /**
   * Releases the configured PMF and returns it back to the Object Pool,
   * so that it can be reused by some other process
   *
   * @param datastoreName - string denoting the name of the pre-configured datastore
   * @param pmFactory - PersistenceManagerFactory object that can be released back to the pool
   */

  public void releasePMF(String datastoreName, PersistenceManagerFactory pmFactory) {
    this.adapter_.close(datastoreName, pmFactory);
  }

  /**
   * Create  a new JDOAdapter
   *
   */

  public Teller() {
    adapter_ = new JDOAdapter(Teller.PROJECT_NAME_,
                              Teller.INITIAL_POOL_CAPACITY_,
                              Teller.MAXIMUM_POOL_CAPACITY_,
                              Teller.POOL_CAPACITY_INCREMENT_);

    try{
      pmFactory_ = this.getAdapter().obtainPMF(Teller.PROJECT_NAME_,
                                               Teller.DATASTORE_NAME_);
    }catch(Exception exception){
      exception.printStackTrace();
    }
  }

  /**
   * Create Checking or Savings Account
   */

  public void createAccount(String customerNames,
                            int startingBalance,
                            boolean isCheckingAccount)
    throws Exception {

    Account account = null;

    if(isCheckingAccount == true) {
      account = new Checking();
    }
    else {
      account = new Savings();
      ((Savings)account).setInterestRate_(2);
    }
    account.create(customerNames, startingBalance);

    ////////////////////////////////////
    // Perform  JDO operations
    ////////////////////////////////////

    PersistenceManager pManager = null;
    Transaction transaction = null;

    try {

      // Obtain a Persistence Manager from the Factory Object
      pManager = pmFactory_.getPersistenceManager();

      // Retrieve a Transaction object from the Persistence Manager
      transaction = pManager.currentTransaction();

      // Begin a Transaction
      transaction.begin();

      // Invoke Make Persistent on the object
      pManager.makePersistent(account);

      // Commit the transaction
      transaction.commit();
    }catch(Exception exception) {

      // Something bad happened.
      // If the transaction object was obtained
      if(transaction != null) {
        // Rollback the transaction
        transaction.rollback();
      }

      exception.printStackTrace();
    }
     finally {

       // Close the Persistence Manager
       if(pManager != null) {
         pManager.close();
       }

       // Release the Persistence Manager Factory
       if(pmFactory_ != null) {
         releasePMF(DATASTORE_NAME_, pmFactory_);
       }
     }

  }

  /**
   * Delete and close the account
   */

  public void deleteAccount(long accountNumber)
    throws Exception {

    Account account = null;


    ////////////////////////////////////
    // Perform  JDO operations
    ////////////////////////////////////

    PersistenceManager pManager = null;
    Transaction transaction = null;

    try {

      // Obtain a Persistence Manager from the Factory Object
      pManager = pmFactory_.getPersistenceManager();

      // Retrieve a Transaction object from the Persistence Manager
      transaction = pManager.currentTransaction();

      // Begin a Transaction
      transaction.begin();

      // Obtain the related PC object
      account = (Account)pManager.getObjectById(new Oid(accountNumber), true);

      if(account != null) {
        // Invoke Delete Persistent on the object
        pManager.deletePersistent(account);
      }

      // Commit the transaction
      transaction.commit();
    }catch(Exception exception) {

      // Something bad happened.
      // If the transaction object was obtained
      if(transaction != null) {
        // Rollback the transaction
        transaction.rollback();
      }

      exception.printStackTrace();
    }
     finally {

       // Close the Persistence Manager
       if(pManager != null) {
         pManager.close();
       }

       // Release the Persistence Manager Factory
       if(pmFactory_ != null) {
         releasePMF(DATASTORE_NAME_, pmFactory_);
       }
     }
  }

  /**
   * Transfer Money
   */

  public boolean transferMoney(long fromAccountNumber,
                               long toAccountNumber,
                               int amount) {
    boolean result = false;

    if(amount < 0) {
      return false;
    }

    ////////////////////////////////////
    // Perform JDO operations
    ////////////////////////////////////

    PersistenceManager pManager = null;
    Transaction transaction = null;

    try {

      // Obtain a Persistence Manager from the Factory Object
      pManager = pmFactory_.getPersistenceManager();

      // Retrieve a Transaction object from the Persistence Manager
      transaction = pManager.currentTransaction();

      // Begin a Transaction
      transaction.begin();

      // Obtain the related PC object
      Account fromAccount = (Account)pManager.getObjectById(new Oid(fromAccountNumber), true);
      Account toAccount = (Account)pManager.getObjectById(new Oid(toAccountNumber), true);

      if (fromAccount == null)
      {
        throw new Exception("Account Not Found: Account "
          + fromAccountNumber + " can not be found "
          + "and funds cannot be transfered from it.");
      }

      if (toAccount == null)
      {
        throw new Exception("Account Not Found: Account "
          + toAccountNumber + " can not be found "
          + "and funds cannot be transfered to it.");
      }

      int balance = fromAccount.getBalance_();
      if( balance < amount) {
        throw new Exception("Insufficient Funds: Account "+
          fromAccountNumber+
          " has a balance of just $"+
          balance/100+
          ". Cannot transfer $"+
          amount/100);
      }

      fromAccount.debit(amount);
      toAccount.credit(amount);

      // Invoke Make Persistent on the object
      pManager.makePersistent(fromAccount);
      pManager.makePersistent(toAccount);

      // Commit the transaction
      transaction.commit();
      result = true;

    } catch(Exception exception) {

      // Something bad happened.
      // If the transaction object was obtained
      if(transaction != null) {
        // Rollback the transaction
        transaction.rollback();
      }
      result = false;
      exception.printStackTrace();
    } finally {

      // Close the Persistence Manager
      if(pManager != null) {
        pManager.close();
      }

      // Release the Persistence Manager Factory
      if(pmFactory_ != null) {
        releasePMF(DATASTORE_NAME_, pmFactory_);
      }
    }
    return result;
  }

  /**
   * List out all Checking accounts found
   */

  public ArrayList listAllCheckingAccounts() {

    ArrayList list = new ArrayList();

    ////////////////////////////////////
    // Perform JDO operations
    ////////////////////////////////////

    PersistenceManager pManager = null;
    Transaction transaction = null;

    try {

      // Obtain a Persistence Manager from the Factory Object
      pManager = pmFactory_.getPersistenceManager();


      Class pcClass = Class.forName("com.hywy.samples.bank.Checking");
      Extent extent = pManager.getExtent(pcClass, false) ;

      Iterator extentIterator = extent.iterator();
      while ( extentIterator.hasNext()) {

        PersistenceCapable checkingPC = (PersistenceCapable)extentIterator.next();


        Checking checking = (Checking)checkingPC;

        Oid currentCheckingOID = null;

        currentCheckingOID = (Oid)pManager.getTransactionalObjectId(checkingPC);
        System.out.println("_________________________________________________");
        System.out.println("Checking Account for :"+currentCheckingOID.toLong());
        System.out.println("Details are :"+checkingPC.toString());
        System.out.println("_________________________________________________");
        list.add(currentCheckingOID);


      }
    } catch(Exception exception) {

      // Something bad happened.
      // If the transaction object was obtained
      if(transaction != null) {
        // Rollback the transaction
        transaction.rollback();
      }

      exception.printStackTrace();
    } finally {

      // Close the Persistence Manager
      if(pManager != null) {
        pManager.close();
      }

      // Release the Persistence Manager Factory
      if(pmFactory_ != null) {
        releasePMF(DATASTORE_NAME_, pmFactory_);
      }
    }
    return list;
  }

  /**
   * List out all Savings accounts found
   */

  public ArrayList listAllSavingsAccounts() {

    ArrayList list = new ArrayList();

    ////////////////////////////////////
    // Perform JDO operations
    ////////////////////////////////////

    PersistenceManager pManager = null;
    Transaction transaction = null;

    try {

      // Obtain a Persistence Manager from the Factory Object
      pManager = pmFactory_.getPersistenceManager();


      Class pcClass = Class.forName("com.hywy.samples.bank.Savings");
      Extent extent = pManager.getExtent(pcClass, false) ;

      Iterator extentIterator = extent.iterator();
      while ( extentIterator.hasNext()) {

        PersistenceCapable savingsPC = (PersistenceCapable)extentIterator.next();


        Savings savings = (Savings)savingsPC;

        Oid currentSavingsOID = null;

        currentSavingsOID = (Oid)pManager.getTransactionalObjectId(savingsPC);
        System.out.println("_________________________________________________");
        System.out.println("Savings Account for :"+currentSavingsOID.toLong());
        System.out.println("Details are :"+savingsPC.toString());
        System.out.println("_________________________________________________");
        list.add(currentSavingsOID);

      }
    } catch(Exception exception) {

      // Something bad happened.
      // If the transaction object was obtained
      if(transaction != null) {
        // Rollback the transaction
        transaction.rollback();
      }

      exception.printStackTrace();
    } finally {

      // Close the Persistence Manager
      if(pManager != null) {
        pManager.close();
      }

      // Release the Persistence Manager Factory
      if(pmFactory_ != null) {
        releasePMF(DATASTORE_NAME_, pmFactory_);
      }
    }
    return list;
  }

  public static void main(String [] args) {
    Teller teller = new Teller();

    // try creating new accounts
    try {
      teller.createAccount("Gopalan Suresh Raj", 10000000, true);
    }
    catch (Exception exception) {
      exception.printStackTrace();
    }
    try {
      teller.createAccount("Athul Suresh Raj", 20000000, true);
    }
    catch (Exception exception) {
      exception.printStackTrace();
    }

    try {
      teller.createAccount("Gopalan Suresh Raj", 910000000, false);
    }
    catch (Exception exception) {
      exception.printStackTrace();
    }

    try {
      teller.createAccount("Athul Suresh Raj", 2000000000, false);
    }
    catch (Exception exception) {
      exception.printStackTrace();
    }

    List checkingAccountList = teller.listAllCheckingAccounts();
    List savingsAccountList = teller.listAllSavingsAccounts();

    // Try deleting the first 2 accounts
    if((checkingAccountList.size() > 0)&&(savingsAccountList.size() > 0)) {
      try {
        Oid checkingOid = (Oid)checkingAccountList.get(0);
        Oid savingsOid = (Oid)savingsAccountList.get(0);
        teller.deleteAccount(checkingOid.toLong());
        teller.deleteAccount(savingsOid.toLong());
        checkingAccountList.remove(checkingOid);
        savingsAccountList.remove(savingsOid);
      }
      catch (Exception exception) {
        exception.printStackTrace();
      }

    }

    // Try transfering money from one account to the other
    if((checkingAccountList.size() > 0)&&(savingsAccountList.size() > 0)) {
      try {
        Oid fromOid = (Oid)checkingAccountList.get(0);
        Oid toOid = (Oid)savingsAccountList.get(0);
        teller.transferMoney(fromOid.toLong(),
                             toOid.toLong(),
                             100);
      }
      catch (Exception exception) {
        exception.printStackTrace();
      }

    }

  }

}

JDO Extent

An extent is a JDO construct that is leveraged both for performance and as a basis for JDO object queries. An extent represents all of the instances of a class or a class and its subclasses. Extents could be thought of as collections, but with some room for optimization. A JDO implementation can implement an extent such that only batches of returned objects are actually immediately available in memory. In this case, the remaining objects become populated by the JDO implementation one by one, or in groups, as the iterator encounters objects that are not available in memory. The ability to lazily load objects is an improvement over collections, which can require the JDO provider to load thousands of objects to represent a table.

Lines 403 to 427 in the code listing of the Teller class above, provide an example of how to use a JDO Extent. (It has been reproduced here for your convenience)

      // Obtain a Persistence Manager from the Factory Object
      pManager = pmFactory_.getPersistenceManager();


      Class pcClass = Class.forName("com.hywy.samples.bank.Savings");
      Extent extent = pManager.getExtent(pcClass, false) ;

      Iterator extentIterator = extent.iterator();
      while ( extentIterator.hasNext()) {

        PersistenceCapable savingsPC = (PersistenceCapable)extentIterator.next();


        Savings savings = (Savings)savingsPC;

        Oid currentSavingsOID = null;

        currentSavingsOID = (Oid)pManager.getTransactionalObjectId(savingsPC);
        System.out.println("_________________________________________________");
        System.out.println("Savings Account for :"+currentSavingsOID.toLong());
        System.out.println("Details are :"+savingsPC.toString());
        System.out.println("_________________________________________________");
        list.add(currentSavingsOID);

      }

Note that a JDO extent is retrieved through the persistence manager. The parameters are as follows:

Finally, the extent should be closed. This closes all iterators over an extent. After this point, all iterators will return false from their hasNext method.

Upon retrieval of the extent, we get an iterator (just like a collection) and iterate through the contents. The contents of the collection that are printed are all of the Savings Accounts that were created.
 

The Transfer Money Operation

The Transfer Money Object Interaction Diagram is as shown below:

The Transfer Money logic is as follows:

  1. The JDO Client (Teller.java) obtains a configured PersistenceManagerFactory (PMF) from the JDOAdapter.

  2. The JDO Client (Teller.java)  uses the configured PMF to get a PersistenceManager.

  3. The JDO Client (Teller.java)  uses the Persistence Manager to obtain a Transaction object.

  4. The JDO Client (Teller.java) begins a new Transaction using the Transaction object.

  5. The JDO Client (Teller.java) invokes the credit method on the Checking object.

  6. The JDO Client (Teller.java) then invokes the debit method on the Savings object.

  7. The JDO Client (Teller.java) then commits the transaction by invoking the method in the Transaction object.

  8. The JDO Client (Teller.java) then closes the Persistence Manager.

  9. The JDO Client (Teller.java) then closes the JDOAdapter.

Create a file called Teller.java and place it in the %PEJ_HOME%\projects\bank\enhanced folder. Compile it once again from the PE:J Console. If in doubt, please refer to the Quick Start Guide to see how to do this.

Run the Sample Application.

Command Prompt 
C:\run\projects\bank\enhanced>
C:\run\projects\bank\enhanced>
java -classpath .;C:\PEJ\lib\jdo.jar;C:\PEJ\lib\j2ee.jar;C:\PEJ\lib\pej.jar;C:\PEJ\lib\tools.jar com.hywy.samples.bank.Teller
_________________________________________________
Checking Account for :549810130944
Details are : Account: accountType_= 100 customerNames_= Gopalan Suresh Raj balance_= 10000000 openedOn_= Thu May 23 00:00:00 EDT 2002
_________________________________________________
_________________________________________________
Checking Account for :274932224000
Details are : Account: accountType_= 100 customerNames_= Gopalan Suresh Raj balance_= 100 openedOn_= Thu May 23 00:00:00 EDT 2002
_________________________________________________
_________________________________________________
Checking Account for :412371177472
Details are : Account: accountType_= 100 customerNames_= Athul Suresh Raj balance_= 20000000 openedOn_= Thu May 23 00:00:00 EDT 2002
_________________________________________________
_________________________________________________
Checking Account for :687249084416
Details are : Account: accountType_= 100 customerNames_= Athul Suresh Raj balance_= 20000000 openedOn_= Thu May 23 00:00:00 EDT 2002
_________________________________________________
_________________________________________________
Savings Account for :549793353728
Details are : Account: accountType_= 200 customerNames_= Gopalan Suresh Raj balance_= 910000000 openedOn_= Thu May 23 00:00:00 EDT 2002 Savings: interestRate_= 2
_________________________________________________
_________________________________________________
Savings Account for :274915446784
Details are : Account: accountType_= 200 customerNames_= Gopalan Suresh Raj balance_= 100 openedOn_= Thu May 23 00:00:00 EDT 2002 Savings: interestRate_= 2
_________________________________________________
_________________________________________________
Savings Account for :412354400256
Details are : Account: accountType_= 200 customerNames_= Athul Suresh Raj balance_= 2000000000 openedOn_= Thu May 23 00:00:00 EDT 2002 Savings: interestRate_= 2
_________________________________________________
_________________________________________________
Savings Account for :687232307200
Details are : Account: accountType_= 200 customerNames_= Athul Suresh Raj balance_= 2000000000 openedOn_= Thu May 23 00:00:00 EDT 2002 Savings: interestRate_= 2
_________________________________________________

C:\run\projects\bank\enhanced>

Download

You can download the entire enhanced folder from here.

click here to go to
My JDO HomePage...

click here to go to
My Advanced Java Tutorial Page...

About the Author...
Gopalan Suresh Raj is a Software Architect, Developer and an active Author. He has co-authored a number of books including "Professional JMS", "Enterprise Java Computing-Applications and Architecture" and "The Awesome Power of JavaBeans". His expertise spans enterprise component architectures and distributed object computing. Visit him at his Web Cornucopia site (http://gsraj.tripod.com/) or mail him at gopalan@gmx.net.

Go to the Component Engineering Cornucopia page

This site was developed and is maintained by Gopalan Suresh Raj

This page has been visited times since February 26,2002.

Last Updated : Feb 26, 2002

If you have any questions, comments, or problems regarding this site, please write to me I would love to hear from you.


Copyright (c) 1997-2002, Gopalan Suresh Raj - All rights reserved. Terms of use.

All products and companies mentioned at this site are trademarks of their respective owners.