Use a database on BUG

by kengilmer

Here’s a quick howto on using Apache Derby on the BUG 2.0 to get hot RDBMS action.

Step 1, download the Apache Derby binary package from  I chose the ‘lib’ distribution.  Unsure if Derby supports running from an OSGi context, I used the handy bnd utility to check.

$ bnd print lib/derby.jar

[MANIFEST derby.jar]
Ant-Version Apache Ant 1.7.1
Bundle-Activator org.apache.derby.osgi.EmbeddedActivator
Bundle-ManifestVersion 2
Bundle-Name Apache Derby 10.7
Bundle-SymbolicName derby
Bundle-Vendor Apache Software Foundation
Bundle-Version 10.7.1000001.1040133
Class-Path derbyLocale_cs.jar derbyLocale_de_DE.jar derbyLocale_es.jar derbyLocale_fr.jar derbyLocale_hu.jar derbyLocale_it.jar derbyLocale_ja_JP.jar derbyLocale_ko_KR.jar derbyLocale_pl.jar derbyLocale_pt_BR.jar derbyLocale_ru.jar derbyLocale_zh_CN.jar derbyLocale_zh_TW.jar
Created-By 14.3-b01-101 (Apple Inc.)
DynamicImport-Package *
Export-Package org.apache.derby.authentication,org.apache.derby.database,,org.apache.derby.jdbc,org.apache.derby.vti
Manifest-Version 1.0
Sealed true

Here I can see that the jar file has the necessary bits to run in an OSGi context, namely the “export-package” header in the MANIFEST.MF file.

Step 2, install the necessary bundles on BUG.

After unzipping the Derby distribution, we’ll copy the bundle to the BUG:

# scp lib/derby.jar root@[MY BUG IP]:/usr/share/java/bundle/

At this point I ssh into the BUG and run commands from there.  I restart Apache Felix so that the Derby bundle loads up.

# /etc/init.d/felix restart

 I can confim it’s running from the OSGi shell:

# telnet localhost 8090
Connected to localhost.localdomain.
Escape character is '^]'.

(: bundles
33 active Apache Derby 10.7 10.7.1000001.1040133 file:/usr/share/java/bundle/derby.jar

To exit the shell type “disconnect”.

Step 3, create a sample application that exersizes the Derby JDBC driver.

Using the code used in this tutorial, I wrote a simple BUGapp that called the sample classes main() method from the BundleActivator.

I had to do some futzing with my sample application’s manifest file.  I had to add “Bundle-ManifestVersion: 2” to the manifest, as this was not generated by the SDK and seems to be required for Felix to properly load the correct JDBC drivers provided by the Derby bundle.

After installing the sample app on the BUG, you should see some output in /var/log/felix.log that looks like:

SimpleApp starting in embedded mode
Loaded the appropriate driver
Connected to and created database derbyDB
Created table location
Inserted 1956 Webster
Inserted 1910 Union
Updated 1956 Webster to 180 Grand
Updated 180 Grand to 300 Lakeshore
Verified the rows
Dropped table location
Committed the transaction
Derby shut down normally
SimpleApp finished

And that’s it!  Looking at the sample code as provided in the Derby Tutorial, it should be straight forward to incorporate database functionality into BUGapps.