Embedded servers
Derby Server and Administration Guide
13
existing Java applications that currently run against Derby in embedded mode to run
against the Derby Network Server.
explains how to manage the Network Server by
using the command line, including starting and stopping it.
explains how
to use the servlet interface to manage the Network Server.
contains advanced topics for Derby Network
Server users.
Because of the differences in JDBC drivers that are used, you might encounter
differences in functionality when running Derby in the Network Server framework as
opposed to running it embedded in a user application. Refer to
for a complete list of the differences between
embedded and Network Server configurations.
Embedded servers
Because Derby is written in Java, you have great flexibility in how you choose to
configure your deployment. For example, you can run Derby, the JDBC server
framework, and another application in the same JVM as a single process.
How to start an embedded server from an application
In one thread, the embedding application starts the local JDBC driver for its own access.
/*
If you are running on JDK 6 or higher, you do not
need to invoke Class.forName(). In that environment, the
EmbeddedDriver loads automatically.
*/
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection conn = DriverManager.getConnection(
"jdbc:derby:sample");
In another thread, the same application starts the server framework to allow remote
access. Starting the server framework from within the application allows both the server
and the application to run in the same JVM.
Embedded server example
You can start the Network Server in another thread automatically when Derby starts
by setting the derby.drda.startNetworkServer property (see
), or you can start it by using a program. The following example shows how to
start the Network Server by using a program:
import org.apache.derby.drda.NetworkServerControl;
import java.net.InetAddress;
NetworkServerControl server = new NetworkServerControl
(InetAddress.getByName("localhost"),1527);
server.start(null);
The program that starts the Network Server can access the database by using either
the embedded driver or the Network Client driver. The server framework's attempt to
boot the local JDBC driver is ignored because it has already been booted within the
application's JVM. The server framework simply accesses the instance of Derby that is
already booted. There is no conflict between the application and the server framework.
The remote client can then connect through the Derby client driver:
String nsURL="jdbc:derby://localhost:1527/sample";
java.util.Properties props = new java.util.Properties();
props.setProperty("user","usr");