This section demonstrates the ease with which a program that embeds Derby can be modified for a client/server implementation using the Derby Network Server. A Derby client program, WwdClient.java, is created by changing a few lines of the WwdEmbedded.java program. The client program can be run in multiple command shells allowing simultaneous update from two or more sources.
copy WwdEmbedded.java WwdClient.java
cp WwdEmbedded.java WwdClient.java
Original declaration public class WwdEmbedded New declaration public class WwdClient
Original definitions String driver = "org.apache.derby.jdbc.EmbeddedDriver"; String dbName="jdbcDemoDB"; String connectionURL = "jdbc:derby:" + dbName + ";create=true"; New definitions String driver = "org.apache.derby.jdbc.ClientDriver"; ... String connectionURL = "jdbc:derby://localhost:1527/" + dbName + ";create=true";
That's all there is to it.
Before you run the WwdClient program, the Network Server needs to be started.
java -jar %DERBY_HOME%\lib\derbynet.jar start Apache Derby Network Server - 10.2.1.6 - (452058) started and ready to accept connections on port 1527 at 2006-09-22 00:56:33.091 GMT
java -jar $DERBY_HOME/lib/derbynet.jar start Apache Derby Network Server - 10.2.1.6 - (452058) started and ready to accept connections on port 1527 at 2006-09-22 00:56:33.091 GMT
java WwdClient org.apache.derby.jdbc.ClientDriver loaded. Connected to database jdbcDemoDB Enter wish-list item (enter exit to end): a sunny day _________________________________________________________ On 2006-09-21 15:11:50.412 I wished for a peppermint stick On 2006-09-21 15:12:47.024 I wished for an all expenses paid vacation On 2006-09-22 10:08:21.167 I wished for a sunny day ________________________________________________________ Enter wish-list item (enter exit to end): a new car ________________________________________________________ On 2006-09-21 15:11:50.412 I wished for a peppermint stick On 2006-09-21 15:12:47.024 I wished for an all expenses paid vacation On 2006-09-22 10:08:21.167 I wished for a sunny day On 2006-09-22 10:08:33.665 I wished for a new car ________________________________________________________ Enter wish-list item (enter exit to end): exit Closed connection Working With Derby JDBC program ending.
java -jar %DERBY_HOME%\lib\derbynet.jar shutdown Apache Derby Network Server - 10.2.1.6 - (452058) shutdown at 2006-09-22 19:13:51.445 GMT
java -jar $DERBY_HOME/lib/derbynet.jar shutdown Apache Derby Network Server - 10.2.1.6 - (452058) shutdown at 2006-09-22 19:13:51.445 GMT
In a client/server environment, the client program is often used from other computers on the network. Whenever a system accepts connections from other computers, there is a chance of abuse. To maintain security, the Derby Network Server defaults to accepting connections only from clients running on the local machine (localhost). Before this or any other Derby client program can access Network Server from another machine, additional steps should be taken to secure the Network Server environment. Once secured, the Network Server can be safely configured to accept connections from other machines. Refer to the Network Server security and Running the Network Server under the security manager sections of the Derby Server and Administration Guide for important information on securing the Network Server and enabling network connections.
With Network Server started, you can run the client program simultaneously in multiple windows. To demonstrate this, open two command windows and perform the substeps of the Run the client program step in each window. Both clients will operate without a problem. In contrast, it would not be possible for a program that uses the embedded driver (e.g. WwdEmbedded) to access the database until the database or the Network Server is shut down.
You may have noticed that the client program does not shut down the database. This is because the database is a shared resource in a client/server environment and, in most cases, should only be shut down when the Server is shut down. If multiple clients are accessing the database and one shuts down the database, the remaining clients will encounter a failure the next time they attempt an SQL command.
Derby's two architectures have caused confusion for some new Derby users. They mistakenly think that embedded is a single user configuration. This is not true. The embedded driver supports multiple simultaneous connections, performs locking, and provides performance, integrity and recoverability. Any application using the embedded driver can open multiple Derby connections and then provide a means for multiple users to interact with the database on each connection. The Derby Network Server is an example of such an application.