Monday, October 27, 2008

SD Best Practices 2008 Notes

For those that attended in person, thanks for coming to my talks at SD Best Practices.

If you're interested in the slides I presented, please e-mail me. They always change up to the last minute!

If you want advice on how to improve xUnit adoption on your enterprise project, feel free to e-mail me! I'm happy to provide advice. I'm working on a book about adoption of xUnit testing in enterprise projects, so I want to learn as much as possible about the different obstacles out there.

If you can, please take Scott Ambler's 5 minute TDD adoption survey:

Recommended bestpractices for xUnits with the Database:

1. Use the Dependent Object Framework (the DOF) for easy setup of database dependencies for xUnit tests.
2. Use JPA, possibly Hibernate Entity Manager
3. Use an in-memory database, such as H2 (see related blog entry)

To get started on the DOF,
1. Review the DOF User Guide
2. Review the "Hello DOF" example programs.

If you want help implementing the DOF, please contact me.

I'll be speaking at Voices that Matter in San Francisco on December 3

So Wassup with writing software without adding automated unit tests as you go along? Well, this video came to mind. The Wassup video:

In-Memory DBs for TDD: H2 versus HSQLDB

Justin Gordon, October 27, 2008

Use of in-memory databases can dramatically improve your ability to write xUnit tests. Should one also use H2 or HSQLDB for xUnit testing? Or some other Java open source database? My examples for the Dependent Object Framework support both H2 and HSQLDB. It is relatively simple to switch between them. Mostly, it's a matter of switching the JDBC connection string.

Criteria for doing database TDD:

The functionality desired is
  1. FAST. H2 claims to be the fastest In a simple test I did, H2 and HSQLDB performed almost identically.

  2. DB2 or Oracle compatiblity support. H2 claims to have both: Oracle and and MS SQL Server Compatiblity Mode. If this works well, this is a huge win.

  3. Stability: HSQLDB seems somewhat better supported as HSQLDB appears to be used in more commercial applications, so it may be better supported. In the past, Hibernate support seemed more stable. Much of this difference is probably attributable to HSQLDB being older.

  4. Ease of use: H2 slightly beat out HSQLDB in this category with a simpler install that provides a shortcut to starting an in-memory database and a browser session with a
    nice interface. Couldn't be any simpler than this! Setting up HSQLDB was definitely a bit trickier, and the UI is definitely better in H2.
Common DB features not required for TDD:
  1. Scalability
  2. Concurrency
  3. Stability under high load
  4. Data integrity

Those features are absolutely critical to your production application. However, TDD test suites run in single user mode and the tests do not even need to persist the data.


I know of one commercial software company that completely relies on H2 for their unit tests, while they deploy on Oracle.





Article on H2 by H2 creator: Mueller states that H2 is faster than HSQLDB for most
operations and is architecturally superior. Since he wrote the original HSQLDB, he'd be well qualified to comment. Here's the summary straight out of infoq:

HSQLDB creator Thomas Mueller has released 1.0 final of H2, his pure Java database successor to HSQLDB.
H2's focus is to be best database for the lower end (low number of concurrent connections, embedded usage). H2's features are comparable to MySQL and PostgreSQL; it has views, subqueries, triggers, clustering, role based security, encryption, user defined functions, disk and in-memory usage, embedded and client-server usage, referential integrity, scrollable result sets, schema support, transaction isolation. There are a few tools like the browser based Console application (now with auto-complete). A few things are still missing: H2 currently only offers table-level locking, full outer joins are not supported yet, the ODBC driver is only 'experimental' so far, and the standard API for distributed transactions (two-phase commit) is incomplete, however for most use cases these may not be critical.

Here's a good comparison of open source databases by David Leung, based on the following criteria:
  • FREE or Open Source
  • Multiple platform support
  • Easy to install and use
  • Can be bundle in code so little or no extra installation is required
  • Can be scalable, i.e. load balancing, size limit, etc.
  • Reliable, i.e. backup, replication, etc.
  • Efficient, i.e. indexing, fast search, etc.

He recommends, in the following order:
  1. H2
  2. Derby
  4. MySQL
  5. PostgreSQL
It is not so critical which in-memory database you use as it is to simply try out using an in-memory database for your xUnit tests. H2 is a good starting point, especially with it's Oracle and SQL Server compatibility modes.