Project

General

Profile

Sample-DB-worker-node » History » Version 6

Udo Offermann, 03.04.2013 16:13

1 3 Udo Offermann
h1. Sample DB Worker Node
2 2 Udo Offermann
3
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.
4
5
The sample is provided via the repository "z2-samples-dbworker":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-dbworker.
6
7
h2. Prerequisites
8
9
All you need is a JDK 6 distribution as described in [[Step_2_-_Install_and_run_in_5_minutes]]. 
10
11
h2. Setting up the sample
12
13 3 Udo Offermann
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:
14 1 Udo Offermann
15 3 Udo Offermann
<pre><code class="ruby">
16
mkdir install
17
cd install 
18
git clone -b master http://git.z2-environment.net/z2-base.core
19
git clone -b master http://git.z2-environment.net/z2-samples.dbworker
20 1 Udo Offermann
21 3 Udo Offermann
# on Linux / Mac OS:
22
cd z2-base.core/run/bin
23
./gui.sh
24 1 Udo Offermann
25 3 Udo Offermann
# on Windows:
26
cd z2-base.core\run\bin
27
gui.bat
28
</code></pre>
29 2 Udo Offermann
30 3 Udo Offermann
The first time you launch the sample will take a while. Watch the log output and keep an eye out for the following lines:
31 2 Udo Offermann
<pre>
32 3 Udo Offermann
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)
33
04/03 15:50:52 [32]...ment/h2Worker@0.1 [800]: SQLState(90028) vendor code(90028)
34
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)
35
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
36
04/03 15:50:52 [33]...ment/h2Worker@0.2 [800]: 04/03 15:50:52 [1]...worker.WorkerSoul [800]: Completed worker process initialization
37 1 Udo Offermann
</pre>
38
39 3 Udo Offermann
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 [[Step_3_-_First_steps_with_Z2_on_Git|First steps]]).
40
41 2 Udo Offermann
h3. Run the sample app
42
43
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. 
44
45 6 Udo Offermann
h3. How to switch to Derby
46
47
By default the sample uses the H2 RDBMS. In order to use Apache Derby you have to change the following file:
48
49
# in @environment/home.properties@: 
50
  replace the line
51
  @home.workers = com.zfabrik.db.environment/h2Worker, environment/webWorker@ 
52
  by 
53
  @home.workers = com.zfabrik.db.environment/derbyWorker, environment/webWorker@
54
55
# in @environment/systemDB.properties@ you have to enable the Derby related @ds.prop.*@ settings and to disable the related H2 settings:
56
<pre>
57
#
58
# Java DB (Derby)
59
#
60
ds.prop.driverClass=org.apache.derby.jdbc.ClientDriver
61
ds.prop.url=jdbc:derby://localhost/${var.dataBaseName};create=true
62
63
#
64
# H2
65
# jdbc:h2:tcp://<server>[:<port>]/[<path>]<databaseName>
66
# see http://www.h2database.com/html/features.html#database_url
67
#
68
#ds.prop.driverClass=org.h2.Driver
69
#ds.prop.url=jdbc:h2:tcp://localhost/${var.dataBaseName}
70
</pre>
71
72 4 Udo Offermann
h2. How to use the DB Worker Add-on for own projects?
73 2 Udo Offermann
74 3 Udo Offermann
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. Furthermore the _home layout_ defined in @home.properties@ launches the database worker in addition to the web worker:
75
76
<pre>
77
com.zfabrik.component.type=com.zfabrik.homeLayout
78
home.workers = com.zfabrik.db.environment/h2Worker, environment/webWorker
79
</pre>
80
81
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.