apache > db
Apache DB Project
 
Font size:      

The NsSample sample program

The NsSample sample program

The NsSample program is a simple JDBC application that interacts with the Network Server.

This topic includes the following sections:

Overview of the NsSample sample program

The NsSample program performs the following tasks:

  • starts the Network Server.
  • checks that the Network Server is running.
  • loads the DB2 JDBC Universal driver.
  • creates the NsSampledb database if not already created.
  • checks to see if the schema is already created, and if not, creates the schema which includes the SAMPLETBL table and corresponding indexes.
  • connects to the database.
  • loads the schema by inserting data.
  • starts client threads to perform database related operations.
  • has each of the clients perform DML operations (select, insert, delete, update) using JDBC calls. For example, one client thread establishes an embedded connection to perform database operations, while another client thread establishes a client connection to the Network Server to perform database operations.
  • waits for the client threads to finish the tasks.
  • shuts down the Network Server at the end of the demonstration.

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

  • NsSample.java

    This is the entry point into the sample program. The program starts up two client threads. The first client establishes an embedded connection to perform database operations, while the second client establishes a client connection to the Network Server to perform database operations.

    You can change the following constants to modify the sample program:

    • NUM_ROWS - The number of rows that must be initially loaded into the schema.
    • ITERATIONS - The number of iterations for which each client thread does database related work.
    • NUM_CLIENT_THREADS - The number of clients that you want to run the program against.
    • NETWORKSERVER_PORT - The port on which the network server is running.
  • NsSampleClientThread.java

    This file contains two Java classes:

    • The NsSampleClientThread class extends Thread and instantiates a NsSampleWork instance.
    • The NsSampleWork class contains everything required to perform DML operations using JDBC calls. The doWork method in the NsSampleWork class represents all the work done as part of this sample program.
  • NetworkServerUtil.java

    This file contains helper methods to start the Network Server and to shutdown the server.

  • Compiled class files:
    • NsSample.class
    • NsSampleClientThread.class
    • NsSampleWork.class
    • NetworkServerUtil.class

Running the NsSample sample program

To run the NsSample program:

  1. Open a command prompt and change directories to the %DERBY_INSTALL%\demo\programs\ directory, where %DERBY_INSTALL% is the directory where you installed Derby.
  2. Set the CLASSPATH to the current directory (".") and also include the following jar files in order to use the Network Server and the DB2 JDBC Universal Driver.
    • derbynet.jar

      The Network Server jar file. It must be in your CLASSPATH to use any of the Network Server functions.

    • db2jcc.jar

      This jar file must be in your CLASSPATH to use the DB2 JDBC Universal Driver.

    • db2jcc_license_c.jar

      This jar file is the license file for the Universal Driver.

    • 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 as well as the DB2 JDBC Universal Driver along with their respective versions.

  4. After you have set up your environment correctly, execute the application from the same directory:

    java  nserverdemo.NsSample
     
    

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

Derby Network Server created
Server is ready to accept connections on port 1621.
Connection number: 1.
[NsSample] Derby Network Server started.
[NsSample] Sample Derby Network Server program demo starting.
Please wait .....................
Connection number: 2.
[NsSampleWork] Begin creating table - SAMPLETBL and necessary indexes.
[NsSampleClientThread] Thread id - 1; started.
[NsSampleWork] Thread id - 1; requests database connection, 
    dbUrl =jdbc:derby:NSSampledb;
[NsSampleClientThread] Thread id - 2; started.
[NsSampleWork] Thread id - 2; requests database connection, 
    dbUrl =jdbc:derby:net://localhost:1621/
    NSSampledb;create=true:retrieveMessagesFromServerOnGetMessage=true;
    deferPrepares=true;
Connection number: 3.
[NsSampleWork] Thread id - 1 selected 1 row [313,Derby36   ,1.7686243E23,9620]
[NsSampleWork] Thread id - 1 selected 1 row [313,Derby36   ,1.7686243E23,9620]
[NsSampleWork] Thread id - 1; deleted 1 row with t_key = 9620
[NsSampleWork] Thread id - 1 selected 1 row [700,Derby34   ,8.7620301E9,9547]
[NsSampleWork] Thread id - 1 selected 1 row [700,Derby34   ,8.7620301E9,9547]
[NsSampleWork] Thread id - 2 selected 1 row [700,Derby34   ,8.7620301E9,9547]
[NsSampleWork] Thread id - 2 selected 1 row [700,Derby34   ,8.7620301E9,9547]
[NsSampleWork] Thread id - 1; inserted 1 row.
[NsSampleWork] Thread id - 1 selected 1 row [52,Derby34   ,8.7620301E9,9547]
[NsSampleWork] Thread id - 2; updated 1 row with t_key = 9547
[NsSampleWork] Thread id - 1; deleted 1 row with t_key = 9547
[NsSampleWork] Thread id - 2 selected 1 row [617,Derby31   ,773.83636,9321]
[NsSampleWork] Thread id - 2 selected 1 row [617,Derby31   ,773.83636,9321]
[NsSampleWork] Thread id - 2 selected 1 row [617,Derby31   ,773.83636,9321]
[NsSampleWork] Thread id - 2 selected 1 row [617,Derby31   ,773.83636,9321]
[NsSampleWork] Thread id - 1; inserted 1 row.
[NsSampleWork] Thread id - 2; deleted 1 row with t_key = 9321
[NsSampleWork] Thread id - 1; deleted 1 row with t_key = 8707
[NsSampleWork] Thread id - 1; closed connection to the database.
[NsSampleClientThread] Thread id - 1; finished all tasks.
[NsSampleWork] Thread id - 2; deleted 1 row with t_key = 8490
[NsSampleWork] Thread id - 2; closed connection to the database.
[NsSampleClientThread] Thread id - 2; finished all tasks.
[NsSample] Shutting down network server.
Connection number: 4.
Shutdown successful.
 

Running the NsSample program will also create the following new directories and files:

  • NSSampledb

    This directory makes up the NSSampledb database.

  • derby.log

    This log file contains Derby progress and error messages.


Previous Page
Next Page
Table of Contents
Index