Simple Derby Network Server Example

Overview

The primary purpose of this example is to demonstrate how to obtain an embedded connection and client connections using the Network Server to the same database. This program shows how to use either the DriverManager or a DataSource to obtain client connections.

In order for a database to be consistent, only one JVM is allowed to access it at a time. The embedded driver is loaded when the Network Server is started. Hence, the JVM that starts the Network Server can get an embedded connection to the same database that Network Server is accessing to serve the clients from other JVMs. This solution allows you to take advantage of the performance benefits of the embedded driver as well as allow for client connections from other JVMs to connect to the same database.

In this example, the following two programs show how to setup the server and client programs when interacting with the Network Server.

SimpleNetworkServerSample.java: This program starts the Derby Network Server as well as the embedded driver and waits for clients to connect.

SimpleNetworkClientSample.java: This is the client program that interacts with the Derby Network Server from another JVM.

SimpleNetworkServerSample program

In particular, this program:

The following files should be installed in the %DERBY_INSTALL%\demo\programs\nserverdemo\ directory in order to run this sample program:

How to run the SimpleNetworkServerSample program

To run this simple Derby Network Server sample program:

  1. Open a command prompt and change directories to the %DERBY_INSTALL%\demo\programs\nserverdemo directory, where %DERBY_INSTALL% is the directory where you installed Derby.
  2. Set the CLASSPATH to include the following jar files in order to run this program.
  3. Test the CLASSPATH settings by running the following java command:
    java org.apache.derby.tools.sysinfo

    This command will show the Derby jar files that are in the CLASSPATH.

  4. Once you have set up your environment correctly, execute the application from the
    %DERBY_INSTALL%\demo\programs\nserverdemo directory:
    java SimpleNetworkServerSample

You should receive output similar to the following if the program runs successfully:

Starting Network Server
Testing if Network Server is up and running!
Derby Network Server now running
Got an embedded connection.
Testing embedded connection by executing a sample query
number of rows in sys.systables = 16

While my app is busy with embedded work, ij might connect like this:

$ java -Dij.user=me -Dij.password=pw -Dij.protocol=jdbc:derby://localhost:1527/ org.apache.derby.tools.ij
ij> connect 'NSSimpleDB';


Clients can continue to connect:
Press [Enter] to stop Server

Running this program will also create new directories and files:

Example of a Client connecting to the Network Server

SimpleNetworkClientSample program

This program:

The following files should be installed in the %DERBY_INSTALL%\demo\programs\nserverdemo\ directory in order to run this sample program:

How to run the SimpleNetworkClientSample program

To connect to the Network Server that has been started as part of the SimpleNetworkServerSample program:

  1. Open a command prompt and change directories to the %DERBY_INSTALL%\demo\programs\nserverdemo directory, where %DERBY_INSTALL% is the directory where you installed Derby.
  2. Clients of Derby Network Server only need the following jar files in the CLASSPATH in order to connect to the Network Server. Set the CLASSPATH to include the following jar files:
  3. Once you have set up your environment correctly, execute the application from the
    %DERBY_INSTALL%\demo\programs\nserverdemo\ directory:
    java SimpleNetworkClientSample [driverType]

You should receive output similar to the following if the program runs successfully:

Starting Sample client program
Got a client connection via the DriverManager.
connection from datasource; getDriverName = Apache Derby Network Client JDBC Driver
Got a client connection via a DataSource.
Testing the connection obtained via DriverManager by executing a sample query
number of rows in sys.systables = 16
Testing the connection obtained via a DataSource by executing a sample query
number of rows in sys.systables = 16
Goodbye!