Sample DB Worker Node

This sample illustrates the usage of the DB_Worker_Node_Add-on and thereby how easy it is to setup a all-in-one-system comprising the z2 environment and the database. The sample web-application has a very simple database schema containing one table with a primary key and a varchar column while the UI provides just the basic CRUD actions.

The sample is provided via the repository z2-samples-dbworker.

Prerequisites

Z2 has the following Java Version requirements

Z2 Version Min. Java version required Max Java version supported
2.1 - 2.3.1 Java 6 Java 7
2.4 - master Java 8 Java 8

Note: Most samples suggest to use the master branch. You may choose another version branch (please check the respective repository).
Make sure you have a corresponding Java Development Kit (JDK) or Java Runtime Environment (JRE) installed. If in doubt, go to Download Java SE.

Note: Running v2.1-v2.3.1 on Java 8 is supported by specifying

com.zfabrik.java.level=7

(or 6, if that is your desired compilation language level) in <home>/run/bin/runtime.properties. By this the Java compiler version detection does not fall back to a lower level.

Setting up the sample

There is no further pre-requisite to running this sample, and you may proceed as described in How to run a sample. Here's the really fast version:

mkdir install
cd install 
git clone -b master http://git.z2-environment.net/z2-base.core
git clone -b master http://git.z2-environment.net/z2-samples.dbworker

# on Linux / Mac OS:
cd z2-base.core/run/bin
./gui.sh

# on Windows:
cd z2-base.core\run\bin
gui.bat

The first time you launch the sample will take a while. Watch the log output and keep an eye out for the following lines:

4/03 15:50:52 [33]...ment/h2Worker@0.2 [800]: 04/03 15:50:52 [1]...2DatabaseLauncher [800]: H2 TCP Server running at tcp://192.168.178.28:9092 (only local connections allowed)
04/03 15:50:52 [32]...ment/h2Worker@0.1 [800]: SQLState(90028) vendor code(90028)
04/03 15:50:52 [33]...ment/h2Worker@0.2 [800]: 04/03 15:50:52 [1]...2DatabaseLauncher [800]: H2 Console Server running at http://192.168.178.28:8082 (only local connections allowed)
04/03 15:50:52 [33]...ment/h2Worker@0.2 [800]: 04/03 15:50:52 [1]...stemStateResource [800]: Attained system state: com.zfabrik.db.environment/h2WorkerUp
04/03 15:50:52 [33]...ment/h2Worker@0.2 [800]: 04/03 15:50:52 [1]...worker.WorkerSoul [800]: Completed worker process initialization

If you want to inspect the code using Eclipse, please create a workspace in install (i.e. install/workspace) and import the Git repositories and the following projects into your workspace: core from z2-base.core, environemnt and com.zfabrik.samples.dbworker.webapp from z2-samples.dbworker (see also First steps).

Run the sample app

run http://localhost:8080/dbworker_sample and play around with the UI. You can create new entries, modify and update existing entries by clicking inside the name column and delete entries. The sample application itself is not that tricky. It uses the JDBC API in order to create the schema on demand as well as to implement the CRUD operations. A single jsp file provides the UI.

How to switch to Derby

By default the sample uses the H2 RDBMS. In order to use Apache Derby you have to change the following file:

  1. in environment/home.properties:
    replace the line
    home.workers = com.zfabrik.db.environment/h2Worker, environment/webWorker
    by
    home.workers = com.zfabrik.db.environment/derbyWorker, environment/webWorker
  2. in environment/systemDB.properties:
    you have to enable the Derby related ds.prop.* settings and to disable the related H2 settings:
    #
    # Java DB (Derby)
    #
    ds.prop.driverClass=org.apache.derby.jdbc.ClientDriver
    ds.prop.url=jdbc:derby://localhost/${var.dataBaseName};create=true
    
    #
    # H2
    # jdbc:h2:tcp://<server>[:<port>]/[<path>]<databaseName>
    # see http://www.h2database.com/html/features.html#database_url
    #
    #ds.prop.driverClass=org.h2.Driver
    #ds.prop.url=jdbc:h2:tcp://localhost/${var.dataBaseName}
    

After restart the z2-Environment will run with the Apache Derby database. The sample app displays the database product name so you can easily check what database is active.

How to use the DB Worker Add-on for own projects?

The environment project of the sample contains the important pieces: The z2 repository component dbWorkerAddon.properties makes the DB Worker Node Add-on available to the z2-Environment:

environment/dbWorkerAddon.properties:

...
gitcr.uri=http://git.z2-environment.net/z2-addons.dbworker
...

Furthermore the home layout defined in home.properties launches the database worker in addition to the web worker:

environment/home.properties:

com.zfabrik.component.type=com.zfabrik.homeLayout
home.workers = com.zfabrik.db.environment/h2Worker, environment/webWorker

Note that com.zfabrik.db.environment/h2Worker precedes environment/webWorker so that the DB is up and running when before the web applications are started.