Skip to main content

A Read-only database in a jar?

Posted by bernt on October 3, 2005 at 2:26 PM PDT

In Norway, all wines and liquors and beers stronger than 4.75 volume percent are sold at Vinmonopolet a state monopoly (Literally "The Wine Monopoly").

What has this to do with a read only database? Well, I once had an application on my Palm which contained Vinmonopolet's complete catalogue. Every now and then I updated the catalogue and was the able to search for information about their products (they carry a very impressive selection of good wines). The querying possibilities was somewhat limited. E.g. I could only search for wines within certain fixed price ranges, search for wines from only one country/district/area etc.

What I really wanted was an application with poweful searching possibilities: E.g. Show all Pinotage wines from South Africa and all Shiraz wines from Australia where the price is between NOK 130 and 150. Since I'm a programmer this power could be given me by the means of SQL. Yes, I admit that it would not be very user-friendly for the average wine-consumer, but one could imagine SQL queries as an "advanced option".

Ok. To the point. This application could be implemented as a small Java application on top of a SQL database. And yes, it's easy to make such an application with Apache Derby.

The follwing list shows roughly the steps needed to implement my favourite wine application:

  1. Create an application that populates a database for the intended purpose. Remember to do a proper shutdown of this database. This ensures that the database is checkpointed (see the Derby Reference Manual under the shutdown=true url attribute).
  2. Create the user application (of course..... ;-)
  3. Pack the database files and the user application into one jar file. The Derby property needs to be set, and the application need to do the above described shutdown when it is exited (Since a read-only database can be used from several Derby instances simultaneously, eavch instance will create a unique directory under the tempDirectory which will be deleted during shutdown). The database is accessed with the url jdbc:derby:classpath:<path to database>
  4. Ship your combined application/database jar file together with derby.jar and there you go.

Easy, isn't it? And I can envision a lot of other applications for a read only database. Can you?

2005-10-04 11:20:00 CEST: jwenting's comment is kind of right, so I changed from "read only database" to "read only database in a jar" in title and blurb to make the blog entry more precise.

Related Topics >>