HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Optimizing Database Performance in J2EE Applications"
Vol. 7, Issue 5, p. 20

	


Listing 1: Transparent persistence with an ODBMS

pmf.setConnectionURL(dbName); 
pm = pmf.getPersistenceManager(); 
tx = pm.currentTransaction();
tx.begin();
Order odr = new Order("Parrot", 2,  99.95);
pm.makerPersistent(odr);
// make the Order persist
Customer cstr = new Customer("Jay", "D", "Ho");
odr.orderedBy = cstr ;
// the new Customer now persists by the mean of attachment
tx.commit();
pm.close();


Listing 2: The OR mapping layer adds 2550% of ugly code

String select = "select * from Order o where o.odr_id = ?";
pstmt = conn.prepareStatement(select);
pstmt.setLong(1, 10608974);
resultSet = pstmt.executeQuery();
// Construct an instance of Order from its rows
if (resultSet.next()) {
    Order odr = new Order(resultSet.getLong("odr_id"),
   resultSet.getString("ship_address"),
    resultSet.getString("ship_carrier"));
     }
     resultSet.close();
     pstmt.close(); 

String select = "select * from lineItem l where l.odr_id = ? order by lt_id";
pstmt = conn.prepareStatement(select);
pstmt.setLong(1, 10608974);
resultSet = pstmt.executeQuery();
// Construct objects from rows
while (resultSet.next()) {
  LineItem litem = new LineItem(resultSet.getString("lt_id"),
    resultSet.getString("quantity"),
    resultSet.getBigDecimal("unit_price"),
    resultSet.getString("delivery_mode"),
    resultSet.getString("address"));
  odr.lineItems.add(litem);
}
resultSet.close();
pstmt.close();


Listing 3: A hybrid database provides compactness and efficiency

Database db("dbName");
db.open();
db.startTransaction();
Customer cstr = new Customer(db, "Dee", "O", "Haye");
Order odr = new Order(db, "Parrot", 1,  99.95);
odr.setOrderedBy(cstr) ;
// the order and the customer are linked together
by a bi-directional relationship
db.commit();
db.close();


Listing 4: With a hybrid database, SQL queries return objects, not tables

String query = "select REF(c) from Customer c where count(orders) > 20" +
  "AND ((class Consumer).bonusMiles > 50000 OR" +
  "(class Business).creditLine > 10000) ORDER BY lastName";
stmt = conn.createStatement();
resultSet = stmt.executeQuery(select);
while (resultSet.next()) {
  Customer cstr = (Customer)resultSet.getObject(1);
  // instances of Consumer or Business are actually returned
}
resultSet.close();
stmt.close();

 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.