## 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 set up the server and client programs when interacting with the Network Server.

- `SimpleNetworkServerSample.java`: This program starts the Derby Network Server 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:

1. Starts the Derby Network Server using a property and loads the embedded driver
2. Checks if the Derby Network Server is up and running
3. Creates the database `NSSimpleDB` if not already created
4. Obtains an embedded database connection
5. Tests the database connection by executing a sample query
6. Allows client connections to connect to the server until the user decides to stop the server and exit the program
7. Closes the connection
8. Shuts down the Derby Network Server before exiting the program

The following files are needed in the `DERBY_INSTALL\demo\programs\nserverdemo\` directory in order to run this sample program:

- Source file:
  `SimpleNetworkServerSample.java`
- Compiled class file:
  `SimpleNetworkServerSample.class`

### 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.

- The current directory (".")
- `derbynet.jar`:
  The Network Server jar file. It must be in your CLASSPATH since we start the Network Server in this program.
- `derby.jar`:
  The Derby database engine jar file.

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 = 23

    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

Do not press Enter at this time. Leave the server running while you run the `SimpleNetworkClientSample` program.

Running this program will also create new directories and files:

- NSSimpleDB
  This directory makes up the NSSimpleDB database.
- `derby.log`
  This log file contains Derby progress and error messages.

## Example of a client connecting to the Network Server

### SimpleNetworkClientSample program

This program:

1. Obtains a client connection using the DriverManager
2. Obtains a client connection using a DataSource
3. Tests the database connections by executing a sample query
4. Closes the connections and then exits the program

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

- Source file:
  `SimpleNetworkClientSample.java`
- Compiled class file:
  `SimpleNetworkClientSample.class`

### How to run the SimpleNetworkClientSample program

To connect to the Network Server:

1. Open another 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:
   - The current directory (".")
   - `derbyclient.jar`
     This jar file must be in your CLASSPATH to use the Derby Client Network JDBC Driver.
3. Once you have set up your environment correctly, execute the application from the `DERBY_INSTALL\demo\programs\nserverdemo\` directory:

       java SimpleNetworkClientSample [driverType]

   where the possible values for driverType are:

   - derbyClient (default)

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 = 23
Testing the connection obtained via a DataSource by executing a sample query
number of rows in sys.systables = 23
Goodbye!
`

After running the program, return to the command prompt where you ran the `SimpleNetworkServerSample` program and press Enter.