I would advise to take advantage from the fact that Derby can perform both as embedded server (i.e. running in the app server JVM) and network server (i.e. servicing client requests addressed to default port 1527 from the local host). Therefore you benefit from the increased performances of the embedded mode, yet still allow access from e.g. "ij" to administer data while the server is running, and from other server instances over TCP/IP with suitable security settings.
In that configuration, Derby starts and stops along with the application server. No need for extra commands or explicit server start code to launch derby.
The configuration is described below for Glassfish 4 and derby/javaDB 10.10, but will work similarly in other servers and versions. You have indeed to adjust all paths below to match with your own installation.
.1. Make derby classes available to the server 'common' class loader by adding derby.jar, derbyclient.jar, derbynet.jar, derbytools.jar. Copy the jar's for instance into the JVM lib/ext of your server instance, e.g. into C:\java\J2EESDK7U1\glassfish\domains\domain1\lib\ext
.2. Using the glassfish admin GUI, add the following two properties to Configurations > server-config > JVM settings > JVM Options tab:
-Dderby.drda.startNetworkServer=true
and
-Dderby.system.home=C:/java/J2EESDK7U1/glassfish/databases
. The first tells Derby to start listening in network mode when the embedded engine is loaded, the second supplies the essential path to your derby databases and the optional derby.properties file (e.g. with your security settings in PROD)
.3. arrange for the server to load the class org.apache.derby.jdbc.EmbeddedDriver
at startup. A way to achieve this is for instance to annotate an EJB with @Startup
, and then define a @PostConstruct
annotated method in the EJB, alike:
@PostConstruct
private void startup() {
try { Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
logger.info("Started DERBY in embedded+network mode");
} catch (ClassNotFoundException e) {
... your error handling
}
}
for 6 other startup tips, see http://blog.eisele.net/2010/12/seven-ways-to-get-things-started-java.html