Embedding the Apache Derby database in a Web Application
I found this article on integrating Derby with Tomcat. It shows three integration scenarios:
- The prototype integration
- Derby is used only by a single web application, but each HTTP request actually causes an attempt to load the Derby system (in embedded mode) into memory. A new connection is created for each request. There is no proper shutdown of neither single databases nor the Derby system.
- The casual integration
- Derby is available to multiple web applications. Uses Tomcat-specific (non-standard) lifecycle events to load the Derby system (in embedded mode) on startup of the container, and shutting down the system on exit of the container. A separate connection is created for each request.
- The enterprise integration
- As with the casual integration, Tomcat-specific lifecycle events are used to load and shut down the Derby system, but here in Network Server mode. Also, a DataSource is set up and used for getting client connections.
There is a better way to do container-independent lifecycle management in the case that Derby is bundled with/used by only one web application: Application Lifecycle Events, as defined in the Java Servlet Specification version 2.4, chapter SRV.10. With these, you can load the Derby system when your web application is loaded (when the servlet context has been created), and shut down Derby when the web application is unloaded, all in a way which is portable across servlet container implementations.
Also, you could cache existing connections in your application's ServletContext, if you don't care to set up a DataSource.
These techniques should allow you to quickly integrate Derby into your web application.