- Quick Intro to JDO
- The Problem at Hand
- Persisting the Objects
- Retrieving the Objects
- The Wrap-Up
Persisting the Objects
To store our objects, we need an instance of PersistenceManager, retrieved through a PersistenceManagerFactory, as shown in Listing 3.
Listing 3 Initializing our JDO connection
protected void initJDO() throws Exception { Properties props = null; try { props = new Properties(); props.load(new FileInputStream( new File("QuotationExtraction.jdogenie"))); setPMFactory(JDOHelper.getPersistenceManagerFactory(props)); setPM(getPMFactory().getPersistenceManager()); } catch(Exception e) { throw new Exception("Error initializing jdo: " + e); } }
The Properties file here is simply the project file from JDO Genie, which contains all the behind-the-scenes connection information. Once we have our PersistenceManager, we can use it to persist a QuoteSource for each file (see Listing 4).
Listing 4 Persisting a QuoteSource
protected QuoteSource persistQuoteSource(QuoteSource source) throws Exception { Query query = null; PersistenceManager pm = null; Collection collection = null; try { //Get the PersistenceManager pm = getPM(); //See if this QuoteSource exists query = pm.newQuery(QuoteSource.class, "name.toLowerCase() == param"); query.declareParameters("String param"); collection = (Collection)query.execute(source.getName().toLowerCase()); //No results, persist source if(collection.size() == 0) { pm.currentTransaction().begin(); pm.makePersistent(source); pm.currentTransaction().commit(); } //Found a result, return it else { source = (QuoteSource)collection.iterator().next(); refreshQuoteSource(source); } } catch(Exception e) { throw new Exception("Error while persisting QuoteSource: " + e); } return source; }
This method checks to see whether a specified QuoteSource already exists in the database, and returns the QuoteSource if so. If not, it stores the QuoteSource in the database via the single line of code pm.makePersistent(source). All database writes must occur within a transaction, which we handle with the lines of code preceding and following the call to persist the QuoteSource.
To persist the QuoteInfos themselves, the class pulls them from the input file 1,000 at a time, puts them into a global List, and then calls the method in Listing 5 to persist that List.
Listing 5 Persisting multiple QuoteInfos
protected void persistQuoteList() throws Exception { QuoteInfo currInfo = null; PersistenceManager pm = null; try { //Get the PersistenceManager pm = getPM(); //Persist the classes pm.currentTransaction().begin(); for(int i = 0; i < getQuoteList().size(); ++i) { pm.makePersistent((QuoteInfo)getQuoteList().get(i)); } pm.currentTransaction().commit(); //Clear the list getQuoteList().clear(); } catch(Exception e) { throw new Exception("Error in persistQuoteList: " + e); } }
Unlike the example in Listing 4, where we persisted only one object in a transaction, here we're persisting multiple objects within a single transaction.
TIP
You can do as much work as you'd like in a single transaction, but beware that a failure at any point could result in the entire transaction being scrapped.