Apache Derby Network Server

From Salespoint 5
Revision as of 19:21, 22 November 2011 by Martin Morgenstern (Talk | contribs)
Jump to: navigation, search

This tutorial shows how to setup the Apache Derby Database as a Network Server and how to configure Eclipse and your JPA project accordingly.

Contents

Introduction

Derby has two operation modes: Embedded and Network Server. Using the former one, only one Java Virtual Machine (JVM) at a time can access the database (if you are reading this tutorial, you probably have come across this issue during the practical software engineering course). The latter one allows more than one JVM, i. e. more than one program at a time to connect to the database using TCP networking. In this way, it is possible to test an application that uses a particular Derby database and at the same time leave the connection in the Eclipse Data Source Explorer open.

Part I: Setup and start the server

The following steps are valid for Windows, Linux and Mac OS X.

Installation

Download and unzip the latest official Derby release (currently db-derby-10.8.2.2-bin.zip) from http://db.apache.org/derby/derby_downloads.html into a directory of your choice.

Start

The Derby Network Server is started via the command line. First, change to the directory where you have unzipped the file from the previous step.

$ cd path/to/db-derby-10.8.2.2-bin/lib

The actual start command is:

$ java -jar derbyrun.jar server start
Tue Nov 22 19:36:54 CET 2011 : Security manager installed using the Basic server security policy.
Tue Nov 22 19:36:55 CET 2011 : Apache Derby Network Server - 10.8.2.2 - (1181258) started and ready to accept connections on port 1527

If everything is alright, the server starts and shows a message similar to this one in the command-line window. As you can see, it is listening for incoming connections on port 1527.

Note: You can shutdown the server using the keyboard combination Ctrl-C.

Part II: Configuring Eclipse and your JPA project

Eclipse Data Source Explorer

  1. Open the Eclipse Data Source Explorer View.
  2. Right-click Database Connections and select New... to create a new Database Connection Profile.
  3. Select Derby as Connection Profile Type and enter a meaningful name for the connection, e.g. Derby Network Server. Then proceed to the next page.
  4. If you can select Derby Client JDBC Driver from the combo-box, do that and skip the next step. Else we will create this driver in the next step.
  5. Click the blue symbol next to the combo-box to create a new driver definition. From the list of driver templates, select Derby Client JDBC Driver. On the tab JAR List, enter the correct path to the lib/derbyclient.jar file inside the directory you have unzipped in the first part of this tutorial. Then click OK.
  6. In the General tab:
    1. Enter a meaningful name for the database.
    2. Provide an arbitrary, non-empty username and password for the connection.
    3. Ensure that the host is localhost and the port is 1527.
    4. Ensure that the option Create database (if required) is checked.
  7. Click on Test Connection to see if the setup is correct. Then click Finish.

As a result, you should see the server as a new entry in the Data Source Explorer view.

Configuring the Persistence Unit

To use the new connection inside your Java (web) project, the settings for the persistence unit (inside persistence.xml) must also be updated.

  1. Open the persistence.xml file for your specific project.
  2. Inside the tab Connection, click Populate from connection... and select the server you have created before.
  3. Save the file.

Now, you should be able to program your JPA application as usual, but with the benefit of maintaining multiple connections to one database from different programs.

Tip: Using a SQL scrapbook page and the SQL results view, you can easily browse the database from inside Eclipse using SQL queries.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox