Project

General

Profile

Actions

Sample-jta-plain » History » Revision 16

« Previous | Revision 16/25 (diff) | Next »
Henning Blohm, 02.05.2014 09:48


Using Z2 with a Transaction Manager

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.

The Wiki page How_to_TransactionManager explains the general principles behind transaction handling in Z2.

This sample is stored in z2-samples.jta-plain. It implements exactly the same scenario as Sample-hibernate-basic with the exception of not using the built-in JTA implementation.

Prerequisites

You need to run Java DB as network server on localhost. This is explained next.

The application will create a database "z2-samples"

Running a Java DB Network Server

Previously to Java 9, the Java SE Development Kit (JDK) by Oracle provided the Java DB - essentially the same as the Apache Derby DB. That is not the case anymore. However, we use that Database implementation in our samples. In order to run those samples that illustrate use of a relational database, please follow the instructions below to install and run Apache Derby. Could hardly be simpler.

Step 1: Download and Install

Unless you have done so already, download Apache Derby DB and follow the installation how-to.

Note: You do not need to unpack Apache Derby into some global folder on your system. Instead you may want to use some local folder under your user's home folder. There is no problem installing and runnning different instances and configurations at any time.

Step 2: Run

Let's assume you installed (well - unpacked) into a folder $DERBY_INSTALL. Also, let's assume some Java Runtime Environment is installed and ready.

Simply run the following on Linux or Mac OS:

cd $DERBY_INSTALL
java -jar lib/derbyrun.jar server start

On Windows run

cd %DERBY_INSTALL
java -jar lib\derbyrun.jar server start

That's it. Apache Derby will be waiting for connections on port 1527.

Running the sample

This sample is run as explained in How to run a sample. The 5 minutes 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.jta-plain

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

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

Verifying

When everything is up, go to http://localhost:8080/jta-plain. You see something like this:

And indeed, the implemented function is rather obvious: Manage a table of strings - called Thingies in the sample.

Now to the point...

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.

Detailed explanation

The sample has four modules:

  • com.atomikos: This module holds the Atomikos Transaction Essentials libraries - the actual transaction manager implementation (via the mvn fragment mvnFragment ntry/com.atomikos/mvnFragment.properties).
  • com.zfabrik.samples.jta-plain: This module implements supporting functions for the actual applications.
  • com.zfabrik.samples.jta-plain.domain: The domain definition module. This contains the JPA entities and a "Thingy Repository" implementation.
  • com.zfabrik.samples.jta-plain.web: The simple web application that lists the database content and allows to add and delete thingies.

In order to make Atomikos usable, it's transaction manager and user transaction implementations have to be configured and instantiated. All of that is done in com.zfabrik.samples.jta-plain in the form of Z2 components exposed for re-use by other modules. Similarly the Atomikos XA Data Source implementation is made available from this module (Note: You cannot -for all practical matters - separate data source implementations from the transaction management implementations).

Unlike Sample-jta-spring, the starting point for integrations with the transaction management system - although done only once - are a little more distributed:

In order to simplify transaction manager access from various places, we use a utility class Transactions. This class uses components in com.zfabrik.samples.jta-plain and effectively de-couples the JTA implementation from its access points. This class is used for

Updated by Henning Blohm almost 10 years ago · 16 revisions