David has posted 3 posts at DZone. View Full User Profile

jPersist 3.2.6: Efficient Prepared Statements & Batch Updates

05.30.2008
| 6427 views |
  • submit to reddit
Location: 
http://www.jpersist.org

jPersist now includes added support for more efficient use of prepared statements and batch updates, and fixes a couple of issues introduced with yesterday’s jWebApp release.

With jPersist you can now do the following:

        // Inserting contacts - efficiently uses a single prepared statement
db.saveObject(new Contact("contact0", ...));
db.saveObject(new Contact("contact1", ...));
db.saveObject(new Contact("contact2", ...));
db.saveObject(new Contact("contact3", ...));
db.saveObject(new Contact("contact4", ...));

/**
* Inserting contacts - uses batch updating
* and efficiently uses a single prepared statement
*/
db.beginBatch();

db.saveObject(new Contact("contact5", ...));
db.saveObject(new Contact("contact6", ...));
db.saveObject(new Contact("contact7", ...));
db.saveObject(new Contact("contact8", ...));
db.saveObject(new Contact("contact9", ...));

db.executeBatch();
db.endBatch();

/**
* The following are equivalent to the above.
*/

// Inserting contacts - efficiently uses a single prepared statement
new UpdateManager(dbm)
{
public void run() throws JPersistException
{
for (int i = 10; i < 500; i++)
saveObject(new Contact("contact" + i, ...));
}
}.executeUpdates();

/**
* Inserting contacts - uses batch updating
* and efficiently uses a single prepared statement
*/
new UpdateManager(dbm)
{
public void run() throws JPersistException
{
for (int i = 501; i < 1000; i++)
saveObject(new Contact("contact" + i, ...));
}
}.executeBatchUpdates();

 

jPersist is an extremely powerful object-relational persistence API that is based on the Active-Record and Data-Mapper patterns. jPersist wraps JDBC functionality and can work with any relational database, and any type of connection resource, including: java.sql.DriverManager, JNDI, javax.sql.DataSource, and third party connection pooling libraries (DBCP, C3PO, etc.).

Using jPersist is as simple as:

    public void DatabaseExample(DatabaseManager dbm) throws JPersistException
{
// Inserting contact with associations
Contact contact = new Contact("deisenhower", ...);

contact.getSupport().add(new Support("Request", ...));
contact.getSupport().add(new Support("Response", ...));
contact.getSupport().add(new Support("Request", ...));

contact.getOrders().add(new Order("Dwight D. Eisenhower Dollar", ...));
contact.getOrders().add(new Order("Susan B. Anthony Dollar", ...));

// Saving within an automatic transaction (covers all relationships)
dbm.saveObject(contact);

// Load based on information contained in classes
contact = dbm.loadObject(Contact.class, "where :contactId like ?", “tjef%”);
System.out.println("\ncontactId = " + contact.getContactId());

// Load a collection of objects from the database
Collection<Contact> c = dbm.loadObjects(new Vector<Contact>(), Contact.class);

for (Contact contact2 : c)
System.out.println("contactId = " + contact2.getContactId());
}

 

jPersist Features:

  • Object-relational persistence API
  • A Java implementation of the active-record and data-mapper patterns
  • Mapping is automatic, there is no configuration, or annotation required
  • Objects are isolated from database changes, and vice versa
  • Optionally provides simple annotation/functional support for mapping
  • Full Support for MySQL, DB2, Oracle, Derby, HSQL, PostgreSQL, H2, and more
  • Object-oriented queries, inserts, updates, deletes and more
  • All JDBC functionality is supported at some level
  • Intermix objects with SQL, and use object properties in SQL fragments
  • Full support for database transactions (automatic and manual)
  • Automatically handles inheritance via table joins
  • Also supports single table inheritance and concrete table inheritance
  • Easily handles associations with single instance, arrays, and collections
  • Supports any JDBC source including DriverManager, JNDI, and DataSource
  • Support for third-party connection pooling (DBCP, C3PO, etc.)
  • No SQL required for queries, inserts, updates, deletes
  • Uses JDBC with access to underlying connection, statement and result set
  • Access stored procedures and prepared statements with the same object-oriented functionality
  • Full support for batch updates
  • Implements ListIterator and Iterable for object-oriented iteration through data
  • Tracks object persistence for inserting and updating
  • Can be cast to an interface for database proxying
  • Multiple utilities for loading data into collections and maps
  • and more

All in all, jPersist’s sole reason for being is to provide simple trouble-free POJO oriented
access to your database. With jPersist and your POJOs, you should rarely have to
do anything more than:

  • Define a POJO (optionally extending jpersist.PersistentObject), which can be extended by other POJOs (relational hierarchy via joins).
  • Call one of jpersist.DatabaseManager’s: loadObject(), saveObject(), and deleteObject(), or one of jpersist.Database’s: saveObject(), deleteObject(), and queryObject() with jpersist.Result’s loadObject().
  • Group saves and deletes in a transaction using jpersist.TransactionManager or use jpersist.Database’s transaction oriented methods.
  • Group updates in an update manager using jpersist.UpdateManager or use jpersist.Database’s update and batch oriented methods.

That’s not to say you won’t have other JDBC needs, like calling stored procedures or executing SQL statements, but with jPersist it’s all easy to handle.

0
Published at DZone with permission of its author, David B..

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)