Project

General

Profile

Sample-jta-plain » History » Version 10

Henning Blohm, 16.09.2012 17:07

1 3 Henning Blohm
h1. Using Z2 with a Transaction Manager
2 1 Henning Blohm
3 2 Henning Blohm
(Work in progress)
4 1 Henning Blohm
5 2 Henning Blohm
This sample demonstrates how to use Z2 with the Atomikos transaction manager. There is another sample that shows how to use Z2 with a Spring configured transaction manager. See [[sample-jta-spring]]. In contrast to that, this sample is less sophisticated but probably better suited to understand the underlying mechanics.
6 1 Henning Blohm
7
The Wiki page [[How_to_TransactionManager]] explains the general principles behind transaction handling in Z2.
8 2 Henning Blohm
9
This sample is stored in "z2-samples.jta-plain":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-jta-plain.
10
11
h2. Pre-Requisites
12
13
This sample assumes the presence of a MySQL database system on localhost with a database called "z_tx_tests" for which a user "tx" with password "tx" has full permissions. If you have a running MySQL server these SQL commands do the trick:
14
15
<pre><code class="SQL">
16
create database z_tx_tests;
17
grant all on z_tx_tests.* to tx@localhost identified by 'tx';
18
</code></pre>
19
20
h2. Running the sample
21
22
This sample is run as explained in [[How to run a sample]]. The 5 minutes version:
23
24
<pre><code class="ruby">
25
mkdir install
26
cd install 
27
git clone -b master http://git.z2-environment.net/z2-base.core
28
git clone -b master http://git.z2-environment.net/z2-samples.jta-plain
29
30
# on Linux / Mac OS:
31
cd z2-base.core/run/bin
32
./gui.sh
33
34
# on Windows:
35
cd z2-base.core\run\bin
36
gui.bat
37 1 Henning Blohm
</code></pre>
38 3 Henning Blohm
39
h2. Verifying
40
41
When everything is up, go to http://localhost:8080/tx.plain. You see something like this: 
42
43
!plain_thingies.png!
44
45 5 Henning Blohm
And indeed, the implemented function is rather obvious: Manage a table of strings - called Thingies in the sample.
46 3 Henning Blohm
47
h2. Now to the point...
48 4 Henning Blohm
49 1 Henning Blohm
The idea behind this sample is to provide a template for transaction manager integration. Note that before you integrate with a complete transaction management system, you should be sure that you need it. Distributed transactions come at a price in terms of complexity and overhead of local transaction logs. If you do not have multiple data sources, or if those have no strong inherent consistency requirement you may well be better off without a full-blown, XA capable transaction manager. Please also see [[How to transaction management]].
50 5 Henning Blohm
51 6 Henning Blohm
h2. Detailed explanation
52 5 Henning Blohm
53
The sample has four modules:
54
55 7 Henning Blohm
* *com.atomikos*: This module holds the "Atomikos Transaction Essentials":http://www.atomikos.com/Main/TransactionsEssentials libraries - the actual transaction manager implementation* 
56
* *com.zfabrik.samples.jta-plain*: This module implements supporting functions for the actual applications.
57
* *com.zfabrik.samples.jta-plain.domain*: The domain definition module. This contains the JPA entities and a "Thingy Repository" implementation.
58
* *com.zfabrik.samples.jta-plain.web*: The simple web application that lists the database content and allows to add and delete thingies.
59 6 Henning Blohm
60
61 8 Henning Blohm
Let's start by looking at the supporting functions. Note that the Spring Framework as used in the other sample [[Sample.jta-spring]] implements these supportive functions.
62 1 Henning Blohm
63 8 Henning Blohm
We only care about database transactions here. The typical issues to be tackled when working with database connections are
64
65 1 Henning Blohm
h4. Provide control over transaction scopes
66 8 Henning Blohm
67
As we use the Java Transaction API ("JTA":http://de.wikipedia.org/wiki/Java_Transaction_API) the "UserTransaction":http://docs.oracle.com/javaee/6/api/javax/transaction/UserTransaction.html interface is what matters. Atomikos implements it, but it is in our responsibility 
68 6 Henning Blohm
69
h4. One connection per transaction scope and datasource
70
71
h4. One entity manager per transaction scope and persistence unit
72 9 Henning Blohm
73
74
INTEGRATION POINTS:
75
76 10 Henning Blohm
* Jetty Transaction (see e.g. http://docs.codehaus.org/display/JETTY/JNDI)