How to set up a Derby server as a Tomcat application
Derby is a 100% pure Java database engine. Its main strength is that it's embeddable, so if you have a standalone application that could benefit from keeping persistent data in a database but you don't want to have to set up a separate database server just to support it, Derby may be the answer. In the embedded mode your application starts Derby during initialization and shuts it down during termination. In the meantime it can work with database tables using SQL statements through a JDBC driver in the usual way.
Why this article?
Derby can also run as a network server, like any other database. Not only that, you can install it as a web application in a server such as Tomcat so that your database server is available whenever your web server is. In fact, the Derby distribution includes a WAR file that you can deploy that makes this very easy.
The problem is that Derby's documentation seems to concentrate on the embedded mode of operation and it's not easy to find information about setting it up as a Tomcat application without quite a bit of digging. Since I've been through this particular loop I decided to write this, partially as a reference for myself in case I need to do it again, and also for others needing to do the same thing.
This article describes the setup with Tomcat 6 running on Windows (XP Pro, specifically). Unix/Linux setup is similar and it shouldn't be a problem for anyone familiar with those systems to adapt the steps as required.
Download and unpack the distribution
Derby is distributed as a .zip or .tar.gz archive. There are a couple of different downloads available depending on whether you just want the executable JAR files or the full package including documents and sample code. Download the appropriate one then simply unpack it into a convenient place. I used the 'bin' distribution which includes everything.
The archive unpacks such that everything is inside one directory (the current version puts itself into 'db-derby-10.3.2.1-bin' for example, for the 'bin' distribution). This is Derby's 'home' directory, which we'll need to know in a moment.
Setting the environment and Java system properties
Derby only really needs one environment setting:
DERBY_HOMEshould be set to the path of the home directory as noted above. On a Windows system use 'Control Panel-> System' then open the Advanced tab. You can add the new environment setting as a System or User value in the dialog there. 'System' is probably better, especially if you run Tomcat as a service.
You may also want to add
%DERBY_HOME%\binto your PATH setting; this isn't required but it makes it easier to run the Derby command-line tools.
One Java system property really needs to be set, too:
derby.system.homeidentifies a directory where Derby will keep all its databases. Without this, when you run Derby under Tomcat it will default to '.', which equates to the Tomcat installation directory - the place where it keeps its
WebAppsdirectories. This is almost certainly not a good idea (imagine what might happen if an application tries to create a database called 'conf').
I created a "DerbyData" directory to hold all my databases. Setting the
derby.system.homeproperty is a little bit of a problem, but I found a solution that works fine; Tomcat 5 and 6 include a "Monitor" program that you can run, at least on Windows. You can use this to set up the system properties (run the monitor, right-click on the system tray icon that appears, then open the 'Configure' option and click the 'Java' tab). Add
-Dderby.system.home=your-database-directoryto the system properties and click OK. Tomcat doesn't need to be running to do this.
Start Tomcat (if it's already running you should probably shut it down and restart). Start your browser, find the Tomcat Manager page and log in.
Deploy the Derby WAR file in the usual way. You'll find the WAR file (
derby.war) in Derby's
You should now be able to hit the Derby service in your browser - it'll be at
/derby/derbynet. This starts the database running, and if everything's ok you'll see a status page.
Modifying Derby's web.xml
We're not quite done yet. With the default installation you have to hit the Derby URL as above to start the database running every time you restart Tomcat. What's happening is that the database is started by the servlet's
init()method, and that isn't happening until Tomcat gets a request. This probably isn't what you want, especially if you're planning to create other web applications that use the service. You probably want the thing to come up on its own.
You'll find Derby's web.xml file under your Tomcat installation at
WebApps/derby/WEB-INF/web.xml. Open the file in a text editor and add the following tag inside the
This will cause Tomcat to call the servlet's initialization during startup instead of waiting for a request.
Save your text change and restart Tomcat.
After restarting Tomcat you should see a 'derby.log' file appear in your database directory. It may take a few seconds for this to happen, so give it time - if it hasn't shown up after, say, a minute, you may want to check your Tomcat logs to see if anything bad happened. Also, check your Tomcat directory to make sure the log file didn't show up there - if it did, it means that the
derby.system.homesetting hasn't taken for some reason, in which case you should go back and check for misspellings, etc.
If the log file appears in the right place, that probably means everything's ok so far. You can test the installation using Derby's
ijcommand-line tool. Open a command window and do this (this assumes that you set the PATH environment as suggested earlier):
ij version 10.3
> connect 'jdbc:derby://localhost/testdata;create=true';
This tells ij to open a connection to the database named 'testdata', creating it if it doesn't exist (which it shouldn't, at this point).
Now check your database directory - there should be a subdirectory called 'testdata' if all went well. You can delete it manually, although I'd recommend shutting Derby down first - either shut down Tomcat or hit the Derby URL as above and click the Stop button.
Congratulations! You now have a database server that will be available whenever Tomcat is running.