13

I am planning to deploy GlassFish v3 open source edition to a production environment. It comes with JavaDB (Apache Derby) which is just what I need. The only problem is that JavaDB is not started by default when GlassFish starts. I would have to go to the command line and enter:

asadmin start-database

Is there a way to make the database start automatically whenever the server (GlassFish) starts? I hated doing that manually everytime while I was developing my application and I certainly don't want to do that in production.

Thanks in advance

sschober
  • 2,003
  • 3
  • 24
  • 38
del.ave
  • 1,888
  • 5
  • 17
  • 23

4 Answers4

9

This is a little dated but there's a checkbox in the GlassFish section in Eclipse's preferences titled "Start the JavaDB database process when starting GlassFish Server". I am running Eclipse Indigo SR1.

Truman
  • 91
  • 1
  • 1
  • Yep I know the little check box all right, but make sure that in the same dialogue box that it's pointing to the correct JavaDB location, I had that wrong and Eclipse couldn't start Derby, took a while to spot! – EraserheadIRL Aug 29 '12 at 14:48
  • Thanks, Carl. Netbeans has the same option, and it's enabled by default. – DavidS Sep 02 '14 at 16:44
6

Once you go into production, you can start the db once and just leave it running, regardless of the state of the app server.

You could create a shell script to 'bundle' start-domain and start-database into a single uber-start command.

vkraemer
  • 9,864
  • 2
  • 30
  • 44
3

This is what I do, I deploy this to the server packaged as an EJB JAR. This will enable the Derby server to be started as its own enterprise application.

import java.io.PrintWriter;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;

import org.apache.derby.impl.drda.NetworkServerControlImpl;

@Singleton
@Startup
public class LocalDatabase {

private NetworkServerControlImpl networkServerControlImpl = null;

@PostConstruct
private void init() throws Exception {
    networkServerControlImpl = new NetworkServerControlImpl();
    networkServerControlImpl.start(new PrintWriter(System.out));
}

}
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
3

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

Bernard Hauzeur
  • 2,317
  • 1
  • 18
  • 25