Thursday, October 4, 2012

Start Apache Derby in server mode programmatically

Derby can be started in the embedded mode and server mode. In this post, I will explain how to start derby in the server mode. You can start derby in the server mode using the script startNetworkServer which is located under bin folder of derby binary. But suppose you want to start the derby server from your program without downloading derby binary. There are two ways to do this.
  1. You can create your own script which will wrap the original startNetworkServer 
  2. You can start derby in the server mode programmatically
For both cases, you need the following jars to be in your classpath.
  • derby.jar
  • derbyclient.jar
  • derbynet.jar
  • derbytools.jar
Please note that you can't have the jars with their versions. If you are adding those jars using a maven project, you will need to rename them by removing the version part of those jars. 

1. Write a script to wrap startNetworkServer 

Suppose I create a script file called Here is what is inside my script. 

export DERBY_HOME="path where the above jars located"
./startNetworkServer $*

In this I have a copy of startNetworkServer at the same location where my located. If you run the and if the above jars in your classpath, you will be able to start derby in the server mode in it's default port 1527.

2. Start derby in the server mode programmatically

Following is the code snippet that allow you to start derby in the server mode programmatically. Please note that you need to have above jars in your classpath.

System.setProperty("derby.drda.startNetworkServer", "true");
// start derby in port 20000
// db user - airavata
// db user password - airavata
 server = new NetworkServerControl(InetAddress.getByName("localhost"), 
                                   "airavata"); consoleWriter = new, true);

//if you want to shut down the server
// server.shutdown();

Now the derby will start on port 20000.

Hope this will helpful and save some time of you for searching how to do that. 

1 comment:

Sanilk said...

When i add this to my code it shows the below errors.
Could not listen on port 1527 on host Address already in use: JVM_Bind
The connection was refused because the database XXXX was not found.

I am using netbeans IDE.From IDE,it successfully run after start server(JavaDB->start server)