So, What About the Derby Code?
Let's now take a look at the source code required to run a Derby server and client.
Derby Server Startup
Listing 4 illustrates the startup code for the server and the acquisition of an embedded connection.
Listing 4 Starting Up the Server Program
System.out.println("Starting Network Server"); System.setProperty("derby.drda.startNetworkServer","true"); // Booting derby Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Next comes a long wait while the program tests to see if the server is up and running, as illustrated in Listing 5. To reduce clutter, I've removed the exception handling code, but as you can see it's just a loop that contains a five-second delay (via a call to sleep(5000)) and a call to server.ping().
Listing 5 Checking If the Server Is Running
server = new NetworkServerControl(); System.out.println("Testing if Network Server is up and running!"); for (int i = 0; i < 10 ; i ++) { Thread.currentThread().sleep(5000); server.ping(); }
As mentioned in Listing 1, you might notice a delay of a few minutes while the code in Listing 5 executes. Assuming all is well, the next step is getting an embedded connection to the database server, as illustrated in Listing 6.
Listing 6 Connecting To and Testing the Server
embeddedConn = getEmbeddedConnection(DBNAME,"create=true;"); System.out.println("Got an embedded connection."); System.out.println("Testing embedded connection by executing a sample query "); // test connections by doing some work test(embeddedConn); // print how to connect to the network server using ij String howToConnect = ijUsage(); System.out.println(howToConnect); waitForExit();
The first line in Listing 6 gets an embedded connection and also creates the database associated with the symbolic constant DBNAME. Once this line executes, you should see a new folder appearing in the nserverdemo directory with the same name as the value of the constant DBNAME. Notice in Listing 6 the line test(embeddedConn). This method consists of the code in Listing 7.
Listing 7 Testing the Database Connection
stmt = conn.createStatement(); rs = stmt.executeQuery("select count(*) from sys.systables"); while(rs.next()) System.out.println("number of rows in sys.systables = "+ rs.getInt(1));
The code in Listing 7 executes an SQL select from the system catalog tables. This avoids the need to create any tables in the database, because the system catalog tables are always present. Once this test completes, the server is ready to accept client connections. How do clients connect?
A Client Connection
To run the client, you execute the commands in Listing 2, which results in the code illustrated in Listing 8.
Listing 8 The Client Program
System.out.println("Starting Sample client program "); parseArguments(args); loadDriver(); clientConn1 = getClientDriverManagerConnection(); javax.sql.DataSource myDataSource = getClientDataSource(DBNAME, null, null); clientConn2 = getClientDataSourceConn(myDataSource); System.out.println("Please press enter to test the Derby connection."); BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); System.out.println(stdin.readLine()); test(clientConn1); System.out.println("Testing the connection obtained via a DataSource by executing a sample query "); test(clientConn2); System.out.println("Goodbye!");
If you read my earlier article, the code in Listing 8 is familiar territory, and you can see that the method names very clearly reflect what's going on (this is a good practice in general because it makes it easier to understand source code, particularly code written by others). The code in Listing 8 carries out the following tasks:
- Parsing program command line arguments
- Loading the Derby device driver
- Getting a connection instance
- Creating a data source
- Testing the first client database connection
- Testing the second client database connection
- Program exit
Because this is a network database server, you can run multiple clients and verify that the server scales up from one to many clients. When you get a bit bored running clients, you can follow the program instructions to close the clients and server down.