Network Server sample programs for embedded and client connections
Network Server sample programs for embedded and client connections
This Derby Network Server sample program demonstrates how to obtain an embedded connection and client connections to the same database using the Network Server. This program shows how to use either the DriverManager or a DataSource to obtain client connections.
For a database to be consistent, only one JVM can access it at a time. The embedded driver is loaded when the Network Server is started. The JVM that starts the Network Server can get an embedded connection to the same database that the Network Server is accessing to serve clients from other JVMs. This solution allows you to take advantage of the performance benefits of the embedded driver while also allowing client connections from other JVMs to connect to the same database.
This topic contains the following sections:
The SimpleNetworkServerSample Program
The SimpleNetworkServerSample program starts the Derby Network Server as well as the embedded driver and waits for clients to connect. The program performs the following tasks.
- starts the Derby Network Server using a property and also loads the embedded driver
- checks if the Network Server is up and running
- creates the database NSSimpleDB if it is not already created
- obtains an embedded database connection
- tests the database connection by executing a sample query.
- allows client connections to connect to the server until you decide to stop the server and exit the program
- closes the connection
- shuts down the Network Server before exiting the program.
To run the sample program, the following files should be installed in the %DERBY_INSTALL%\demo\programs\nserverdemo\ directory:
- The source file: SimpleNetworkServerSample.java
- The compiled class file: SimpleNetworkServerSample.class
Running the SimpleNetworkServerSample Program
To run the Derby Network Server sample program:
- 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.
- 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 because we start the Network Server in this program.
-
derby.jar
The database engine jar file.
- 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.
- Once you have set up your environment correctly, execute the application from the same 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:net:\\localhost:1527\ org.apache.derby.tools.ij ij> connect 'NSSimpleDB:retrieveMessagesFromServerOnGetMessage=true;'; Watch that punctuation. Put a ':' before the jcc attributes and a ';' after each one (even the last). Clients can continue to connect: Press [Enter] to stop Server
Running the SimpleNetworkServerSample program will also create the following new directories and files:
-
NSSimpleDB
This directory makes up the NSSimpleDB database.
-
derby.log
This log file contains Derby progress and error messages.
Connecting a Client to the Network Server with the SimpleNetworkClientSample program
The SimpleNetworkClientSample program is a client program that interacts with the Derby Network Server from another JVM. The program performs the following tasks.
- loads the DB2 Universal JDBC Driver
- obtains a client connection using the DriverManager
- obtains a client connection using a DataSource
- tests the database connections by executing a sample query
- 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 the sample program:
- The source file: SimpleNetworkClientSample.java
- The compiled class file: SimpleNetworkClientSample.class
Running the SimpleNetworkClientSample Program
To connect to the Network Server that has been started as part of the SimpleNetworkServerSample program:
- 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.
- Network Server clients need only the following jar files in the CLASSPATH to connect to the Network Server. Set the CLASSPATH to include the following jar files:
- the current directory (".")
-
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 for Derby.
- Once you have set up your environment correctly, execute the application from the same directory:
java SimpleNetworkClientSample
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 = IBM DB2 JDBC Universal Driver Architecture 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!
Previous Page
Next Page
Table of Contents
Index