How to make GlassFish's embedded Derby listen on a socket
The convenience of having instant access to an embedded production-quality database was one of the reasons why I found GlassFish interesting. However, even an embedded database must at times be accessible from other clients than the main client, for purposes such as installation, maintenance, debugging, etc. There are embedded databases such as Berkeley DB and SQLite that solve this with filesystem locks. Derby has another option: the embedded Derby can start a server thread that listens on a network socket. The GlassFish documentation recommends that you start a separate JVM process for the database server in order to run client applications that use network JDBC-urls, but this complicates the GlassFish service management in a way that isn't really necessary. After all, the GlassFish server process contains an embedded Derby which already has the capability to listen on sockets.
But I found no obvious way to turn on this feature in the GlassFish admin console. After wading through huge amounts of documentation, I finally found a simple solution: by setting the following system properties, GlassFish will start a Derby network server on localhost, which goes away when GlassFish is stopped.
To turn this on in GlassFish, add these properties to the first domain configuration. Insert these lines somewhere among the <jvm-options> under <java-config> in "domains/domain1/config/domain.xml":
Addendum: a few weeks after writing this, I found out that in a freshly installed GlassFish, the Derby network server isn't started at boot, because the embedded-Derby driver doesn't get loaded until the first time it is used. One way to force this to happen is by accessing the built-in connection pool
[Update: I have since found that doing it like this is not a good approach. See "Embedded Derby on a socket, revisited"]
But I found no obvious way to turn on this feature in the GlassFish admin console. After wading through huge amounts of documentation, I finally found a simple solution: by setting the following system properties, GlassFish will start a Derby network server on localhost, which goes away when GlassFish is stopped.
derby.system.home=/opt/glassfish/databasesThe above assumes that "/opt/glassfish" is the installation directory. The "asadmin start-database" command will then use "/opt/glassfish/databases" for network clients, so that's what we want for the embedded derby listener as well in order to be compatible. The last two properties are only needed if you want something other than the default values of localhost and 1527.
derby.drda.startNetworkServer=true
derby.drda.host=localhost
derby.drda.portNumber=1527
To turn this on in GlassFish, add these properties to the first domain configuration. Insert these lines somewhere among the <jvm-options> under <java-config> in "domains/domain1/config/domain.xml":
<jvm-options>-Dderby.system.home=${com.sun.aas.installRoot}/databases</jvm-options>First stop glassfish and any standalone derby server that might be running, then apply this patch and then start glassfish again, and voila! You can now use the Derby JDBC network client without starting a separate Derby server.
<jvm-options>-Dderby.drda.startNetworkServer=true</jvm-options>
Addendum: a few weeks after writing this, I found out that in a freshly installed GlassFish, the Derby network server isn't started at boot, because the embedded-Derby driver doesn't get loaded until the first time it is used. One way to force this to happen is by accessing the built-in connection pool
__TimerPool
. This can be done from the command line:asadmin ping-connection-pool __TimerPoolOr you can deploy an application that creates a
javax.ejb.Timer
. This seems to have the permanent effect that __TimerPool
is accessed automatically at boot time, causing the Derby embedded driver to load and start the network server.[Update: I have since found that doing it like this is not a good approach. See "Embedded Derby on a socket, revisited"]