| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 18 |
<HTML lang="en"> |
| 19 |
<HEAD> |
| 20 |
<TITLE>Derby Network Server sample program</TITLE> |
| 21 |
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1"> |
| 22 |
</HEAD> |
| 23 |
<BODY> |
| 24 |
<H2><A name=Top_Of_Page></A><A name=nsrun></A><A name=nssample></A>Derby |
| 25 |
Network Server sample program</H2> |
| 26 |
<P>The Network Server sample demo program (NsSample) is a simple JDBC application that |
| 27 |
interacts with the Derby Network Server. The program:</P> |
| 28 |
<OL> |
| 29 |
<LI>Starts the Derby Network Server.</LI> |
| 30 |
<LI>Connects to the Network Server and creates the database if not already |
| 31 |
created. The Network Client driver is loaded automatically.</LI> |
| 32 |
<LI>Checks to see if the schema is already created, and if not, </LI> |
| 33 |
<LI>Creates the schema, which includes the <tt>SAMPLETBL</tt> table and |
| 34 |
corresponding indexes.</LI> |
| 35 |
<LI>Connects to the database.</LI> |
| 36 |
<LI>Loads the schema by inserting data.</LI> |
| 37 |
<LI>Starts client threads to perform database related operations.</LI> |
| 38 |
<LI>Has each of the clients perform DML operations (select, insert, delete, |
| 39 |
update) using JDBC calls; in particular, one client opens an embedded |
| 40 |
connection to perform database operations, while the other client opens a |
| 41 |
client connection to the Derby Network Server to perform database |
| 42 |
operations.</LI> |
| 43 |
<LI>Waits for the client threads to finish the tasks.</LI> |
| 44 |
<LI>Shuts down the Derby Network Server at the end of the demo.</LI> |
| 45 |
</OL> |
| 46 |
<P>The following files in the |
| 47 |
<TT><i>DERBY_INSTALL</i>\demo\programs\nserverdemo\</TT> directory are needed to |
| 48 |
run the sample program (<i>DERBY_INSTALL</i> is the directory where you |
| 49 |
installed Derby):</P> |
| 50 |
<UL> |
| 51 |
<LI><TT>NsSample.java</tt><br> |
| 52 |
This is the entry point into the demo application. The program starts up two clients. One client opens an embedded |
| 53 |
connection to perform database operations, while the other client opens a client |
| 54 |
connection to the Derby Network Server to perform database operations. This program provides the following constants that |
| 55 |
can be changed to modify the sample demo: |
| 56 |
<UL> |
| 57 |
|
| 58 |
<LI><tt>NUM_ROWS</tt> - The number of rows that must be initially loaded into the schema.</li> |
| 59 |
<LI><tt>ITERATIONS</tt> - The number of iterations for which each client thread does database related work.</LI> |
| 60 |
<LI><tt>NUM_CLIENT_THREADS</tt> - The number of clients that you want to run the program against.</li> |
| 61 |
<LI><tt>NETWORKSERVER_PORT</tt> - The port on which the network server is running.</LI> |
| 62 |
</UL> |
| 63 |
|
| 64 |
You can also modify the program to exclude starting the Network Server as part of the sample program. |
| 65 |
Make sure to recompile the Java files before running the program if you make any |
| 66 |
changes to the source code. |
| 67 |
|
| 68 |
</li> |
| 69 |
|
| 70 |
<li><tt>NsSampleClientThread.java</tt></br> |
| 71 |
This file contains two Java classes: |
| 72 |
<UL> |
| 73 |
<LI>The <tt>NsSampleClientThread</tt> class extends Thread and does all the |
| 74 |
necessary work by instantiating a <tt>NsSampleWork</tt> instance.</li> |
| 75 |
<LI>The <tt>NsSampleWork</tt> class contains everything required to perform |
| 76 |
DML operations using JDBC calls. The <tt>doWork</tt> method in |
| 77 |
the <tt>NsSampleWork</tt> class represents all the work done as |
| 78 |
part of this sample program.</LI> |
| 79 |
</UL> |
| 80 |
</li> |
| 81 |
<LI><tt>NetworkServerUtil.java</tt><br> |
| 82 |
This file contains helper methods to start the Derby Network Server and to shut down the server. |
| 83 |
</li> |
| 84 |
<LI>Compiled class files: |
| 85 |
<UL> |
| 86 |
<LI><tt>NsSample.class</tt></li> |
| 87 |
<LI><tt>NsSampleClientThread.class</tt></li> |
| 88 |
<LI><tt>NetworkServerUtil.class</tt></li> |
| 89 |
</UL> |
| 90 |
</li> |
| 91 |
</ul> |
| 92 |
|
| 93 |
|
| 94 |
<H2>Running the Network Server demo program</H2> |
| 95 |
<P>To run the Derby Network Server demonstration program:</P> |
| 96 |
<OL> |
| 97 |
<LI>Open a command prompt and change directories to the <tt><i>DERBY_INSTALL</i>\demo\programs\</tt> directory. |
| 98 |
<LI>Set the CLASSPATH to the current directory (".") and also include the following |
| 99 |
jar files in order to use the Derby Network Server and the Derby Client Network JDBC Driver. |
| 100 |
<ul> |
| 101 |
<li><tt>derbynet.jar</tt><br>The Network Server jar file. It must be in your CLASSPATH to use any of the Derby Network |
| 102 |
Server functions.</li> |
| 103 |
<li><tt>derbyclient.jar</tt><br>This jar file must be in your CLASSPATH to use the Derby Client Network JDBC Driver.</li> |
| 104 |
<li><tt>derby.jar</tt><br>The Derby database engine jar file.</li> |
| 105 |
</ul> |
| 106 |
</LI> |
| 107 |
<li>Test the CLASSPATH settings by running the following java command:<pre>java org.apache.derby.tools.sysinfo</pre>This command will show the Derby jar files that are in the CLASSPATH along with their respective versions.</li> |
| 108 |
<li>Once you have set up your environment correctly, execute the application from the <br> |
| 109 |
<tt><i>DERBY_INSTALL</i>\demo\programs\</tt> directory:<br> |
| 110 |
<pre>java nserverdemo.NsSample [driverType]</pre> |
| 111 |
<ul> |
| 112 |
<li>Where the possible values for driverType are: |
| 113 |
<ul> |
| 114 |
<li>derbyClient (default) |
| 115 |
</ul> |
| 116 |
</ul> |
| 117 |
</li> |
| 118 |
</OL> |
| 119 |
You should receive output similar to the following if the |
| 120 |
program runs successfully:<br><br> |
| 121 |
|
| 122 |
<tt> |
| 123 |
Using JDBC driver: org.apache.derby.jdbc.ClientDriver<br> |
| 124 |
Derby Network Server created<br> |
| 125 |
Apache Derby Network Server started and ready to accept connections on port 1621<br> |
| 126 |
[NsSample] Derby Network Server started.<br> |
| 127 |
[NsSample] Sample Derby Network Server program demo starting. <br> |
| 128 |
Please wait .....................<br> |
| 129 |
[NsSampleWork] Begin creating table - SAMPLETBL and necessary indexes.<br> |
| 130 |
[NsSampleClientThread] Thread id - 2; started.<br> |
| 131 |
[NsSampleWork] Thread id - 2; requests database connection, dbUrl =jdbc:derby://localhost:1621/NSSampledb;create=true;<br> |
| 132 |
[NsSampleClientThread] Thread id - 1; started.<br> |
| 133 |
[NsSampleWork] Thread id - 1; requests database connection, dbUrl =jdbc:derby:NSSampledb;<br> |
| 134 |
[NsSampleWork] Thread id - 1; inserted 1 row.<br> |
| 135 |
[NsSampleWork] Thread id - 1; inserted 1 row.<br> |
| 136 |
[NsSampleWork] Thread id - 1; deleted 1 row with t_key = 9372<br> |
| 137 |
[NsSampleWork] Thread id - 1 selected 1 row [920,Derby50 ,951.7808,9216]<br> |
| 138 |
[NsSampleWork] Thread id - 1 selected 1 row [920,Derby50 ,951.7808,9216]<br> |
| 139 |
[NsSampleWork] Thread id - 1; inserted 1 row.<br> |
| 140 |
[NsSampleWork] Thread id - 1 selected 1 row [920,Derby50 ,951.7808,9216]<br> |
| 141 |
[NsSampleWork] Thread id - 1; deleted 1 row with t_key = 9216<br> |
| 142 |
[NsSampleWork] Thread id - 1 selected 1 row [824,Derby26 ,8.802546E22,9155]<br> |
| 143 |
[NsSampleWork] Thread id - 1; updated 1 row with t_key = 9155<br> |
| 144 |
[NsSampleWork] Thread id - 1; closed connection to the database.<br> |
| 145 |
[NsSampleClientThread] Thread id - 1; finished all tasks.<br> |
| 146 |
[NsSampleWork] Thread id - 2; updated 0 row with t_key = 9372<br> |
| 147 |
[NsSampleWork] Thread id - 2; updated 1 row with t_key = 9155<br> |
| 148 |
[NsSampleWork] Thread id - 2 selected 1 row [56,Derby26 ,8.802546E22,9155]<br> |
| 149 |
[NsSampleWork] Thread id - 2; inserted 1 row.<br> |
| 150 |
[NsSampleWork] Thread id - 2; updated 1 row with t_key = 9155<br> |
| 151 |
[NsSampleWork] Thread id - 2; deleted 1 row with t_key = 9155<br> |
| 152 |
[NsSampleWork] Thread id - 2 selected 1 row [785,Derby2 ,0.30170244,8280]<br> |
| 153 |
[NsSampleWork] Thread id - 2 selected 1 row [785,Derby2 ,0.30170244,8280]<br> |
| 154 |
[NsSampleWork] Thread id - 2; updated 1 row with t_key = 8280<br> |
| 155 |
[NsSampleWork] Thread id - 2 selected 1 row [59,Derby2 ,0.30170244,8280]<br> |
| 156 |
[NsSampleWork] Thread id - 2; closed connection to the database.<br> |
| 157 |
[NsSampleClientThread] Thread id - 2; finished all tasks.<br> |
| 158 |
[NsSample] Shutting down network server.<br> |
| 159 |
Apache Derby Network Server - shutdown<br> |
| 160 |
[NsSample] End of Network server demo.<br> |
| 161 |
<br> |
| 162 |
</tt> |
| 163 |
|
| 164 |
<P>Running the demo program will also create new directories and files:</P> |
| 165 |
<UL> |
| 166 |
<LI><A name=Bot_Of_Page></A><tt>NSSampledb</tt><br>This directory makes up the |
| 167 |
NSSampledb database.</li> |
| 168 |
<LI><tt>derby.log</tt><br>This log file contains Derby progress and error messages.</li> |
| 169 |
</ul> |
| 170 |
|
| 171 |
|
| 172 |
</BODY> |
| 173 |
</HTML> |