Java Chatter and Random Nagging

Saturday, November 18, 2006

Adding your development database under source control

<disclaimer>
I am not too sure whether the technique I am going to present today falls under the category of Source Control best practices. But I have been using this technique for a while now, I kind of like it, and I didn't experience any problems with it.
</disclaimer>

I like HSQLDB, in fact, I like it a lot !
(For any Marsians, HSQLDB is the leading SQL relational database engine written in Java. It is also the database engine in OpenOffice.)
And since I am using HSQLDB for my petproject, I wanted it to be portable and easily configurable/buildable, just like the petproject itself. And I wanted to be able to start it up and shut it down (I use mainly the standalone server) from within my favorite IDE. So these are the steps it took to meet my requirements :
  1. In Eclipse, create a New Project (of the General type, since you don't need a Java compiler).
  2. Create the following files in this project:
    • lib/hsqldb.jar: the binary jar, downloaded from the HSQLDB website.
    • build.properties: contains the properties for the ant build file.
    • server.properties: contains the parameters for starting up the database server.
    • build.xml: the ant build file.
  3. Add the whole project to source control.


The content of each of these files (except for the .jar) :

server.properties

#Ant properties for starting the hsqlserver hrutil
server.port=9001
server.database.0=myProject
server.dbname.0=myProjectDB

build.properties

#Build properties for starting, stopping and communicating with the database
db.driver=org.hsqldb.jdbcDriver
db.url=jdbc:hsqldb:hsql://localhost/myProjectDB
db.user=sa
db.password=

build.xml

<project name="hsqldb" default="startup">
<property file="build.properties"/>
<property file="server.properties"/>

<path id="master-classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
</path>


<target name="startup" description="Starts up the standalone hsqldb server.
The server will automatically use the properties from server.properties .">
<java classname="org.hsqldb.Server" classpathref="master-classpath" fork="true"/>
</target>

<target name="manager" description="Starts up the built-in hsqldb databasemanager.">
<java classname="org.hsqldb.util.DatabaseManager" classpathref="master-classpath" fork="true"/>
</target>

<target name="shutdown" description="Shuts down the standalone hsqldb server.">
<sql
driver="${db.driver}" classpathref="master-classpath"
url="${db.url}"
userid="${db.user}"
password="${db.password}">shutdown;
</sql>
</target>

<target name="deleteDbFiles" description="Deletes the script files that the hsqldb server has created (Necessary for a clean restart.)">
<delete>
<fileset dir="">
<include name="${server.database.0}.properties"/>
<include name="${server.database.0}.script"/>
</fileset>
</delete>
</target>
</project>

The descriptions in the build.xml should be pretty self-explanatory to use this configuration. Its startup and shutdown targets make it possible to start and stop your hsqldb database server from within Eclipse. And even better, when developing on another computer, simply import the project from source control and you are immediately ready to start the database!!

0 Comments:

Post a Comment

<< Home