Project

General

Profile

Actions

Sample-jta-spring » History » Revision 8

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


Using a Spring configured full-blown transaction manager across modules in Z2

As does Sample-jta-plain, so does this sample demonstrate how to use Z2 with the Atomikos transaction manager. In this case however, we choose to use the Spring framework to configure the transaction manager and enable re-use using Z2's support for modular Spring applications (see also How to Spring).

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

This sample is stored in z2-samples.jta-spring. It implements exactly the same scenario as Sample-spring-hibernate 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-spring

# 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-spring. You see something like this:


tra
And indeed, it's the same database we use elsewhere.

Now to the point...

This sample illustrates how to use a third-party transaction manager, the Atomikos implementation in this case, in a modular application as indicated in How_to_TransactionManager. This in conjunction with Spring resulting in a full-blown, highly adaptable and completely modular environment, scalable in deployment and development.

Unlike Sample-jta-plain, Atomikos configuration and set up of transaction manager and user transaction is done by Spring. This may be perceived a more consistent approach by Spring users.

Instead of invoking the JTA Transaction Manager interface directly, transaction demarcation is done using Spring's \@Transactional annotation (in the Controller). Note that Spring transaction de-marcation cannot be used with direct transaction manager usage at the same time (as Spring keeps additional context).

Updated by Henning Blohm almost 10 years ago · 8 revisions